Yao Lirong's Blog

Introduction to Vim

2020/03/15
loading

First Steps in Vim

Editing

  • h j k l: huang he (W), Java (S), Kosovo (N), Los Angeles (E)
  • i: begin insertion at current cursor
  • a: begin insertion after current cursor
  • o: creates a new line after this line and begin insertion
  • O: opens a line above the cursor

Deleting Characters

  • x: delete one character forward dl
  • X: deletes one character backward dh
  • dd: delete a whole line
  • J: delete the line break in this line.

Undo and Redo

  • u: undo your last edit
  • ctrl-R: redo changes

Moving Around

Move by Words

a string of English characters “thisIsAWord” or symbols “^&%” is considered a word* in vim. A mix of both is not.

  • w: move to the start of next word (forward)
  • b: move to the start of last word (back)
  • e: move to the next end of a word (end)
  • ge: move to the previous end of a word

These lowercases command consider the appearance of different kinds of characters as separation of word. There uppercases version W B E gE consider only whitespaces to be delimiter.

Using Counts and Combination of Commands

You can add numbers before commands to perform this command multiple times. e.g. 3w moves you to the start of the third next word. 5gE moves you to the end of the fifth previous word delimited by whitespaces. 4a!<esc> appends ! four times after the position of our cursor.

  • dw: delete the word forward
  • d$: delete from the cursor to the end of the line
  • d2e: delete two words from the cursor to the end of the second end of word ahead

Search Characters

  • f<character>: move to the position of character’s first occurrence on right (forward)
  • F<character>: move to the position of character’s first occurrence on left (backward)

Move by Lines

  • <number>G: go to specific line in file
  • G: go to the end of the file
  • gg: go to the top of the file
  • <number0~100>%: go to some percentage of the file
  • H: go to the top line of what’s visible (home)
  • M: go to the middle part of what’s visible (middle)
  • L: go to the last line of what’s visible (last)

Scrolling

???

  • “``”: jumps back to the position you just came from (a jump is a move by lines)

Searching Words

  • /xxx search for xxx in our “file”
  • searching has history, you can use arrow key up and down to go through histories
  • *: use asterisk on a word to search this word in a file
  • match whole words
    • /the: matches “the”, “there”, “soothe”
    • /\<the: matches “the”, “there”
    • /the\> matches “the”, “soothe”
    • /\<the\> matches only “the”
  • You can also use regular expressions in searching

Making Small Changes

Changing Text

c works just like d except it leaves you in the editing mode after deletion.

  • c2wbe: deletes two words forward and then insert “be”
  • cc: changes (deletes and leaves you in editing mode) a whole line (as dd does)
  • c$: deletes to the end of the line and leaves user in editing mode

r<single character> replaces the character under our cursor with the <single character> we typed in. e.g. rw replaces the character with “w”.

Repeating a Change

. repeats the last change we made (edit of the file)

e.g. we want to delete all occurrences of “

1
2
3
4
5
6
f<   find first < 
df> delete to >
f< find next <
. repeat df>
f< find next <
. repeat df>

e.g. We want to change all occurrences of “four” to “five”

1
2
3
4
5
6
7
8
9


/four<Enter> find the first string "four"
cwfive<Esc> change the word to "five"
n find the next "four"
. repeat the change to "five'
n find the next "four"
. repeat the change
etc.

Visual Mode

其实就是一个可以选中文字的模式。

  • vllld selects three characters right to the cursor and deletes them.
  • V selects whole line (commands like hl move the cursor but don’t change the selected area)
  • Vjjd selects and deletes this line and two lines below it
  • o: gets you to the other side of selected area in visual mode

Moving Text

When you delete something with the d, x, or another command, the text is saved. You can paste it back by using the p command. (The Vim name for this is put).

  • p: puts the word deleted after the cursor
  • P: puts the word deleted before the cursor
  • xp: a combination of command to swap two characters if you typed them wrong (e.g. teh -> the)

Copying Text

  • y: copy selected word (use p to paste it)
  • yy: copy the whole line
  • Y: also copy the whole line (not like D, which deletes until the end of the line)

Clipboard

use the "* to use Vim clipboard

  • "*yy: copy a whole line to the clipboard
  • "*p: puts a line from the clipboard

Text Objects

  • daw: delete the word your cursor is at (aw: a word)
  • das: deletes a sentence, including any trailing whitespace (as: a sentence)
  • dis: deletes a sentence until the period, not including the trailing whitespaces (is: interior sentence)

Set Your Settings

Vimrc

find .vimrc by :scriptnames and add the following commands at the end of the file

  • :set incsearch: makes Vim display the match for the string while you are still typing it
  • :set ruler: This will display the cursor position in the lower right corner of the Vim window
  • :set autoindent: use the indent of previous line for a newly created line

Mapping

map a shorter combination of commands to a complicated function

  • :map <F5> i{<Esc>ea}<Esc>: insert curly brackets around the current word by pressing function key F5

One key commonly used in mapping is the backslash \ For example,

  • :map \p i(<Esc>ea)<Esc>: press \p to insert parenthesis
  • :map \c i{<Esc>ea}<Esc>: press \c to insert curly brackets

Plugin

Global Plugins

You can just add global plugins under ~/.vim/plugin/ directory and Vim will load them automatically when it starts. Don’t forget you can organize these plugins by putting them into different folders: ~/.vim/plugin/perl/ or ~/.vim/plugin/cpp/

Filetype Plugins

Start these plugins by :filetype plugin on

Filetype plugins are stored under ~/.vim/ftplugin/. When name your plugin, you have to name them according to the file type (extension names). e.g. ~/.vim/ftplugin/stuff.vim works only for file of type “stuff”. When you have two plugins for stuff and want to distinguish these two, you should use underscore to separate filetypes and the names of plugin. e.g. stuff_too.vim is an extension for filetype stuff with name “too”.

More Options

  • :set nowrap: let the text continue right after the window
  • :set <option>&: set the value of this option back to default e.g. :set iskeyword&
  • :set list: display tab as ^I, end of line as $
  • :set listchars=tab:>-,trail:-: set tabs to be displayed as >---, trailing whitespaces as ----.
  • :set iskeyword+=_: “_” now also becomes a part of keyword (e.g. stuff_abc is now a single word, w or b will not stop at the “a” when they move)
  • :set cmdheight=3: set the command line height at the bottom to be 3 (more space for command line)
CATALOG
  1. 1. First Steps in Vim
    1. 1.1. Editing
    2. 1.2. Deleting Characters
    3. 1.3. Undo and Redo
  2. 2. Moving Around
    1. 2.1. Move by Words
    2. 2.2. Using Counts and Combination of Commands
    3. 2.3. Search Characters
    4. 2.4. Move by Lines
    5. 2.5. Scrolling
    6. 2.6. Searching Words
  3. 3. Making Small Changes
    1. 3.1. Changing Text
    2. 3.2. Repeating a Change
    3. 3.3. Visual Mode
    4. 3.4. Moving Text
    5. 3.5. Copying Text
    6. 3.6. Clipboard
    7. 3.7. Text Objects
  4. 4. Set Your Settings
    1. 4.1. Vimrc
    2. 4.2. Mapping
    3. 4.3. Plugin
      1. 4.3.1. Global Plugins
      2. 4.3.2. Filetype Plugins
    4. 4.4. More Options