VIMTUTOR
Lesson 1 SUMMARY
-
The cursor is moved using either the arrow keys or the hjkl keys.
h (left) j (down) k (up) l (right) -
To start Vim from the shell prompt type: vim FILENAME <ENTER>
-
To exit Vim type: <ESC> :q! <ENTER> to trash all changes.
OR type: <ESC> :wq <ENTER> to save the changes. -
To delete the character at the cursor type: x
-
To insert or append text type:
i type inserted text <ESC> insert before the cursor
A type appended text <ESC> append after the line
NOTE: Pressing <ESC> will place you in Normal mode or will cancel
an unwanted and partially completed command.
Lesson 2 SUMMARY
-
To delete from the cursor up to the next word type: dw
-
To delete from the cursor to the end of a line type: d$
-
To delete a whole line type: dd
-
To repeat a motion prepend it with a number: 2w
-
The format for a change command is:
operator [number] motion
where:
operator - is what to do, such as d for delete
[number] - is an optional count to repeat the motion
motion - moves over the text to operate on, such as w (word),
$ (to the end of line), etc. -
To move to the start of the line use a zero: 0
-
To undo previous actions, type: u (lowercase u)
To undo all the changes on a line, type: U (capital U)
To undo the undo’s, type: CTRL-R
Lesson 3 SUMMARY
-
To put back text that has just been deleted, type p . This puts the
deleted text AFTER the cursor (if a line was deleted it will go on the
line below the cursor). -
To replace the character under the cursor, type r and then the
character you want to have there. -
The change operator allows you to change from the cursor to where the
motion takes you. eg. Type ce to change from the cursor to the end of
the word, c$ to change to the end of a line. -
The format for change is:
c [number] motion
Lesson 4 SUMMARY
-
CTRL-G displays your location in the file and the file status.
G moves to the end of the file.
number G moves to that line number.
gg moves to the first line. -
Typing / followed by a phrase searches FORWARD for the phrase.
Typing ? followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or N to search in the opposite direction.
CTRL-O takes you back to older positions, CTRL-I to newer positions. -
Typing % while the cursor is on a (,),[,],{, or } goes to its match.
-
To substitute new for the first old in a line type :s/old/new
To substitute new for all 'old’s on a line type :s/old/new/g
To substitute phrases between two line #'s type :#,#s/old/new/g
To substitute all occurrences in the file type :%s/old/new/g
To ask for confirmation each time add ‘c’ :%s/old/new/gc
Lesson 5 SUMMARY
-
:!command executes an external command.
Some useful examples are:
(MS-DOS) (Unix)
:!dir :!ls - shows a directory listing.
:!del FILENAME :!rm FILENAME - removes file FILENAME. -
:w FILENAME writes the current Vim file to disk with name FILENAME.
-
v motion :w FILENAME saves the Visually selected lines in file
FILENAME. -
:r FILENAME retrieves disk file FILENAME and puts it below the
cursor position. -
:r !dir reads the output of the dir command and puts it below the
cursor position.
Lesson 6 SUMMARY
-
Type o to open a line BELOW the cursor and start Insert mode.
Type O to open a line ABOVE the cursor. -
Type a to insert text AFTER the cursor.
Type A to insert text after the end of the line. -
The e command moves to the end of a word.
-
The y operator yanks (copies) text, p puts (pastes) it.
-
Typing a capital R enters Replace mode until <ESC> is pressed.
-
Typing “:set xxx” sets the option “xxx”. Some options are:
‘ic’ ‘ignorecase’ ignore upper/lower case when searching
‘is’ ‘incsearch’ show partial matches for a search phrase
‘hls’ ‘hlsearch’ highlight all matching phrases
You can either use the long or the short option name. -
Prepend “no” to switch an option off: :set noic
Lesson 7 SUMMARY
-
Type :help or press <F1> or <Help> to open a help window.
-
Type :help cmd to find help on cmd .
-
Type CTRL-W CTRL-W to jump to another window
-
Type :q to close the help window
-
Create a vimrc startup script to keep your preferred settings.
-
When typing a : command, press CTRL-D to see possible completions.
Press <TAB> to use one completion.
VIMRC
" Comments in Vimscript start with a `"`.
" If you open this file in Vim, it’ll be syntax highlighted for you.
" Vim is based on Vi. Setting `nocompatible` switches from the default
" Vi-compatibility mode and enables useful Vim functionality. This
" configuration option turns out not to be necessary for the file named
" ‘~/.vimrc’, because Vim automatically enters nocompatible mode if that file
" is present. But we’re including it here just in case this config file is
" loaded some other way (e.g. saved as `foo`, and then Vim started with
" `vim -u foo`).
set nocompatible
" Turn on syntax highlighting.
syntax on
" Disable the default Vim startup message.
set shortmess+=I
" Show line numbers.
set number
" This enables relative line numbering mode. With both number and
" relativenumber enabled, the current line shows the true line number, while
" all other lines (above and below) are numbered relative to the current line.
" This is useful because you can tell, at a glance, what count is needed to
" jump up or down to a particular line, by {count}k to go up or {count}j to go
" down.
set relativenumber
" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2
" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can’t backspace before the insertion point set with ‘i’.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start
" By default, Vim doesn’t let you hide a buffer (i.e. have a buffer that isn’t
" shown in any window) that has unsaved changes. This is to prevent you from "
" forgetting about unsaved changes and then quitting e.g. via :qa!. We find
" hidden buffers helpful enough to disable this protection. See :help hidden
" for more information on this.
set hidden
" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase
" Enable searching as you type, rather than waiting till you press enter.
set incsearch
" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " ‘Q’ in normal mode enters Ex\ mode. You almost never want this.
" Disable audible bell because it’s annoying.
set noerrorbells visualbell t_vb=
" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a
" Try to prevent bad habits like using the arrow keys for movement. This is
" not the only possible bad habit. For example, holding down the h/j/k/l keys
" for movement, rather than using more efficient movement commands, is also a
" bad habit. The former is enforceable through a .vimrc, while we don’t know
" how to prevent the latter.
" Do this in normal mode…
nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>
" …and in insert mode
inoremap <Left> <ESC>:echoe "Use h"<CR>
inoremap <Right> <ESC>:echoe "Use l"<CR>
inoremap <Up> <ESC>:echoe "Use k"<CR>
inoremap <Down> <ESC>:echoe "Use j"<CR>
Vim进阶
搜索和替换
:s(替换命令)
%s/foo/bar/g
- 在整个文件中将foo全局替换成bar%s/\[.*\](\(.*\))/\1/g
- 将有命名的链接替换成简单
多窗口
- 用
:sp/:vsp来分割窗口 - 同一个缓存可以在多个窗口中显示
- 用
<c-w>+(hjkl)切换窗口
宏
q{字符}来开始在寄存器{字符}中录制宏q停止录制@{字符}重放宏- 宏的执行错误会停止
{计数}@{字符}执行一个宏{计数}次- 宏可以递归
- 首先用
q{字符}q清除宏 - 录制该宏,用
@{字符}来递归调用该宏(在录制完成之前不会有任何操作)
- 首先用