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
~/.vimrcput 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=100increase default history from 20 to 100set nobackupdon’t create backup filesset noswapfiledon’t create swap filescolorscheme murphya dark colored themeset showcmdshow partial Normal mode command on Command Line and character/line/block-selection for Visual modeset wildmode=longest,list,fullbash 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=0no. of characters in a line after which Vim will automatically create new linefiletype plugin indent onVim installation comes with lot of defaults:echo $VIMRUNTIMEgives your installation directory. One of them isindentdirectory 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 autoindentcopy 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=4defines how many characters to use when using indentation commands like>>set tabstop=4defines how many characters Tab key is made ofset expandtabchange Tab key to be made up of Space characters instead of single Tab characterset cursorlinehighlight 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
nnoremapNormal mode non-recursive mappingvnoremapVisual mode non-recursive mappinginoremapInsert mode non-recursive mappinginoreabbrevInsert mode non-recursive abbreviation:h :map-commandsfor more info
Search Settings
set incsearchthe cursor would move to matching pattern as you typeset hlsearchhighlight 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 / /\vautomatically add very magic mode modifier for forward direction searchnnoremap ? ?\vautomatically 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>PressF2function key to save file. PressingEsckey gets quite natural after writing text in Insert mode. Instead of multiple key presses to save using Command Line,F2is easiernnoremap #3 :wq<CR>PressF3to save the file and quitnnoremap #4 ggdGPressF4to delete entire contents of filennoremap #5 gg"+yGPressF5to 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>aPressF2to save file in Insert mode as well- Note the mapping sequence - it requires going to Normal mode first. Hence the
acommand 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>eaCtrl+e to move to end of wordinoremap <C-b> <C-o>bCtrl+b to move to beginning of wordinoremap <C-a> <C-o>ACtrl+a to move to end of lineinoremap <C-s> <C-o>ICtrl+s to move to start of line- Can't use Ctrl+i remapping as it affects Tab as well
inoremap <C-v> <C-o>"+pCtrl+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/python3In Insert mode, use py for Python interpreter pathinoreabbrev teh theAutomatically 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 useicommand 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 :autocmdand: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 $VIMRUNTIMEget path of installation directorymkdir -p ~/.vim/plugincreate required directories from terminal or within Vim if you prefercp /usr/share/vim/vim74/macros/matchit.vim ~/.vim/plugin/replace/usr/share/vim/vim74with output of echo if it is different in your case:h matchit-installfor more info
Guioptions
set guioptions-=mno menu barset guioptions-=Tno tool bar- See also
:h guioptions