Vim

Warning

Use at your own risk!!! Commands, procedures etc. described in this page worked for me. That does not mean they will automatically work for you.

Table of contents (sitemap)

Full table of contents

Vim is a highly configurable text editor built to enable efficient text editing. Vim is my favorite text editor. When managing Linux/UNIX knowledge knowledge of Vi is almost mandatory and using Vim makes it that much easier.

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

Vim is often called a “programmer’s editor,” and so useful for programming that many consider it an entire IDE. It’s not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

—- http://www.vim.org/

Basic vim is good, but extending knowledge and customizing your setup is highly recommended. vimcasts have a number of screencasts that are bound to improve your efficiency in using vim. And you can find a number of vim configurations on github (including mine).

One of the best features of Vim is that it is extendable through the use of plugins. My current list of plugins is not extensive but still useful:

VundleVim/Vundle.vim the plug-in manager for Vim
MarcWeber/vim-addon-mw-utils interpret a file by function and cache file automatically.
tomtom/tlib_vim Some utility functions for VIM
garbas/vim-snipmate snipMate.vim aims to be a concise vim script that implements some of TextMates snippets features in Vim.
scrooloose/nerdtree A tree explorer plugin for vim.
tpope/vim-fugitive a Git wrapper so awesome, it should be illegal
vim-airline/vim-airline lean & mean status/tabline for vim that’s light as air.
vim-airline/vim-airline-themes A collection of themes for vim-airline.

Notes

Put a ‘c’ after a substitute command

Not always needed but does help keeping control of your changes. If in stead of this:

s/foo/bar/g

You write this:

s/foo/bar/gc

Then vim will ask for confirmation on every change. This can help prevent you mutilating a document entirely.

vim-airline

If like me you mostly use vim through a terminal that by default does not use the xterm-256color terminal type using the vim-airline plugins may not be what you want.

My solution:

Conditional vim-airline addition.
if &term=~'xterm-256color'
  Plugin 'vim-airline/vim-airline'
  Plugin 'vim-airline/vim-airline-themes'
  if !exists("g:airline_symbols")
    let g:airline_symbols = {}
  endif
  let g:airline_theme="luna"
  let g:airline_powerline_fonts=1
  let g:airline#extensions#branch#empty_message  =  "no .git"
  let g:airline#extensions#whitespace#enabled    =  0
  let g:airline#extensions#syntastic#enabled     =  1
  let g:airline#extensions#tabline#enabled       =  1
  let g:airline#extensions#tabline#tab_nr_type   =  1 " tab number
  let g:airline#extensions#tabline#fnamecollapse =  1 " /a/m/model.rb
  let g:airline#extensions#hunks#non_zero_only   =  1 " git gutter
endif

Non greedy match

Non greedy match
s/\".\{-}\"/foo/

If you need the shortest match between quotes or whatever adding .\{-} to the pattern turns the match from greedy to non greedy.

Take the following line:

item1="1" item="2"

A normal substitute like this:

s/\".*\"/foo/

Will result in:

item1=foo

Changing the substitute with .\{-}:

s/\".\{-}\"/foo/

Will result in:

item1=foo item="2"

See: nongreedy for more information.

Move matching lines to bottom

Move matching lines to bottom
v/pattern/normal ddGp
  • v [command]: inverse match
  • normal [action]: Execute Normal mode commands
  • dd [command]: delete (cut) command
  • G [command]: go to the end of the file
  • p [command]: paste line in buffer

Match a pattern and uppercase the match

Match a pattern and uppercase the match
s/g_\(.\{-}\)\>/\U\1\E/gc
  • s [command]: substitute
  • \(<pattern>\) [pattern]: store the patterns results for use in the replacement.
  • .\{-} [pattern]: non greedy match
  • \> [pattern]: end of a word
  • \U [action]: uppercase
  • \1 : result of the first \(<pattern>\)
  • \E : end of \u, \U, \l and \L.

See: substitute