开发者

Emacs like strict auto-indent in vim

开发者 https://www.devze.com 2023-04-07 03:58 出处:网络
Emacs, while editing a C file, forces me to follow a particular indentation. When I press tab on an incorrectly indented line,开发者_开发问答 it corrects the indentation. I want this behaviour from my

Emacs, while editing a C file, forces me to follow a particular indentation. When I press tab on an incorrectly indented line,开发者_开发问答 it corrects the indentation. I want this behaviour from my vim editor. Till now I have done the following :

set cindent
set smartindent
set autoindent
set expandtab
set tabstop=2
set shiftwidth=2
set cinkeys=0{,0},:,0#,!,!^F

in my .vimrc file. However, I am not achieving the same emacs-like forced effect as I want.

Is it possible at all in vim ?


'smartindent' is obsolete. There's really no reason you need to have that in your vimrc.

'cindent' overrules 'smartindent', so setting both in your vimrc is pointless. Setting 'cindent' in your vimrc isn't very useful either, since it only really works well on C-like languages.

filetype indent on will enable the filetype-specific indentation plugins (c.f., the indent directory under $VIMRUNTIME). That paired with 'autoindent' at least gives you basic automatic indentation support regardless of what filetype you're editing.

If you want to add indent settings for a specific filetype, you can create your own indent script in ~/.vim/indent/<filetype>.vim, or ~/.vim/after/indent/<filetype>.vim if you're augmenting an existing system-wide indent script.

As the settings you posted show, pressing Ctrlf in insert mode will do what Emacs does when you press Tab. This is described at :help indentkeys-format. 'cinkeys' is used when 'cindent' is enabled and 'indentexpr' is empty. 'indentkeys' is for the reverse. It's just a slight change to modify the setting so Tab can be used in place of/in addition to Ctrlf.

On a final note, I'd recommend learning your way around Vim's help. It's very thorough and easy to use once you figure things out. :help usr_toc is a good place to start for user-level documentation. :help describes some of the basic about navigating the help, how to search for topics, etc.


The == command is what you want, if I understand you correctly. It reindents the current line according to syntax rules.

As for binding it to tab, that's certainly possible, but I have not done that and am not completely sure as to how you can catch the right moment when it should actually insert the tab and when it should reindent.

Personally, I find it less confusing to just press ==. = accepts a range even, so you can go into visual mode, make a selection and tap = and the region will be reindented.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号