vim代码自动格式化

Vim、Emacs配置和使用
回复
vimindel
帖子: 2
注册时间: 2010-04-22 22:43

vim代码自动格式化

#1

帖子 vimindel » 2010-04-22 22:46

我在shell中使用vim编辑C代码,可是每次按回车后光标都会到下一行的最开头,而不是自动格式化到正确的位置。请问如何使vim打开格式化代码的功能呢?
头像
adagio
论坛版主
帖子: 22110
注册时间: 2008-02-17 23:47
来自: 美丽富饶的那啥星球

Re: vim代码自动格式化

#2

帖子 adagio » 2010-04-22 23:15

不懂你的“自动格式化”啥意思
明天就换大三八!
——8核CPU、8G内存、8T硬盘……
8卡交火,80寸大屏放8个……
IPv8的光纤要8条……

---------------------------------------------------------------------------------
[图片版]新手当自强(续)FAQ
[新手进阶]挂载、fstab、调整linux分区
[图片版]E17桌面环境配置手把手
vimindel
帖子: 2
注册时间: 2010-04-22 22:43

Re: vim代码自动格式化

#3

帖子 vimindel » 2010-04-22 23:56

如我输入if(){,按回车后我希望光标已经缩进好而不是停留在开头我还得手工TAB几下才能缩进
头像
佚之狐L
帖子: 840
注册时间: 2008-01-28 13:02

Re: vim代码自动格式化

#4

帖子 佚之狐L » 2010-04-22 23:56

默认是选中后=号键
携书弹剑走黄沙,瀚海天山处处家;
大漠西风飞翠羽,江南八月看桂花
头像
adam8157
帖子: 2794
注册时间: 2009-03-05 16:31
联系:

Re: vim代码自动格式化

#5

帖子 adam8157 » 2010-04-22 23:58

代码: 全选

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Indent
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Auto indent
set autoindent

" Smart indet
set smartindent

" C-style indeting
set cindent

" Set tabstop width
set tabstop=4
set shiftwidth=4
set softtabstop=4

" Enable filetype plugin
filetype on
filetype plugin on
filetype indent on

" Set <BS> delete fake tabs
set smarttab

" Insert spaces instead of real tabs
set expandtab
zerger
帖子: 23
注册时间: 2010-01-05 22:47

Re: vim代码自动格式化

#6

帖子 zerger » 2010-04-23 12:08

vimindel 写了:我在shell中使用vim编辑C代码,可是每次按回车后光标都会到下一行的最开头,而不是自动格式化到正确的位置。请问如何使vim打开格式化代码的功能呢?
196 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
197 " 6. Autocommands
198 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
199 " do c-style indenting, cinoptions changes:
200 ":N Place case labels N characters from the indent of the switch(). (default 'shiftwidth').
201 "gN Place C++ scope declarations N characters from the indent of the block they are in. (default 'shiftwidth').
202 "tN Indent a function return type declaration N characters from the margin. (default 'shiftwidth').
203 "(N When in unclosed parentheses, indent N characters from the line with the unclosed parentheses. (default 'shiftwidth' * 2).
204 autocmd FileType c,cpp :set cindent cinoptions=:0,g0,t0,(0 textwidth=79 formatoptions=tcroqmB

更多设置请参考我的博客: "Vim学习笔记11: 晒晒我的
.vimrc"(http://guoyoooping.blog.163.com/blog/st ... 317316862/)
混混人见人爱
帖子: 55
注册时间: 2009-11-24 13:05

Re: vim代码自动格式化

#7

帖子 混混人见人爱 » 2010-04-25 19:50

vi ~/.vimrc

加入
set cindent
hpccn
帖子: 5
注册时间: 2010-08-05 15:24

Re: vim代码自动格式化

#8

帖子 hpccn » 2010-08-15 18:41

自动格式化代码命令

gg=G
回复