Vim
This page is not supposed to be a tutorial or a FAQ. It's just got some quick reference pointers. Vim has a reputation for being hard to use. OK, you might be stumped after loading it up for the first time, but it only takes a few minutes to get going. The First steps in Vim page will get you up and running in no time
Overriding System Set Options
Depending on the type of file you're editing, it can seem that vim will
ignore some options that you've setup in your .vimrc
file. In fact the case is,
rather than being ignored they are being overridden by the corresponding
file type plugin file read on start up. One particular case of this is when
editing c files, the option to automatically insert a comment leader after a
carriage return from a commented line is enabled.
If you look in the corresponding file type plugin (ftplugin
) file
(/usr/share/vim/vim64/ftplugin/c.vim
) you can see the formatoption
being changed. Here's the relevant excerpt.
/usr/share/vim/vim64/ftplugin/c.vim
" Set 'formatoptions' to break comment lines but not other lines, " and insert the comment leader when hitting <CR> or using "o". setlocal fo-=t fo+=croql
Finding out if your options are being overridden and where the overriding
occurs is quite easy. In the case of formatoptions
use the following vim
command.
:verbose set formatoptions
The output, an example given below, states where the option was last set. So it's possible to
check if your .vimrc
options are being overridden.
formatoptions=croql Last set from /usr/share/vim/vim64/ftplugin/c.vim
To restore the options that you really want, you need to create a file
that is read after the file type plugin file. These are placed in
~/.vim/after/ftplugin/
and are given the same name as the system
ftplugin
file.
~/.vim/after/ftplugin/c.vim
set formatoptions-=r
Now when you try to edit a c file and do the same test to see where the
formatoptions
were last set, you can see that it was in your new ftplugin
file.
formatoptions=croql Last set from /home/xxx/.vim/after/ftplugin/c.vim
Example .vimrc
File
Below I've listed my vim configuration file. Hopefully you might spot some useful features from
this example. Being a Linux user I only use the shell version, rather than the window managed GVim
version. However, a lot of the options still make sense.
~/.vimrc
" Use vim features - ignore vi compatibility set nocompatible " Make sure vim checks for 'set commands' in opened files set modeline set modelines=1 " Make the window title reflect the file being edited set title set titlestring=VIM:\ %F " At command line, complete longest common string, then list alternatives. set wildmode=longest,list " Automatically insert the current comment leader " after hitting 'o' or 'O' in Normal mode. set fo+=o " Do not automatically insert a comment leader after an enter set fo-=r " Do no auto-wrap text using textwidth (does not apply to comments) set fo-=t " Turn off the bell set vb t_vb= " Enable the mouse set mouse=a behave xterm set selectmode=mouse " Set list Chars - for showing characters that are not " normally displayed i.e. whitespace, tabs, EOL set listchars=trail:.,tab:>-,eol:$ set nolist " Show incomplete paragraphs set display+=lastline " Turn ruler on set ruler " Set horizontal scrolling jump to 10 set sidescroll=10 " Turn off end of line wrapping set nowrap " Make sure status line is always visible set laststatus=2 " Make command line two lines high set ch=2 " Make the Tab spacing 3 characters wide set ts=3 " Make the shift width 3 characters wide set sw=3 " Allow backspace to delete set bs=2 " Make window height VERY large so they always maximise on window switch set winheight=9999 " Switch off search pattern highlighting. set nohlsearch " Turn Brace/Bracket match showing off set noshowmatch " Setup backup location and enable set backupdir=~/backup/vim set backup " Set Swap directory set directory=~/backup/vim/swap " Show/Hide hidden Chars map <silent> <F2> :set invlist<CR> " Show/Hide found pattern (After search) map <silent> <F3> :set invhlsearch<CR> " Remove whitespace from end of lines map <silent> <F4> :%s/\s\+$//g<CR> " Make F5 reload .vimrc map <silent> <F5> :source ~/.vimrc<CR> " Do a word count map <silent> <F7> g<C-G> " Format paragraph map <silent> <F8> gwap " Set html creation to use style sheets let html_use_css = 1 " Mapping for creating HTML of current buffer map <silent> <F9> :runtime! syntax/2html.vim<CR> " Set up Printer options set printoptions=left:15mm,right:15mm,top:15mm,bottom:15mm,paper:A4,header:2,syntax:n set printfont=courier_new:h7 " Set Printer up set printexpr=PrintFile(v:fname_in) if !exists("*PrintFile") function PrintFile(fname) call system("lpr -r -PLaserjet " . a:fname) return v:shell_error endfunc endif " Enable 'in-column' up and down motion in INSERT mode on wrapped lines imap <silent> <Up> <C-o>gk imap <silent> <Down> <C-o>gj " Enable 'in-column' up and down motion on wrapped lines map <silent> <Up> gk map <silent> <Down> gj " Map shift up and down to page scrolling map <S-Up> <C-E> map <S-Down> <C-Y> " Folding options set foldmethod=marker set foldcolumn=2 set foldtext=MyFoldText() if !exists("*MyFoldText") function MyFoldText() let line = getline(v:foldstart) let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g') return "FOLD (" . (v:foldend - v:foldstart) . ")" . sub endfunction " /* }}} - End folding on this function declaration */ endif " Switch on syntax highlighting. syntax on " Use colours that work well on a dark background set background=dark " Set some nice highlighting colours hi Normal guifg=white guibg=black hi Comment ctermfg=brown hi Folded ctermfg=red hi FoldColumn ctermbg=grey ctermfg=red hi NonText ctermbg=black ctermfg=blue hi SpecialKey ctermbg=black ctermfg=grey hi Search ctermbg=red ctermfg=white hi Todo ctermbg=yellow ctermfg=white