Thursday, December 12, 2013

Vim indentation tips


Some Vim indentation tips to collect here mainly as a reminder. Note that the amount of spaces used for indentation is mainly governed by the variable shiftwidth.


  • Moving around (aka, prelude to action): 
    • H to top of screen, L to bottom, M to middle. Then use nG to go to line n, G alone to bottom of file, gg to top of file.
    • Go visual pressing v then move around to selct text. Once happy, you can copy (yy) or delete (dd) the selected text. 
  • Indent text (visual mode):
    • in command mode, press v then move cursor around (text will be highlighted), then press > to indent the highlighted text.
  • Indent text (markers):
    • position yourself then place a marker with ma, then move down to your block end and type >'a (a can be replaced with another valid character)
  • Indent text (fixed):
    • to indent current line type >>
    • to de-indent current line type <<
    • to indent 10 lines from the current one use 10>>
    • ... you get the gist ...
    • to indent lines from 10 to 20 type :10,20>
Some variables you can add to your .vimrc:

set shiftwidth=5    #Indent by 5 spaces when using >>, <<, etc.
set softtabstop=5   #Indent by 5 spaces when pressing
set autoindent      "Keep indentation from previous line

All of this is tested/verified for vim, not sure about vi.



No comments: