Contents

Practical VIM

Practical VIM Tips

Normal Mode

Vim has two model: Normal model and Insert model. Most other text editors resemble Insert model.

Chunk Your Undos

Vim use u and <CR> to undo and recover command. In vim, undo command has control granularity. The granularity unit is one change. For example: from Insert model to Normal mode, everything we type as a change. In Normal mode, everything we operation as a change.

So the key is control your chunk. <ESC> at the end of a sentence, consider what you will write next. Then press A to continue writing.

Compose Repeatable Changes

Vim is optimized for repetition. so choose a more repeatable command. For example: delete a word can use daw command and repeat . command to delete another words.

operator + motion = action

Much of Vim’s power stems from the way that operators and motions can be combined.

Vim’s Operators Commands:

TriggerEffect
cChange
dDelete
yYank into register
g~Swap case
guMake lowercase
gUMake uppercase
>Shift right
<Shift left
=Autoindent
!Filter {motion} lines through an external program

Vim’s Motions documentation

Insert Mode

Make Corrections Instantly from Insert Mode

When we making a typing error, we want to fix it immediately in Insert Model. We could switch to Normal mode and do some operation to modify the word.

Here are some keystrokes to delete errors instantly.

KeystrokesEffect
<C-h>Delete back one character (backspace)
<C-w>Delete back one word
<C-u>Delete back to start of line

Get Back to Normal Mode

Here are some alternative keystrokes to get back to Normal mode

KeystrokesEffect
<Esc>Switch to Normal mode
<C-[>Switch to Normal mode
<C-o>Switch to Insert Normal mode

Insert Normal mode is a special version of Normal mode, we can fire off a single command, after which we’ll be returned to Insert mode immediately.

For example, type <C-o>zz in Insert mode will redraw the screen with current line in the middle of the window.

Paste from a Register Without Leaving Insert Mode

Sometimes, we want to use yank without leaving Insert mode, use <C-r>{register} can paste words from Insert mode.

Visual Mode

Visual mode allows us to define a selection of text and then operate upon it.

Vim has three kinds of Visual mode: character-wise, line-wise and block-wise.

CommandEffect
vcharacter-wise Visual mode
Vline-wise Visual mode
<C-v>block-wise Visual mode
gvReselect the last visual selection
oGo to other end of highlighted text

In visual mode(<C-v>), We can use A or I to inset some text. This will drops us into Insert Mode on the topmost line of our selection. Anything that we type will appear on this line only for the duration of Insert mode, but as soon as we revert to Normal mode, our changes are propagated across the rest of the lines that we selected.

Files

buffer

Vim lets us work on multiple files at the same time. The buffer list lets us keep track of the set of files that we’ve opened in the course of an editing session.

vim *.txt will open multiple files in one editing session.

:ls will list all buffers that have been loaded into memory.

:bnext will switch to the next buffer. some similar keys :bprevious :bfirst :blast

:b {buffername} will switch to specified buffer directly.

:bdelete {buffernumber} will remove the buffer in memory, but has no effect on its associated file.

window

Vim allows us to view multiple buffers side by side by dividing our workspace into split windows.

Split window:

CommandEffect
<C-w>ssplit horizontally
<C-w>vsplit vertically

Changing window:

CommandEffect
<C-w>wCycle between open windows
<C-w>h/j/k/lleft/below/above/right

Closing window:

CommandEffect
<C-w>cClose the active window
<C-w>oKeep only the active window, closing all others

tab

Vim’s tabbed interface is different from that of many other text editors. We can use tab pages to organize split windows into a collection of workspaces.

opening and closing tabs:

CommandEffect
:tabe[dit] {filename}Open {filename} in a new tab
<C-w>TMove the current window into its own tab
:tabc[lose]Close the current tab page and all of its windows
:tabo[nly]Keep the active tab page, closing all others

switching between tabs:

CommandEffect
{N}gtSwitch to tab page number {N}
gtSwitch to the next tab page
gTSwitch to the previous tab page

open Files

:edit command allows us to open files from within Vim, either by specifying an absolute or a relative filepath.

:find command allows us to open a file by its name without having to provide a fully qualified path.

sometimes, we don not have permission to write a file. we can use :w !sudo tee % > /dev/null

Text Object selection

This is a series of commands that can only be used while in Visual mode or after an operator.

This command can select a word / a sentence / a text in [] …

Text object range only have two modes: i (inner) and a (arround)

CommandEffect
commands that start with “a”select object including white space
commands that start with “i”select object without white space
  • iw …inner word
  • aw …a word
  • iW …inner WORD
  • aW …a WORD
  • is …inner sentence
  • as …a sentence
  • ip …inner paragraph
  • ap …a paragraph
  • it …inner tag
  • at …a tag
  • i( or i) …inner block
  • a( or a) …a block
  • i< or i> …inner block
  • a< or a> …a block
  • i{ or i} …inner block
  • a{ or a} …a block
  • i[ or i] …inner block
  • a[ or a] …a block
  • i" …inner block
  • a" …a block
  • i` …inner block
  • a` …a block

Mark Place

The m{a-zA-Z} command marks the current cursor location.

The ``{mark}`command moves to the line where a mark was set.

:marks will show all marks and asoociated files.

Vim has automatic marks:

KeystrokesBuffer Contents
Position before the last jump within current file
‘.Location of last change
‘^Location of last insertion
‘[Start of last change or yank
‘]End of last change or yank
‘<Start of last visual selection
‘>End of last visual selection

Jump

Jump between matching parentheses

The % command lets us jump between opening and closing sets of parentheses. It works with (), {}, and [].

Traverse the Jump List

Vim records our location before and after making a jump and provides a couple of commands for retracing our steps.

<C-o>command is like the back button

<C-i>command is like the forward button

:jumps will show a list that can we can use <C-o> <C-i> to jump

Traverse the Change List

g; traverse backward

g, traverse forward

Registers

Vim’s registers are simply containers that hold text. They can be used in the manner of a clipboard for cutting, copying, and pasting text, or they can be used to record a macro by saving a sequence of keystrokes.

Delete, Yank, and Put with Vim’s Unnamed Register

Normally when we discuss cut, copy, and paste, we talk about putting text on a clipboard. In Vim’s terminology, we don’t deal with a clipboard but instead with registers. we can specify which ones we want to use. If wen don’t specify a register, then vim will use the unnamed register.

Specify register command prefix: "{register} example: "add

Other registers

1.THE YANK REGISTER (“0)

When we use the y{motion} command, the specified text is copied not only into the unnamed register but also into the yank register, which is addressed by the 0 symbol.

2.THE NAMED REGISTERS (“A–“Z)

Vim has one named register for each letter of the alphabet. Named registers can become really useful when we’ve got one or more pieces of text that we want to paste in several places.

3.THE BLACK HOLE REGISTER ("_)

The black hole register is a place from which nothing returns. This can be useful if we want to delete text without overwriting the contents of the unnamed register.

4.THE SYSTEM CLIPBOARD (“+) AND SELECTION ("*) REGISTERS

All of the registers that we’ve discussed so far are internal to Vim. If we want to copy some text from inside of Vim and paste it into an external program, then we have to use one of the system clipboards.

Vim’s plus register references the system clipboard and is addressed by the + symbol.

The X11 windowing system has a second kind of clipboard called the primary. This represents the most recently selected text, and we can use the middle mouse button (if we have one) to paste from it. Vim’s quotestar register maps to the primary clipboard and is addressed by the * symbol.

In Windows and Mac OS X, there is no primary clipboard, so we can use the “+ and “* registers interchangeably: they both represent the system clipboard.

Vim can be compiled with or without support for X11 clipboard integration. To find out whether your version of Vim has the feature enabled, run the :version command and look for xterm_clipboard. If it’s prefixed with a minus sign, then your version of Vim does not support this feature. A plus sign means that the feature is available.

5.THE EXPRESSION REGISTER (“=)

When we inpu “=, vim will drop into command-line mode, showing an = promot, we can input vim script and run it, the register will store expression result.

6.MORE REGISTERS

RegisterContents
"%Name of the current file
"#Name of the alternate file
".Last inserted text
":Last Ex command
"/Last search pattern

Paste from register

The p command puts the text from a register after the cursor position. The P command puts the text from a register before the cursor position.

From insert mode, we can use <C-r>{register} to put text before the cursor position.

Macros

Macros allow us to record a sequence of changes and then play them back.

  • start recording: q + {register}. example: qa
  • stop recording: q

  • replay: @ + {register}. example: @a
  • replay most recently: @@

  • repeatable replay: 1000@a

  • replay by line: :’<,’>normal @a