11

I have recently started using Vim for my graduate level projects. The main problem I face is that sometimes I check in unindented code. I feel if I can somehow create a shortcut of auto-indent+save+close then that should solve my problem.

My .vimrc file:

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

Is there any way to create such command shortcuts & override with :x (save+exit).

Please let me know.

1
  • 4
    Instead of a trigger before saving, use set autoindent and indent your code properly in the first place! Apr 13, 2013 at 19:57

4 Answers 4

18

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 auto-indented on save, which I strongly recommend against, add this hook to your .vimrc:

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

If you want only certain file types to be auto-indented on save, which I recommend, then follow the instructions. Lets say you want C++ files to be auto-indented on save, then create ~/.vim/after/ftplugin/cpp.vim and put this hook there:

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

The same would go for any other file types, i.e. ~/.vim/after/ftplugin/java.vim for Java and so on.

2
  • Why do you strongly recommend against auto indenting on save? May 2, 2018 at 19:41
  • @BallpointBen, I strongly recommend against auto-indenting on save for all file types as this will most likely be not what you'd expect. Instead, put auto-indenting on save hooks only to the dedicated file types as shown in the answer (e.g. for C++). May 3, 2018 at 12:24
11

I would recommend turning on autoindent to avoid this problem in the first place. It is much easier to work with properly indented code at every stage of development.

set autoindent

Read the docs via :help autoindent.

However, the = command will indent lines according to the filetype's rules. You can create a BufWritePre autocmd to perform indentation on the whole file.

I've not tested this, and don't know how well it will actually work:

autocmd BufWritePre * :normal gg=G

Read :help autocmd for more information on the topic. gg=g breaks down as:

  • :normal execute as a normal mode editing command rather than an :ex command
  • gg move to the top of the file
  • = indent until...
  • G ... the end of the file.

I really don't recommend this strategy though. Get used to using set autoindent instead. It's probably unwise to define this autocmd on all files (as with *). It could be done on certain filetypes only:

" Only for c++ files, for example
autocmd BufWritePre *.cpp :normal gg=G
4
  • 2
    Do not use normal without bang in scripts, gg=G does not necessary break down as you written it here in this case.
    – ZyX
    Apr 13, 2013 at 22:23
  • This command is helpful unless you have tendency to save the file after every insertion, because it will bring the cursor back to gg, making it not useable.
    – Sbu
    Mar 3, 2017 at 6:10
  • 2
    @SimonBudin you could use magg=G`a which will create a mark on the 'a' register, reindent the file, then take you back to mark 'a' Aug 10, 2018 at 18:32
  • 1
    @TylerFowle, this is the exact behaviour I was wanting. Thank you for that gem. Aug 13, 2020 at 2:50
2

To indent a file that already exists, you can use the shortcut gg=G (not a command; just press g twice, then =, then Shift+g), particularly since you are using the filetype indent ... line.

Vim: gg=G aligns left, does not auto-indent

0

I would go with

autocmd BufWritePre * :normal gg=G``

Such will indent whole the file and get back to the recent cursor position.

  • did tried to add <C-o> <C-o> which failed, however double tick is a robust solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.