#three modes(三种模式)
Command mode (命名模式)
This is the default when you enter vi. In command mode, most letters, or short sequences of letters, that you type will be interpreted as commands, without explicitly pressing Enter . If you press Esc when you're in command mode, your terminal will beep at you. This is a very good way to tell when you're in command mode.
这是进入vi的默认模式。在该模式下,大多数字母或字母序列,都将作为是命名执行,而不需要按Enter键。如果你按Esc键,系统将发出嘟嘟的声音。这是判断是否进入该模式的一个好方法。
Insert mode (编辑模式)
In insert mode, whatever you type is inserted in the file at the cursor position. Type a (lowercase letter a, for append),A,i,I,o,O to enter insert mode from command mode; press Esc to end insert mode, and return to command mode.
在插入(我觉得叫编辑模式贴切)模式,所有的输入都被插入在光标位置,输入a(在光标位置后追加),A(在光标所以行末尾追加),i(在光标位置之前插入),I(在光标所在行行头插入),o(在光标所在行之下插入新空行),O(在光标所在行之上插入新空行)从命名模式进出插入模式。按Esc返回到命名模式。
Line mode (命名行模式)
Use line mode to enter line oriented commands. To enter line mode from command mode, type a colon ( : ). Your cursor moves to the bottom of the screen, by a colon prompt. Type a line mode command, then press Enter. Any sensible command from the Unix line editor ex will work, and a few are good to know about.
用行模式输入面向行的命令。键入冒号(:)进入行模式,光标会移动到屏幕的底部,这里会有一个冒号的提示符。输入一个行命令,然后敲回车键,任何unix编辑器ex的智能命名都可以被执行,其中有一些命名是很有用的。
#starting vi and saving files(开始vi并保存文件)
#start editing filename, create it if necessary
vi filename
开始编辑一个文件,如果文件不存在则创建一个新文件。
#write the file to disk and quit
:wq
保存文件并退出
#quit without saving any changes
:q!
强制退出,不保存。
#write all lines from the entire current file into the file 'newfile', overwriting any existing newfile
:w! newfile
将当前文件整个写入到新文件中,如果新文件存在则将被覆盖。
#write the lines from n to m, inclusive, into the file newfile, overwriting any existing newfile
:n,m w! newfile
将当前文件的第n到m行,包括n和m行,写入到新文件里,如果文件存在则被覆盖。
#Moving the cursor(移动光标)
Many commands take number prefixes; for example 5w moves to the right by 5 words.
很多命令都可以以数字作为前缀。例如,5w则向右移动5个words.
Type: | To Move To: |
h | one space to the left (also try left arrow) |
j | one line down (also try down arrow) |
k | one line up (also try up arrow) |
l | one space to the right (also try right arrow) |
$ | end of current line |
^ | beginning of current line |
Enter | beginning first word on the next line |
G | end of file |
:n | line n; use :0 to move the beginning of the file |
w | beginning of next word; 5w moves to the beginning of the 5th word to the right |
e | end of next word |
b | beginning of previous word |
Ctrl-b | one page up |
Ctrl-f | one page down |
% | the matching (, ), [, ], {, or } (Press % with your cursor on one of these characters to move your cursor its mate.) |
#searching text(搜索文本)
Type: | To: |
/string | search down for string |
?string | search up for string |
n | repeat last search from present position |
#insertind text(插入文本)
Type: | To: |
a | append starting right of cursor |
A | append at the end of the current line |
i | insert starting left of cursor |
I | insert at beginning of the current line |
o | open line below cursor, then enter insert mode |
O | open line above cursor, then enter insert mode |
:r newfile | add the contents of the file newfile starting below the current line |
#deleting text(删除文本)
Type: | To: |
x | delete single character; 5x deletes 5 characters |
dw | delete word; 5dw deletes 5 words |
dd | delete line; 5dd deletes ... well you get the idea! |
cw | delete word, leaves you in insert mode (i.e. change word) |
cc | change line -- delete line and start insert mode |
s | change character -- delete character and start insert mode |
D | delete from cursor to end of line |
C | change from cursor to end of line -- delete and start insert mode |
u | undo last change |
U | undo all changes to current line |
J | join current line with line that follows (press Enter in insert mode to split line) |
#cutting and pasting(剪切和粘贴)
Type: | To: | |
xp | transpose two characters (two commands, x followed by p) | |
yy | yank (i.e. copy) one line into a general buffer (5yy to yank 5 lines) | |
"ayy | yank into the buffer named a | |
P | put the general buffer back before the current line | |
"aP | put from buffer a before current line | |
p | put the general buffer back after the current line | |
"ap | put from buffer a after the current line |
Note: dd and any other delete will save a copy of the deleted string in the same general buffer.Therefor a cut and paste can be done with dd and p, rather than copy with yy and p. But make sure not to overwrite the buffer with some other yank or delete command before you have a chance to paste.
注意:dd和其他删除命令会保存一份被删除的字符串在同一个普通缓冲区。因此剪切和粘贴可以用dd后再用p命令完成,而不是用yy和p。但是,你需要确认,在你粘贴之前,不能用其他删除或复制的命令。
#miscellaneous Commands (其他杂项)
Type: | To: |
Ctrl-g | show line number of current line |
Ctrl-l | redraw the entire display |
:!sh | fork a shell; type Ctrl-d to get back to vi |
. | repeat last text change command at current cursor position |
#Customizing vi: the .exrc file
The .exrc file in your login directory is a collection of vi commands to customize that environment.
.exrc 文件在你登录目录,它是vi命令和自定义环境的一个集合。
:set showmode | show when you are in insert mode |
:set ic | ignore case when searching |
:set noic | turn ignore case off |
:set nu | turn on line numbering |
:set nonu | turn line numbering off |
#block editing(块操作)
快速处理 '、"、()、[]、{}、<> 等配对标点符号中的文本内容,包括更改、删除、复制等。
- ci'、ci"、ci(、ci[、ci{、ci< - 分别更改这些配对标点符号中的文本内容
- di'、di"、di(、di[、di{、di< - 分别删除这些配对标点符号中的文本内容
- yi'、yi"、yi(、yi[、yi{、yi< - 分别复制这些配对标点符号中的文本内容
- di(等价于dib,di{等价于diB。
- 把 i 改成 a 的话,会连配对标点一起操作
-
写HTML的时候还有vit, vat, dit, dat, cit, cat等等
#row editing(列编辑)
ctrl+v 进入列编辑Visual Block模式
h,j,k,l 移动光标
i 插入 esc确认
d删除 esc确认
#example(使用实例)
1.character twiddling(字符颠倒)
"the"->"teh"
the 打成了teh
put the cursor on the cursor on the "e" and type xp.
把光标放到e上让后输入xp
The "x" command deletes a character (the "e") and the "p" pastes it after the cursor (which is now placed over the "h".)
x命令删除一个字符(这是是e),而p命令会把它粘贴在当前光标(这个时候被h代替)之后。
2.interactively replacing one word with another (把一个单词换成另外一个 one by one)
Suppose you want to replace every occurrence of the word "idiot" with the word "manager". But you want the chance to review each change before you do it.
Here is what you do:
1. | 1G | Go to the top of the document |
2. | /idiot<enter> | Find the first occurrence of the word "idiot" |
3. | cwmanager | Change the word (cw) to manager. |
4. | n | Repeat the last search (find the next idiot.) |
5. | . | Repeat the last edit (change one word to manager) (If you do not want to change the word, skip this step.) |
假设把所有的单词idiot置换成manager,但是你又想在每次置换前在重新确认一边。就按以上步骤操作。
3.interactively replacing one word with another (把一个单词换成另外一个 another method)
Suppose you want to replace every occurrence of the word "idiot" with the word "manager". But you want the chance to review each change before you do it.
Execute the command:
:%s//<idiot/>/manager/gc
This command will make the change and pause after each change to give you a chance to confirm it. You can enter "y" to accept the change or "n" to not accept it.
假设把所有的单词idiot置换成manager,但是你又想在每次置换前在重新确认一边。就用该命令,这个命令会在做置换的时候提示你是否确认,如果确认则输入y,否则输入n即可。命令的解释如下
: | Enter command mode |
% | Perform this command on all lines (% is a synomim for the first to last line.) |
s | The short form of the :substitute command. |
//<idiot/>/ | This text specifies the text we are looking for wand want. The /< tells Vim to match a word start and the /> tells Vim to match the end of a word. |
/manager/ | The replacement text |
gc | The flags. These are
|
4.moving text using vi style command(移动文字 用vi形势的命令)
Command | Explaination | |
1. | Move the cursor to the top of the paragraph you want to move. | |
2. | ma | Place a mark named "a" at this location. (Vim will give you no indication that this command has been executed. In other words, the screen will not change.) |
3. | Move the cursor to the bottom of the paragraph to be moved. | |
4. | d'a | Delete to mark "a". This puts the deleted text in a cut buffer. |
5. | Move the cursor to line where the text is to go. The paragraph will be placed after this one. | |
6. | p | Paste the text in below the cursor. |
5.moving text using visual block(移动文字 用虚拟块的方式)
Command | Explaination | |
1. | Move the cursor to the top of the paragraph you want to move. | |
2. | v | Start visual mode. (If you want to move only full lines, use the V command which starts visual line mode. |
3. | Move the cursor to the bottom of the paragraph to be moved. The text to be moved will be hightlighted. | |
4. | d | Perform a visual delete. In other words delete the highlighted text. |
5. | Move the cursor to line where the text is to go. The paragraph will be placed after this one. | |
6. | p | Paste the text in below the cursor. |
6.copy a block of text from one file to another vi style(把文本块从一个文件复制到另外一个文件 vi 方式)
Command | Explaination | |
1. | Edit the file containing the text you want to copy. | |
2. | Go to the top line to be copied. | |
3. | ma | Mark this line as mark "a". |
4. | Go to the bottom line to be copied | |
5. | y'a | Yank (y) the text from the current cursor location to the mark "a" ('a) |
6. | :split second-file | Open another window containing the second file. (This the file in which the text is to be inserted.) |
7. | Go to the line where the insert is to occur. The text will be place after this line. | |
8. | p | Put the text after the cursor. |
7.copy a block of text from one file to another visual block
Command | Explaination | |
1. | Edit the file containing the text to be copied. | |
2. | Go to the top line to be copied. | |
3. | v | Start visual mode. If you want to copy a block of full lines, use V to go start Visual Line Mode |
4. | Go to the bottom line to be copied. The text to be copied will be hightlighted. | |
5. | y | Yank (y) the text. |
6. | :split second-file | Open another window containing the second file. (This the file in which the text is to be inserted.) |
7. | Go to the line where the insert is to occur. The text will be place after this line. | |
8. | p | Put the text after the cursor. |
8.sorting a session vi style (分类)
Command | Explaination | |
1. | Move the cursor to the first line to be sorted. | |
2. | ma | Mark the first line as mark a. |
3. | Move to the bottom of the text to be sorted. | |
4. | !'asort | The ! command tells Vim to run the text through UNIX command. The 'a tell the editor that the text to be worked on starts at the current line and ends at mark a. The command that the text is to go through is sort. |
9.sorting a session visual block
Command | Explaination | |
1. | Move the cursor to the first line to be sorted. | |
2. | V | Enter visual line mode |
3. | Move to the bottom of the text to be sorted. | |
4. | !sort | The ! command tells Vim to run the hightlighted text through UNIX command. The command that the text is to go through is sort. |
注意:/ spce tab(:set list)的特殊处理
10.formating a text paragraph(设置段落格式)
:set wrapmargin=70
Command | Explaination | |
1. | Move to the top of the paragraph. | |
2. | gq} | The "!" command tells Vim to pipe a section of text through a filter. The } tells Vim that the section of text for the pipe command is a single paragraph. |
3. | fmt -70 | The UNIX command fmt is a primitive formatter. It performs word-wrapping well enough for text documentation. The -70 tells fmt to format lines for 70 characters per line. |
11.find a procedure in c program
generate a table of contents of all the C program files in your current working directory, use the command:
$ctags *.c
then if you want to edit the procedure write_file use the command:
$vi -t write_file
Now suppose as you are looking at the write_file procedure that it calls setup_data and you need to look at that procedure. To jump to that function, position the cursor at the beginning of the word setup_data and press Ctrl+]. This tells Vim to jump to the definition of this procedure. This repositioning will occur even if Vim has to change files to do so.
12.drawing a comment box(画一个注释框 利用类似alias别名的方法)
I like to put a big comment box at the top of each of my procedures. For example:
/******************************************************* * Program -- Solve it -- Solves the worlds problems. * * All of them. At once. This will be a great * * program when I finish it. * *******************************************************/
Drawing these boxes like this is tedious at best. But Vim has a nice feature called abbreviations that makes things easier.
First, you need to create a Vim initialization file called ~/.vimrc. (At first this may look like a ex initialization file. It is. The Vim command is actually a mode of the ex editor.)
The ~/.vimrc file need to contain the lines:
:ab #b /************************************************ :ab #e ************************************************/
These command define a set of Vim abbreviations. What's a Vim abbreviation? Its a word that stands for another word. When Vim see the abbreviation, it will expand it to the full word. In this case we've defined an abbreviation called #b that expands to the beginning line of a comment box. The #e abbreviation does the same thing.
So to create a comment box enter #b<enter>. The screen looks like:
/************************************************
Enter the comments, including the beginning and ending "*" characters. Finally end the comment by typing #e<enter>. This causes the ending comment to be entered.
13.reading a man page(察看命令帮助文档)
$man subject | ul -i | vim -
14.remving carriage returns from MS-DOS file (删除windows自带的^M)
To remove the ^M characters from a MS-DOS file, enter the command:
:1,$s/{ctrl+v}{ctrl+M}//{Enter}
15.trimming the blank of an end line(删除每行结尾的空格或tab)
:1,$s/[ <tab>]*$//
16.change "first,last" to "last first"
:1,$s//([^,]*/), /(.*$/)//2 /1/
The figure below shows the relationship between the /( /) enclosed strings and the /1, /2 markers.
:1,$s//([^,]*/), /(.*$/)//2 /1/ ^^ ^^ ^^ ^^^ ^ ^ || || || ||| | +----- String matched by || || || ||| | first /( /) || || || ||| +--------- String matched by || || || ||| second /( /) || || || ||+----------- Slash separating || || || || old/new strings || || ++---++------------ Second /( /) ++-----++--------------------- First /( /)The next figure breaks out the various parts of the regular expressions used in this example.
:1,$s//([^,]*/), /(.*$/)//2 /1/ ^^^^^ ^^ ^^^ ||||| || ||+--- The end of the line ||||| || |+---- Repeated 0 or more time ||||| || +----- Any character ||||| || +++--- Any character, repeated, ||||| || followed by EOL ||||| |+-------- The character space ||||| +--------- The character comma ||||+------------ Repeated 0 or more times |||+------------- Closes the [] expression ||+-------------- The character comma |+--------------- Match anything except the | next character +---------------- Starts a set of matches ++++------------- Match anything but comma +------------ Repeated 0 or more times +--------- Followed by comma
17.how to edit all the file contains one word (如何批量编辑所有包含某单词的文件)
$ vim `fgrep -l indentation_level *.c`
` is the backtick (`是反)
another method:
start vi
:grep >word< >file-list<
This finds the first location of word in the given files and positions the cursor on that line. You can use the command :cn to find the next occurance.