First Steps in Vim
Editing
h j k l
: huang he (W), Java (S), Kosovo (N), Los Angeles (E)i
: begin insertion at current cursora
: begin insertion after current cursoro
: creates a new line after this line and begin insertionO
: opens a line above the cursor
Deleting Characters
x
: delete one character forwarddl
X
: deletes one character backwarddh
dd
: delete a whole lineJ
: delete the line break in this line.
Undo and Redo
u
: undo your last editctrl-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 forwardd$
: delete from the cursor to the end of the lined2e
: delete two words from the cursor to the end of the second end of word ahead
Search Characters
f<character>
: move to the position ofcharacter
’s first occurrence on right (forward)F<character>
: move to the position ofcharacter
’s first occurrence on left (backward)
Move by Lines
<number>G
: go to specific line in fileG
: go to the end of the filegg
: go to the top of the file<number0~100>%
: go to some percentage of the fileH
: 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 forxxx
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 (asdd
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 | f< find first < |
e.g. We want to change all occurrences of “four” to “five”
1 |
|
Visual Mode
其实就是一个可以选中文字的模式。
vllld
selects three characters right to the cursor and deletes them.V
selects whole line (commands likehl
move the cursor but don’t change the selected area)Vjjd
selects and deletes this line and two lines below ito
: 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 cursorP
: puts the word deleted before the cursorxp
: a combination of command to swap two characters if you typed them wrong (e.g. teh -> the)
Copying Text
y
: copy selected word (usep
to paste it)yy
: copy the whole lineY
: also copy the whole line (not likeD
, 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 keyF5
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
orb
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)