A great editor for programmers and awesome people.


Movement

  • h, l, k, j : for character-wise movement to the left, right, up, and down.
  • b, w: move the cursor 1 word token to the left/right

Yanking and Killing

Yanking and Killing are the equivalent to window’s copy and cut commands. I’m not gonna cover basic usage here.

Paste from a register (can be used on the command line):
<ctrl-r>" Where the " is the register character. (Use " for the default register).

Marking and Jumping To Positions

  • create a mark: ma (name of mark is a, can be a-z)
  • go to mark: `a

Find and Replace

The editor command ":%s/foo/bar/gc" searches the whole file and asks you to confirm the replacement. Omit the trailing c to do the replacing without confirmation.

Unicode

Unicode chars can be found within files using vim’s search pattern, /[^ -~^|], or from the command line via grep, grep --color='auto' -Pnr "[\x80-\xFF]" *

Code Folding by Indent

Add the following to your vimrc:

"" Folding
set foldmethod=indent
set foldnestmax=2       " don't nest more than 2 folds
set foldlevel=10        " start with all folds open

The main commands you need are:

  • zr = increase the indent to fold
  • zm = decrease the indent to fold
  • za = open/close a folded block
  • zR = increase to the maximum fold level
  • zM = decrease to the minimum fold level
  • zA = recursively open/close a fold (and any folds contained therein)

Registers

Most register commands start with the character ". Vim has the concept of multiple types of registers. The types of registers are split up by their naming scheme. For example the numbered registers ([0-9]) are automatically filled as you make changes to a file and cleared when you open a new file.

The only registers you should routinely use for general use-cases are the 52 named registers.

The 52 named registers, defined by the reg-ex, "[a-zA-Z]", are only filled when you give the command to do so. If you use the lower case identifier then Vim will replace the contents of the register. With the upper case identifier Vim will append to the contents of the register.

:reg (:di does the samet thing)
Display the registers that are in use.
"xy{motion}
Yank into register "x". If you do this in visual mode you can exclude the "{motion}" section.
"xp
Put text from register "x" into the buffer. P can be substituded for p to put the text before the cursor instead of after.

It seems that it’s possible to clear a register using :let @a='' where "a" is the register you want to clear. This trick only sets the register to a null value; it does not remove the register from those listed by the :di command.

Tabs

Each tab page is just a new layout container to hold buffer windows.

  • :tabe: Open a new tab page. Equivallent to :e or :edit but using a new tab as the target.
  • :tabs: Show all open tabs.
  • :tabc: Close current tab page.
  • :tabo: Close all other tab pages.
  • :tabn, :tabp, gt, gT: Move to next/previous tab.
  • :tabm [N]: Move current tab to position N in the tabline.

Sorting Text

If you make a selection in visual mode then you can sort the lines therin using the command sort.

Misc. Tips

Change the behaviour of vim on long lines with:

:set wrap
:set nowrap

Plugin Notes

NERD Tree

The NERD Tree is a plugin get it at vim.org.

  • :NERDTreeToggle: Turn the NERDTree on/off for this tab.
    • This has been mapped to <F5> using map <F5> :NERDTreeToggle<CR> in my .vimrc file.
  • ?: show quick help
  • o: open selected object
  • t: open in new tab
  • i: open in new split
  • s: open in new vsplit
  • C: change the root of the tree
  • u: move the root up one directory (U leaves the current directory open too.)
  • cd: change CWD to the selected node
  • I: toggle hidden files
  • F: toggle whether files are displayed
  • B: toggle bookmarks table
  • A: toggle maximized/minimized NERD Tree window
" "