Created: Jan, 2012
Last modified: Feb, 2016
A tiny subset of VIM commands I find very useful…
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.
42G
42gg
:42<CR>
w
; move forward by 3 words: 3w
.b
; move backward by 3 words: 3b
.0
; move to the non-blank character of the line: ‘^’.)
; move backward by one sentence: (
.}
; move backward by one paragraph: {
.Move to the top of screen: H
; middle of screen: M
; and bottom of screen: L
u
; redo: Ctrl+r
.This page summarizes it well.
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.
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.
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
:! g++ %
which will compile the current file with GCC C++ compiler.
: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.
Press Ctrl+v
to enter visual block mode. It is amazing. See this tutorial for a brief introduction.