Vim自动缩进或保存时自动缩进
自动缩进
这非常方便,特别是如果正在使用Vim进行快速代码编辑,甚至进行长时间的编码会话。强制执行特定的缩进样式。
在~/.vimrc文件中,添加以下选项:
syntax enable
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
新行将自动缩进,花括号将自动对齐。
保存时自动缩进
要在保存时自动缩进文件,请将此添加到您的vimrc中:
augroup autoindent
au!
autocmd BufWritePre * :normal migg=G`i
augroup End
如果只想对某些文件(例如sss文件)执行此操作,您可以更改正则表达式:
autocmd BufWritePre * :normal migg=G`i
到
autocmd BufWritePre *.scss :normal migg=G`i
操作方式
- autocmd BufWritePre specifies this is a command to be executed automatically before writing the buffer to file.
- '* matches the files to run this auto-command on. If we want only text files, use *.txt, or only html files, use *.html, etc.
- :normal says to execute the following command in normal mode
- mi puts a mark on the current line, and saves it in “i”.
gg goes to the top of the file - = is the indentation command, a motion is needed following the = command
- G tells the = command to auto-indent to the bottom of the file
- `i says to go to the mark stored in i
- augroup and au! are for good practive
要查看有关标记的更多信息
http://vim.wikia.com/wiki/Using_marks
要了解有关自动命令的更多信息
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
要了解为什么它包含在 augroup 中
http://learnvimscriptthehardway.stevelosh.com/chapters/14.html