2 minute read

Vim tips you might find useful. Some of these hints are taken from the 7 Habits for Effective Text Editing 2.0 video and the references listed below.


Normal editing

A   append at end of line cw  change word
ciw  change in word ci(  change in (
ci[  change in [
...  change in {“’

Autocompletion (in edit mode)

^Cx ^Cn: Word completion - forward
^Cx ^Cp: Word completion - backward
^Cx ^Cl: Line completion
^Cx ^Cf: File name completion
^Cx ^Ck: Dictionary based completion

You have to set the dictionary option in your .vimrc to use dictionary based completion.

set dictionary+=/usr/share/dict/words

Searching & Replacing

Hacks for searching in vim.

 *              search the word under the cursor
 :set hlsearch  highlight search results`

Insert a pagebreak using search & replace. The following example inserts a pagebreak before and after every mention of the word Section.

%s:/Section [0-9]+/^M\0^M

^M is typed by first entering CTRL+V and entering CTRL+M afterwards.


Hacks regarding the navigation in the file / between files.

!ctags -R .          create a tags file for the current directory (and it’s sub-directories) tag {function}       jump to the given function
grep {text} **/*.py  search for {text} in all Python files
  cnext               move to the first occurrence of {text}  gf                  go to the filename under the cursor

Word/Paragraph Navigation

b|w                   go to the previous|next word
{ | }                 go to the beginning/end of the current paragraph
%                     jump between parenthesis and brace delimiters

Screen Navigation

H|M|L                 first/middle/last line of the current screen
^b|^f                 back/forward one page


m[a-z]                 set a mark within a file m[A-Z]                 set a mark across files `[a-z] or '[a-z]   go to the mark/line in which the mark is set

Applying commands to lines:

:[count] command [where] apply the command count times to the given line :1,.d  delete from line one to the current line :0,$d  delete everything

Source code navigation using cTags

  • Install the exuberant-ctags package.
  • Run ctags -R in the directory holding the source code, afterwards the following commands will work in vim

:ta {tagname}        jump to the given tag name (opening the required files, etc)
^]                   jump to the tag name under the cursor
^T                   return to the previous position

  • Installing the taglist.vim plugin provides a tag overview side panel.

Text Formatting

{visual}gq     format the highlighted text
gq{motion}     format the area covered by motion
gqap           format the current paragraph
gq{            format everything to the beginning of the current paragraph

Intend Code

Test Mode:

^T   indent code ^D   de-indent code

Set shift width: :set sw=4


A buffer is specified using “.

"mdd   use buffer ”m” for delete line "mp    paste from buffer m


Miscellaneous tips.

Spell Checking

To enable/disable spell checking type:

:set spell {spelllang=en_us|de_at}
:set nospell

Handling of spelling mistakes:

z=    show alternatives zg    mark word as good
zw    mark word as wrong
:]s   move to next mis-spelled word :[s   move to previous mis-spelled word

To get customized dictionaries working you’ll need to set the spellfile variable

:set spellfile=~/.vim/spellfile.{encoding}.add


Safe the current session settings

:mksession session.vim

Restore the session by starting vim with vim -S session.vim or source Session.vim.

File type specific Settings

Enable file type detection:


Use autocommands.

Example: Set auto indent for all python files:

:au *.py set ai

Include Settings in the current file

*/ ex: set tabstop=8 expandtab: */


Create a file called .exrc. All commands in this file get executed when vim starts.

Example .vimrc

set expandtab           " use spaces as tabs  :-)   
set sts=4               " use 4 softtabstops  
set ts=4  
set sw=4                " shiftwidth fixes tab  
set nocompatible  
syntax on  
set background=light  
set showmatch  
set ignorecase  
set showmode  
set autoindent  
set smartindent  
" dictionary for word auto completion  
set dictionary+=/usr/share/dict/words  
set guifont=Courier\ New\ 14   
" disable expandtab for Makefiles  
au Filetype make set noexpandtab


Leave a comment