Aggiungi quanto segue al .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
Se si desidera che tutti i tipi di file per essere auto-frastagliata al momento del salvataggio, che vi consiglio vivamente di non, aggiungere questo gancio per il vostro .vimrc
:
" Indent on save hook
autocmd BufWritePre <buffer> call Indent()
Se si desidera che solo determinati tipi di file ad essere auto-rientrato su Sa ve, che raccomando, quindi seguire le istruzioni.Diciamo che si desidera file C++ per essere auto-frastagliata su Save, quindi creare ~/.vim/after/ftplugin/cpp.vim
e mettere questo gancio c'è:
" Indent on save hook
autocmd BufWritePre <buffer> call Indent()
Lo stesso sarebbe andato per altri tipi di file, vale a dire ~/.vim/after/ftplugin/java.vim
per Java e così via.
fonte
2013-04-14 00:14:34
Invece di un trigger prima di salvare, utilizzare 'set autoindent' e rientrare il codice correttamente, in primo luogo! –