RockPaperScissors

LizardSpock

Vimfu Reindent

| Comments

The latest addition to my .vimrc below, allows ;g to smart indent the current file. It is the same as gg=G but returning you to the same position.

Update based on Recommendations from Drew Neil and Jonathan Palardy, a more generic pereserving state function, which can be mapped to a key sequence with specific commands.

function! Preserve(command)
  " Preparation: save last search, and cursor position.
  let _s=@/
  let l = line(".")
  let c = col(".")
  " Do the business:
  execute a:command
  " Clean up: restore previous search history, and cursor position
  let @/=_s
  call cursor(l, c)
endfunction

" SmartIndent keeping cursor position
map ;g :call Preserve("normal! gg=G")<CR>

If this does not work for a particular file type you use you probably need to look at getting a *.syntax file for your language.

My Original Method

function! Indent()
  " Capture Current Line
  let currentline_num = line(".")
  let currentcol_num = virtcol('.')

  " Reindent from start to end of file
   normal! gg=G

   " Restore Current Line and column
   exe 'normal '.currentline_num.'G'.currentcol_num.'|'
endfunction

"assign to ;g in normal mode
map ;g :call Indent()<CR>

Comments