Indentation in vi editor
In general many of us might get irritated by working on vi editor since we are more used to MSword/any windows editor.Below i present some very useful tips for indentation purposes in vi editor.In the commands below, "re-indent" means "indent lines according to your indentation settings."
shiftwidth
is the primary variable that controls indentation.General Commands
>> Indent line by shiftwidth spaces
<< De-indent line by shiftwidth spaces
5>> Indent 5 lines
5== Re-indent 5 lines
>% Increase indent of a braced or bracketed block (place cursor on brace first)
=% Reindent a braced or bracketed block (cursor on brace)
<% Decrease indent of a braced or bracketed block (cursor on brace)
]p Paste text, aligning indentation with surroundings
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
>i{ Increase inner block indent
<i{ Decrease inner block indent
You can replace
{
with }
or B
, e.g. =iB
is a valid block indent command. Take a look at "Indent a Code Block" for a nice example to try these commands out on.Also, remember that
. Repeat last command
, so indentation commands can be easily and conveniently repeated.Re-indenting complete files
Another common situation is requiring indentation to be fixed throughout a source file:
gg=G Re-indent entire buffer
You can extend this idea to multiple files:" Re-indent all your c source code:
:args *.c
:argdo normal gg=G
:wall
Or multiple buffers:" Re-indent all open buffers:
:bufdo normal gg=G:wall
In Visual Mode
Vjj> Visually mark and then indent 3 lines
In insert mode
These commands apply to the current line:
CTRL-T insert indent at start of line
CTRL-D remove indent at start of line
0 CTRL-D remove all indentation from line
Ex commands
These are useful when you want to indent a specific range of lines, without moving your cursor.
:< and :> Given a range, apply indentation e.g.
:4,8> indent lines 4 to 8, inclusive
Indenting using markers
Another approach is via markers:
ma Mark top of block to indent as marker 'a'
...move cursor to end location>'a Indent from marker 'a' to current location
Variables that govern indentation
You can set these in your .vimrc file.
set expandtab "Use softtabstop spaces instead of tab characters for indentation
set shiftwidth=4 "Indent by 4 spaces when using >>, <<, == etc.
set softtabstop=4 "Indent by 4 spaces when pressing <TAB>
set autoindent "Keep indentation from previous line
set smartindent "Automatically inserts indentation in some cases
set cindent "Like smartindent, but stricter and more customisable
Vim has intelligent indentation based on filetype. Try adding this to your .vimrc:if has ("autocmd")
" File type detection. Indent based on filetype. Recommended.
filetype plugin indent on
endif
Helpful page - Thanks!
ReplyDelete