Cheat Logo

Cheat Sheet

Hints for vim

  1. .vimrc
  2. Line by Line movement
  3. Navigation
  4. OS X GVim
  5. Search and replace
  6. Spelling
  7. Vim Folding

.vimrc

Last updated: 2010-06-10
Level: Intermediate
OS:
  • Windows
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

A custom .vimrc file can save lots of time and make editing easier. Put the following commands in ~/.vimrc:

set nohlsearch 
set autoindent
set tabstop=4
set shiftwidth=4
set ruler
set showcmd
syntax on
set bg=dark
set spell " enables live spell checking
nnoremap <up> gk
nnoremap <down> gj
inoremap <up> <C-o>gk
inoremap <down> <C-o>gj

Line by Line movement

Last updated: 2010-10-07
Level: Intermediate
OS:
  • Windows
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

If you dislike vim jumping over all lines when you have a line that wraps, use gk and gj to move up and down. If you want to map these to the up and down arrows, add the following two lines to your .vimrc file:

nnoremap <up> gk
nnoremap <down> gj

Fortunately, doing this does not make it so that gk and gj are inserted when in insert mode. Up and down return to their normal mode.

To have up and down have the same behavior in insert mode add the following to your .vimrc file:

inoremap <up> <C-o>gk
inoremap <down> <C-o>gj

Navigation

Last updated: 2010-06-10
Level: Beginner
Tags:

^f forward screen ^b back screen

OS X GVim

Last updated: 2010-10-07
Level: Beginner
OS:
  • Windows
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

To begin, download the latest stable version of MacVim. Move the .app to your Applications folder.

Make sure you have a ~/.bin folder and it is included in you PATH variable in your .bash_profile file.

In your .bin folder create the following script named gvim and make sure you chmod +x the file.

#!/bin/bash
if [ $# -eq 0 ]; then
	# just open GVim
	open /Applications/MacVim.app
else
	# create the file if it does not exist
	if [ ! -f "$1" ]; then
		touch $1
	fi
	# open the file with GVim
	open $1 -a /Applications/MacVim.app
fi

Next create your ~/.gvimrc file. Example file:

set backspace=indent,eol,start " allows all proper backspacing
set vb " visual beep
set lines=56
set columns=92
set anti " antialiasing on
set gfn=Monaco:h12 " set the font and size
colorscheme torte " set the colorscheme to torte, if torte is not found, use billw
set guioptions-=T " removes toolbar

Search and replace

Last updated: 2010-09-09
Level: Intermediate
Tags:
Programs:

vim uses regular expressions for search and replace.

use /foo to find foo in the text.

s/foo/bar replaces all foo on a line with bar.

To replace all foo with bar in the whole file use g/foo/s//bar/g

When searching for text it can be helpful to highlight all occurrences. To enable this use the command :set hlsearch. To disable it use :set nohlsearch.

To search for foo and replace it with bar within a visual selection use :%s/\%Vfoo/bar/g.

Spelling

Last updated: 2010-06-10
Level: Beginner
Tags:

To enable live spell checking in vim, use :set spell

Vim Folding

Last updated: 2010-10-07
Level: Intermediate
OS:
  • Windows
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

Use V or v to select a few lines then type zf. This will collapse that selection. To open it again, go to the folded line and hit the right arrow.

©2015 Schutt Design (Luke & Noel Schutt).
Contact us using one of our first names at schuttdesign dot net.