How to tell vim to automatically indent before saving

I recently started using vim for my alumni projects. The main problem, I come across the fact that sometimes I check the code with indentation. I feel that if I can somehow make the auto-indent shortcut + save + close, then this should solve my problem.

My .vimrc file is:

set expandtab set tabstop=2 set shiftwidth=2 set softtabstop=2 set pastetoggle=<F2> syntax on filetype indent plugin on 

Is there a way to create such command shortcuts and rewind with: x (save + exit).

Please let me know.

+6
source share
3 answers

Add the following to your .vimrc :

 " Restore cursor position, window position, and last search after running a " command. function! Preserve(command) " Save the last search. let search = @/ " Save the current cursor position. let cursor_position = getpos('.') " Save the current window position. normal! H let window_position = getpos('.') call setpos('.', cursor_position) " Execute the command. execute a:command " Restore the last search. let @/ = search " Restore the previous window position. call setpos('.', window_position) normal! zt " Restore the previous cursor position. call setpos('.', cursor_position) endfunction " Re-indent the whole buffer. function! Indent() call Preserve('normal gg=G') endfunction 

If you want all file types to be automatically indented when saving , which I highly recommend against , add this hook to your .vimrc :

 " Indent on save hook autocmd BufWritePre <buffer> call Indent() 

If you want to save only certain types of files automatically , which I recommend , follow the instructions. Suppose you want C ++ files to be automatically indented when saved, then create ~/.vim/after/ftplugin/cpp.vim and put this hook there:

 " Indent on save hook autocmd BufWritePre <buffer> call Indent() 

The same could be used for any other types of files, i.e. ~/.vim/after/ftplugin/java.vim for Java, etc.

+14
source

I would recommend enabling autoindent to avoid this problem in the first place. At each stage of development, it is much easier to work with correctly aligned code.

 set autoindent 

Read the docs via :help autoindent .

However, the = command will indent line by line according to file type rules. You can create autocmd BufWritePre to indent the entire file.

I have not tested this and do not know how much it will really work:

 autocmd BufWritePre * :normal gg=G 

Read :help autocmd for more information on this topic. gg=g breaks like:

  • :normal is executed as a command to edit normal mode, not a command :ex
  • g g go to beginning of file
  • = indent while ...
  • g ... end of file.

I really do not recommend this strategy. Use set autoindent . It is probably unreasonable to define this autocmd for all files (as with * ). This can only be done for certain types of files:

 " Only for c++ files, for example autocmd BufWritePre *.cpp :normal gg=G 
+5
source

To defer a file that already exists, you can use the shortcut gg=G (and not the command, just press g twice, then = , then Shift+g ), especially if you use filetype indent ... line.

Vim: gg = G align left, not auto-indent

+1
source

All Articles