The Vim commands cheat sheet (ubuntu) vi编辑器常用的命令及快捷键

How to Exit

:q[uit] Quit Vim. This fails when changes have been made.
:q[uit]! Quit without writing.
:cq[uit] Quit always, without writing.
:wq Write the current file and exit.
:wq! Write the current file and exit always.
:wq {file} Write to {file}. Exit if not editing the last
:wq! {file} Write to {file} and exit always.
:[range]wq[!] [file] Same as above, but only write the lines in [range].
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as ":q!").

Editing a File

:e[dit] Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim.
:e[dit]! Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.
:e[dit] {file} Edit {file}.
:e[dit]! {file} Edit {file} always. Discard any changes to the current buffer.
gf Edit the file whose name is under or after the cursor. Mnemonic: "goto file".

Inserting Text

a Append text after the cursor [count] times.
A Append text at the end of the line [count] times.
i Insert text before the cursor [count] times.
I Insert text before the first non-blank in the line [count] times.
gI Insert text in column 1 [count] times.
o Begin a new line below the cursor and insert text, repeat [count] times.
O Begin a new line above the cursor and insert text, repeat [count] times.

Inserting a file

:r[ead] [name] Insert the file [name] below the cursor.
:r[ead] !{cmd} Execute {cmd} and insert its standard output below the cursor.

Deleting Text

<Del> or
x
Delete [count] characters under and after the cursor
X Delete [count] characters before the cursor
d{motion} Delete text that {motion} moves over
dd Delete [count] lines
D Delete the characters under the cursor until the end of the line
{Visual}x or
{Visual}d
Delete the highlighted text (for {Visual} see Selecting Text ).
{Visual}CTRL-H or
{Visual}
When in Select mode: Delete the highlighted text
{Visual}X or
{Visual}D
Delete the highlighted lines
:[range]d[elete] Delete [range] lines (default: current line)
:[range]d[elete] {count} Delete {count} lines, starting with [range]

Changing (or Replacing) Text

r{char} replace the character under the cursor with {char}.
R Enter Insert mode, replacing characters rather than inserting
~ Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters.
~{motion} switch case of {motion} text.
{Visual}~ Switch case of highlighted text

Substituting

:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count] For each line in [range] replace a match of {pattern} with {string}.
:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count] Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add extra flags
The arguments that you can use for the substitute commands:
[c]  Confirm each substitution.  Vim positions the cursor on the matching
  string.  You can type:
      'y'      to substitute this match
      'n'      to skip this match
         to skip this match
      'a'      to substitute this and all remaining matches {not in Vi}
      'q'      to quit substituting {not in Vi}
      CTRL-E  to scroll the screen up {not in Vi}
      CTRL-Y  to scroll the screen down {not in Vi}.
[e]     When the search pattern fails, do not issue an error message and, in
  particular, continue in maps as if no error occurred.  
[g]  Replace all occurrences in the line.  Without this argument,
  replacement occurs only for the first occurrence in each line.
[i]  Ignore case for the pattern.  
[I]  Don't ignore case for the pattern.  
[p]  Print the line containing the last substitute.

Copying and Moving Text

"{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put).
:reg[isters] Display the contents of all numbered and named registers.
:reg[isters] {arg} Display the contents of the numbered and named registers that are mentioned in {arg}.
:di[splay] [arg] Same as :registers.
["x]y{motion} Yank {motion} text [into register x].
["x]yy Yank [count] lines [into register x]
["x]Y yank [count] lines [into register x] (synonym for yy).
{Visual}["x]y Yank the highlighted text [into register x] (for {Visual} see Selecting Text ).
{Visual}["x]Y Yank the highlighted lines [into register x]
:[range]y[ank] [x] Yank [range] lines [into register x].
:[range]y[ank] [x] {count} Yank {count} lines, starting with last line number in [range] (default: current line), [into register x].
["x]p Put the text [from register x] after the cursor [count] times.
["x]P Put the text [from register x] before the cursor [count] times.
["x]gp Just like "p", but leave the cursor just after the new text.
["x]gP Just like "P", but leave the cursor just after the new text.
:[line]pu[t] [x] Put the text [from register x] after [line] (default current line).
:[line]pu[t]! [x] Put the text [from register x] before [line] (default current line).

Undo/Redo/Repeat

u Undo [count] changes.
:u[ndo] Undo one change.
CTRL-R Redo [count] changes which were undone.
:red[o] Redo one change which was undone.
U Undo all latest changes on one line. {Vi: while not moved off of it}
. Repeat last change, with count replaced with [count].

Moving Around

Basic motion commands:

        k              
      h   l          
        j             
h or [count] characters to the left (exclusive).
l or
or
[count] characters to the right (exclusive).
k or
or
CTRL-P
[count] lines upward
j or
or
CTRL-J or
or
CTRL-N
[count] lines downward (linewise).
0 To the first character of the line (exclusive).
<Home> To the first character of the line (exclusive).
^ To the first non-blank character of the line
$ or
<End>
To the end of the line and [count - 1] lines downward
g0 or
g<Home>
When lines wrap ('wrap on): To the first character of the screen line (exclusive). Differs from "0" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost character of the current line that is on the screen. Differs from "0" when the first character of the line is not on the screen.
g^ When lines wrap ('wrap' on): To the first non-blank character of the screen line (exclusive). Differs from "^" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost non-blank character of the current line that is on the screen. Differs from "^" when the first non-blank character of the line is not on the screen.
g$ or
g<End&gr;
When lines wrap ('wrap' on): To the last character of the screen line and [count - 1] screen lines downward (inclusive). Differs from "$" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the rightmost character of the current line that is visible on the screen. Differs from "$" when the last character of the line is not on the screen or when a count is used.
f{char} To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive).
F{char} To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive).
t{char} Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive).
T{char} Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive).
; Repeat latest f, t, F or T [count] times.
, Repeat latest f, t, F or T in opposite direction [count] times.
- <minus> [count] lines upward, on the first non-blank character (linewise).
+ or
CTRL-M or
<CR>
[count] lines downward, on the first non-blank character (linewise).
_ <underscore> [count] - 1 lines downward, on the first non-blank character (linewise).
<C-End> or
G
Goto line [count], default last line, on the first non-blank character.
<C-Home> or
gg
Goto line [count], default first line, on the first non-blank character.
<S-Right> or
w
[count] words forward
<C-Right> or
W
[count] WORDS forward
e Forward to the end of word [count]
E Forward to the end of WORD [count]
<S-Left> or
b
[count] words backward
<C-Left> or
B
[count] WORDS backward
ge Backward to the end of word [count]
gE Backward to the end of WORD [count]

These commands move over words or WORDS.

A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, ). This can be changed with the 'iskeyword' option.

A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a word and a WORD.

( [count] sentences backward
) [count] sentences forward
{ [count] paragraphs backward
} [count] paragraphs forward
]] [count] sections forward or to the next '{' in the first column. When used after an operator, then the '}' in the first column.
][ [count] sections forward or to the next '}' in the first column
[[ [count] sections backward or to the previous '{' in the first column
[] [count] sections backward or to the previous '}' in the first column

Marks

m{a-zA-Z} Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command).
m' or
m`
Set the previous context mark. This can be jumped to with the "''" or "``" command (does not move the cursor, this is not a motion command).
:[range]ma[rk] {a-zA-Z} Set mark {a-zA-Z} at last line number in [range], column 0. Default is cursor line.
:[range]k{a-zA-Z} Same as :mark, but the space before the mark name can be omitted.
'{a-z} To the first non-blank character on the line with mark {a-z} (linewise).
'{A-Z0-9} To the first non-blank character on the line with mark {A-Z0-9} in the correct file
`{a-z} To the mark {a-z}
`{A-Z0-9} To the mark {A-Z0-9} in the correct file
:marks List all the current marks (not a motion command).
:marks {arg} List the marks that are mentioned in {arg} (not a motion command). For example:

Searching

/{pattern}[/] Search forward for the [count]'th occurrence of {pattern}
/{pattern}/{offset} Search forward for the [count]'th occurrence of {pattern} and go {offset} lines up or down.
/<CR> Search forward for the [count]'th latest used pattern
//{offset}<CR> Search forward for the [count]'th latest used pattern with new. If {offset} is empty no offset is used.
?{pattern}[?]<CR> Search backward for the [count]'th previous occurrence of {pattern}
?{pattern}?{offset}<CR> Search backward for the [count]'th previous occurrence of {pattern} and go {offset} lines up or down
?<CR> Search backward for the [count]'th latest used pattern
??{offset}<CR> Search backward for the [count]'th latest used pattern with new {offset}. If {offset} is empty no offset is used.
n Repeat the latest "/" or "?" [count] times.
N Repeat the latest "/" or "?" [count] times in opposite direction.

Selecting Text (Visual Mode)

To select text, enter visual mode with one of the commands below, and use motion commands to highlight the text you are interested in. Then, use some command on the text.

The operators that can be used are:
  ~  switch case
  d  delete
  c  change
  y  yank
  >  shift right 
  <  shift left 
  !  filter through external command 
  =  filter through 'equalprg' option command 
  gq  format lines to 'textwidth' length 
v start Visual mode per character.
V start Visual mode linewise.
<Esc> exit Visual mode without making any changes

How to Suspend

CTRL-Z Suspend Vim, like ":stop". Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character.
:sus[pend][!] or
:st[op][!]
Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later!

reference:

1. http://www.tuxfiles.org/linuxhelp/vimcheat.html

2. http://www.fprintf.net/vimCheatSheet.html

3.  http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know

vi编辑器常用的命令及快捷键

光标控制命令 

命令                        光标移动
h或^h                    向左移一个字符
j或^j或^n                向下移一行
k或^p                    向上移一行
l或空格                     向右移一个字符
G                        移到文件的最后一行
nG                       移到文件的第n行
w                        移到下一个字的开头
W                        移到下一个字的开头,忽略标点符号
b                        移到前一个字的开头
B                        移到前一个字的开头,忽略标点符号
L                        移到屏幕的最后一行
M                        移到屏幕的中间一行
H                        移到屏幕的第一行
e                        移到下一个字的结尾
E                        移到下一个字的结尾,忽略标点符号
(                        移到句子的开头
)                        移到句子的结尾
{                        移到段落的开头
}                        移到下一个段落的开头
0或|                     移到当前行的第一列
n|                       移到当前行的第n列
^                        移到当前行的第一个非空字符
$                        移到当前行的最后一个字符
+或return                移到下一行的第一个字符
-                        移到前一行的第一个非空字符

在vi中添加文本

命令                         插入动作
a                        在光标后插入文本
A                        在当前行插入文本
i                        在光标前插入文本
I                        在当前行前插入文本
o                        在当前行的下边插入新行
O                        在当前行的上边插入新行
:r file                  读入文件file内容,并插在当前行后
:nr file                 读入文件file内容,并插在第n行后
escape                   回到命令模式
^v char                  插入时忽略char的指定意义,这是为了插入特殊字符

在vi中删除文本

命令                         删除操作
x                        删除光标处的字符,可以在x前加上需要删除的字符数目
nx                       从当前光标处往后删除n个字符
X                        删除光标前的字符,可以在X前加上需要删除的字符数目
nX                       从当前光标处往前删除n个字符
dw                       删至下一个字的开头
ndw                      从当前光标处往后删除n个字
dG                       删除行,直到文件结束
dd                       删除整行
ndd                      从当前行开始往后删除
db                       删除光标前面的字
ndb                      从当前行开始往前删除n字
:n,md                    从第m行开始往前删除n行
d或d$                    从光标处删除到行尾
dcursor_command          删除至光标命令处,如dG将从当产胆行删除至文件的末尾
^h或backspace            插入时,删除前面的字符
^w                       插入时,删除前面的字

修改vi文本

每个命令前面的数字表示该命令重复的次数
命令                         替换操作
rchar                    用char替换当前字符
R text escape            用text替换当前字符直到换下Esc键
stext escape             用text代替当前字符
S或cctext escape         用text代替整行
cwtext escape            将当前字改为text
Ctext escape             将当前行余下的改为text
cG escape                修改至文件的末尾
ccursor_cmd text escape  从当前位置处到光标命令位置处都改为text

在vi中查找与替换

命令                         查找与替换操作
/text                    在文件中向前查找text
?text                    在文件中向后查找text
n                        在同一方向重复查找
N                        在相反方向重复查找
ftext                    在当前行向前查找text
Ftext                    在当前行向后查找text
ttext                    在当前行向前查找text,并将光标定位在text的第一个字符
Ttext                    在当前行向后查找text,并将光标定位在text的第一个字符
:set ic                  查找时忽略大小写
:set noic                查找时对大小写敏感
:s/oldtext/newtext       用newtext替换oldtext
:m,ns/oldtext/newtext    在m行通过n,用newtext替换oldtext
&                        重复最后的:s命令
:g/text1/s/text2/text3   查找包含text1的行,用text3替换text2
:g/text/command          在所有包含text的行运行command所表示的命令
:v/text/command          在所有不包含text的行运行command所表示的命令

在vi中复制文本

命令                         复制操作
yy                       将当前行的内容放入临时缓冲区
nyy                      将n行的内容放入临时缓冲区
p                        将临时缓冲区中的文本放入光标后
P                        将临时缓冲区中的文本放入光标前
dsfsd "(a-z)nyy          复制n行放入名字为圆括号内的可命名缓冲区,省略n表示当前行
"(a-z)ndd                删除n行放入名字为圆括号内的可命名缓冲区,省略n表示当前行
"(a-z)p                  将名字为圆括号的可命名缓冲区的内容放入当前行后
"(a-z)P                  将名字为圆括号的可命名缓冲区的内容放入当前行前

在vi中撤消与重复

命令                         撤消操作
u                        撤消最后一次修改
U                        撤消当前行的所有修改
.                        重复最后一次修改
,                        以相反的方向重复前面的f、F、t或T查找命令
;                        重复前面的f、F、t或T查找命令
"np                      取回最后第n次的删除(缓冲区中存有一定次数的删除内容,一般为9)
n                        重复前面的/或?查找命令
N                        以相反方向重复前面的/或?命令

保存文本和退出vi

命令                         保存和/或退出操作
:w                       保存文件但不退出vi
:w file                  将修改保存在file中但不退出vi
:wq或ZZ或:x              保存文件并退出vi
:q!                      不保存文件,退出vi
:e!                      放弃所有修改,从上次保存文件开始再编辑

vi中的选项

选项                         作用
:set all                 打印所有选项
:set nooption            关闭option选项
:set nu                  每行前打印行号
:set showmode            显示是输入模式还是替换模式
:set noic                查找时忽略大小写
:set list                显示制表符(^I)和行尾符号
:set ts=8                为文本输入设置tab stops
:set window=n            设置文本窗口显示n行

vi的状态

选项                         作用
:.=                      打印当前行的行号
:=                       打印文件中的行数
^g                       显示文件名、当前的行号、文件的总行数和文件位置的百分比
:l                       使用字母"l"来显示许多的特殊字符,如制表符和换行符

在文本中定位段落和放置标记

选项                         作用
{                        在第一列插入{来定义一个段落
[[                       回到段落的开头处
]]                       向前移到下一个段落的开头处
m(a-z)                   用一个字母来标记当前位置,如用mz表示标记z
'(a-z)                   将光标移动到指定的标记,如用'z表示移动到z

在vi中连接行

选项                         作用
J                        将下一行连接到当前行的末尾
nJ                       连接后面n行

光标放置与屏幕调整

选项                         作用
H                        将光标移动到屏幕的顶行
nH                       将光标移动到屏幕顶行下的第n行
M                        将光标移动到屏幕的中间
L                        将光标移动到屏幕的底行
nL                       将光标移动到屏幕底行上的第n行
^e(ctrl+e)               将屏幕上滚一行
^y                       将屏幕下滚一行
^u                       将屏幕上滚半页
^d                       将屏幕下滚半页
^b                       将屏幕上滚一页
^f                       将屏幕下滚一页
^l                       重绘屏幕
z-return                 将当前行置为屏幕的顶行
nz-return                将当前行下的第n行置为屏幕的顶行
z.                       将当前行置为屏幕的中央
nz.                      将当前行上的第n行置为屏幕的中央
z-                       将当前行置为屏幕的底行
nz-                      将当前行上的第n行置为屏幕的底行

vi中的shell转义命令

选项                         作用
:!command                执行shell的command命令,如:!ls
:!!                      执行前一个shell命令
:r!command               读取command命令的输入并插入,如:r!ls会先执行ls,然后读入内容
:w!command               将当前已编辑文件作为command命令的标准输入并执行command命令,如:w!grep all
:cd directory            将当前工作目录更改为directory所表示的目录
:sh                      将启动一个子shell,使用^d(ctrl+d)返回vi
:so file                 在shell程序file中读入和执行命令

vi中的宏与缩写
(避免使用控制键和符号,不要使用字符K、V、g、q、v、*、=和功能键)

选项                         作用
:map key command_seq     定义一个键来运行command_seq,如:map e ea,无论什么时候都可以e移到一个字的末尾来追加文本
:map                     在状态行显示所有已定义的宏
:umap key                删除该键的宏
:ab string1 string2      定义一个缩写,使得当插入string1时,用string2替换string1。当要插入文本时,键入string1然后按Esc键,系统就插入了string2
:ab                      显示所有缩写
:una string              取消string的缩写

在vi中缩进文本

选项                         作用
^i(ctrl+i)或tab          插入文本时,插入移动的宽度,移动宽度是事先定义好的
:set ai                  打开自动缩进
:set sw=n                将移动宽度设置为n个字符
n<<                      使n行都向左移动一个宽度
n>>                      使n行都向右移动一个宽度,例如3>>就将接下来的三行每行都向右移动一个移动宽度

顺带附上vim的用户手册:

http://vcd.gro.clinux.org/doc/usr_toc.html

http://vimdoc.sourceforge.net/htmldoc/

来源:http://hily.me/blog/2006/04/vi-shortcut/

猜你喜欢

转载自justcoding.iteye.com/blog/1580482