Customizing the editor
- General Settings
- Text and Indent Settings
- Mappings
- Search Settings
- Custom Mappings
- Abbreviations
- autocmd
- Matchit
- Guioptions
- Further Reading
Different programming languages require different syntax, indentation, etc. The company you work for might have its own text format and guidelines. You might want to create a short-cut for frequently used commands or create your own command
~/.vimrc
put your custom settings in this file, see this sample vimrc file for example- Based on Vim version
7.4
- Some mappings are suitable only for gvim
General Settings
set history=100
increase default history from 20 to 100set nobackup
don’t create backup filesset noswapfile
don’t create swap filescolorscheme murphy
a dark colored themeset showcmd
show partial Normal mode command on Command Line and character/line/block-selection for Visual modeset wildmode=longest,list,full
bash like Tab completion- first tab hit will complete as much as possible
- second tab hit will provide a list
- third and subsequent tabs will cycle through
- How to set persistent Undo
Text and Indent Settings
set textwidth=0
no. of characters in a line after which Vim will automatically create new linefiletype plugin indent on
Vim installation comes with lot of defaults:echo $VIMRUNTIME
gives your installation directory. One of them isindent
directory which has indent styles for 100+ different file types- Using this setting directs Vim to apply based on file extensions like
.pl for Perl
,.c for C
,.v for verilog
, etc
set autoindent
copy indent on starting a new line- useful for files not affected by
filetype plugin indent on
- See also
:h smartindent
- useful for files not affected by
set shiftwidth=4
defines how many characters to use when using indentation commands like>>
set tabstop=4
defines how many characters Tab key is made ofset expandtab
change Tab key to be made up of Space characters instead of single Tab characterset cursorline
highlight the cursor line- Show vertical bar at particular column as visual guide for required textwidth
Mappings
Mappings allow to create new commands or redefine existing ones. Mappings can be defined for specific mode. Examples given here are restricted to specific mode and non-recursive
nnoremap
Normal mode non-recursive mappingvnoremap
Visual mode non-recursive mappinginoremap
Insert mode non-recursive mappinginoreabbrev
Insert mode non-recursive abbreviation:h :map-commands
for more info
Search Settings
set incsearch
the cursor would move to matching pattern as you typeset hlsearch
highlight searchpatternvnoremap * y/<C-R>"<CR>
press*
to search visually selected text in forward directionvnoremap # y?<C-R>"<CR>
press#
to search visually selected text in backward directionnnoremap / /\v
automatically add very magic mode modifier for forward direction searchnnoremap ? ?\v
automatically add very magic mode modifier for backward direction searchnnoremap <silent> <Space> :noh<CR><Space>
Press Space to clear highlighted searches<silent>
modifier executes the command without displaying on Command Line- Note that command also retains default behavior of Space key
Custom Mappings
Normal mode
nnoremap #2 :w<CR>
PressF2
function key to save file. PressingEsc
key gets quite natural after writing text in Insert mode. Instead of multiple key presses to save using Command Line,F2
is easiernnoremap #3 :wq<CR>
PressF3
to save the file and quitnnoremap #4 ggdG
PressF4
to delete entire contents of filennoremap #5 gg"+yG
PressF5
to copy entire contents of file to system clipboardnnoremap Y y$
change Y to behave similar to D and C- Check out use of Leaders for even more mapping tricks
Insert mode
inoremap <F2> <Esc>:w<CR>a
PressF2
to save file in Insert mode as well- Note the mapping sequence - it requires going to Normal mode first. Hence the
a
command to get back to Insert mode inoremap <F2> <C-o>:w<CR>
alternate way, Ctrl+o allows to execute a command and return back to insert mode automatically
- Note the mapping sequence - it requires going to Normal mode first. Hence the
inoremap <C-e> <Esc>ea
Ctrl+e to move to end of wordinoremap <C-b> <C-o>b
Ctrl+b to move to beginning of wordinoremap <C-a> <C-o>A
Ctrl+a to move to end of lineinoremap <C-s> <C-o>I
Ctrl+s to move to start of line- Can't use Ctrl+i remapping as it affects Tab as well
inoremap <C-v> <C-o>"+p
Ctrl+v to paste from clipboard in insert mode- Ctrl+q can be used instead of Ctrl+v to insert characters like Enter key (may not work with vim)
- See also How to make vim paste from (and copy to) system's clipboard?
inoremap <C-l> <C-x><C-l>
Ctrl+l to autocomplete matching lines- just saves the additional Ctrl+x, see also
:h i_CTRL-x
- just saves the additional Ctrl+x, see also
- See also
:h ins-special-special
Abbreviations
From typo correction to short-cuts to save typing, abbreviations are quite useful. Abbreviations get expanded only when they stand apart as a word by itself and not part of another word. For example, if the letter p is abbreviated, it is activated in variety of ways like pressing Esc, Space, Enter, Punctuations, etc not when it is part of words like pen, up, etc
inoreabbrev p #!/usr/bin/perl<CR>use strict;<CR>use warnings;<CR>
In Insert mode, type p followed by Enter key - this will automatically add the Perl interpreter path and two statementsinoreabbrev py #!/usr/bin/python3
In Insert mode, use py for Python interpreter pathinoreabbrev teh the
Automatically correct typo 'teh' to 'the'inoreabbrev @a always @()<CR>begin<CR>end<Esc>2k$
This one works best with @a followed by Esc key in Insert mode. This inserts an empty always block (used in Verilog) and places the cursor at end of first line. After which usei
command to type inside the parenthesis
autocmd
Execute commands based on events
augroup plpy
autocmd!
" automatically add Perl path using previously set inoreabbrev for p
autocmd BufNewFile *.pl :normal ip
" automatically add Python path
autocmd BufNewFile *.py :normal ipy
" Prevent comment character leaking to next line
autocmd FileType * setlocal formatoptions-=r formatoptions-=o
augroup END
Further Reading
:h :autocmd
and:h autocmd-groups
- autocmd tutorial
- augroup tutorial
Matchit
set matchpairs+=<:>
adds <> to%
matchpairs
To match keywords like HTML tags, if-else pairs, etc with %
, one can install matchit.vim
plugin. It is not enabled by default as it is not backwards compatible
:echo $VIMRUNTIME
get path of installation directorymkdir -p ~/.vim/plugin
create required directories from terminal or within Vim if you prefercp /usr/share/vim/vim74/macros/matchit.vim ~/.vim/plugin/
replace/usr/share/vim/vim74
with output of echo if it is different in your case:h matchit-install
for more info
Guioptions
set guioptions-=m
no menu barset guioptions-=T
no tool bar- See also
:h guioptions