about // doc

Vim Notes

Created: Jan, 2012

Last modified: Feb, 2016

A tiny subset of VIM commands I find very useful…

Windows

  • Split window vertically: “C-w v”
  • Split window horizontally: “C-w s”
  • Switch among windows: “C-w w”
  • Resize the current window horizontally: “:resize 60” (resize to 60 rows); “:resize +5” (increase by 5 columns); can also use “res” instead of “resize” for abbreviation.
  • Resize the current window vertically: everything is the same to horizontal resizing by replacing “resize” with “vertical resize”.
  • Use file explorer netrw

General

  • Insert characters in multiple lines (such as inserting space at the head of multiple lines): see this post

  • How to use Vundle to manage VIM plugins.

  • Open multiple files in new tabs or split windows and navigate: this can be handy when working on multiple files.

  • Go to line: (at least) 3 different ways
    • 42G
    • 42gg
    • :42<CR>
  • Move forward by one word: w; move forward by 3 words: 3w.
  • Move backward by one word: b; move backward by 3 words: 3b.
  • Move to the beginning of the line: 0; move to the non-blank character of the line: ‘^’.
  • Move forward by one sentence: ); move backward by one sentence: (.
  • Move forward by one paragraph: }; move backward by one paragraph: {.
  • Move to the top of screen: H; middle of screen: M; and bottom of screen: L

  • Undo: u; redo: Ctrl+r.

Using marks

This page summarizes it well.

Indentation Settings

Take care of indentation of C/C++ code (this link), and generally the indentation in Vim.

Put the following lines to “~/.vimrc”:

set autoindent
set shiftwidth=4
set softtabstop=4
set expandtab

Here “autoindent” does no more than indenting following the previous line. For C/C++ files one may explore “cindent” option.

To disable autoindentation when pasting text, do this in normal mode:

:set paste

After done with pasting, do this to re-enable autoindentation with

:set nopaste

Another trick is to set a key shortcut to toggle this “paste action” by adding this line to ‘~/.vimrc’:

set pastetoggle=<F10>

which will associate key F10 with this toggling behavior.

Reference here.

Color

By default, the comments are lightlighted with dark blue in a black background, which is hard to see in a terminal. I find using “desert” color theme in a black ground works better. Add this line to “~/.vimrc”:

colors desert

See some discussions here and here.

Search & Replace

To search and replace A with B in the current line

:s/A/B/g

To search and replace A with B globally (all lines)

:%s/A/B/g

To search and replace A with B globally with confirmation of each match

:%s/A/B/gc

Command-line interaction

  • To run a shell command with the file being edited like
:! g++ %

which will compile the current file with GCC C++ compiler.

  • To run a command in certain working directory, first check the working directory with
:pwd

To change the working directory to the directory the file being editing is in, run

:cd %:p:h

Explanation of the above command: % gives the name of the current file, %:p gives its full path, and %:p:h gives its directory (the “head” of the full path).

For more information on this topic, see this post.

Visual block mode

Press Ctrl+v to enter visual block mode. It is amazing. See this tutorial for a brief introduction.

comments powered by Disqus