缤纷多彩的植物信息世界

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面。例如语法加亮,可视化操作不仅可以在终端运行,也可以运行于x window、 mac os、 windows。

vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令。由于对Unix及 Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它。Vi也是Linux中最基本的文本编辑器,学会它后,您将在Linux的世界里畅行无阻。

[简单地,可以使用上下左右方向箭头和delete,backspace键来进行位置移动和删除,不管是命令模式还是插入模式]

1、vi的基本概念
基本上vi可以分为三种状态,分别是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下:
1) 命令行模式command mode)
控制屏幕光标的移动,字符、字或行的删除,移动复制某区段及进入Insert mode下,或者到 last line mode。
2) 插入模式(Insert mode)
只有在Insert mode下,才可以做文字输入,按「ESC」键可回到命令行模式。
3) 底行模式(last line mode)
将文件保存或退出vi,也可以设置编辑环境,如寻找字符串、列出行号……等。

不过一般我们在使用时把vi简化成两个模式,就是将底行模式(last line mode)也算入命令行模式command mode)。

2、vi的基本操作
a) 进入vi
在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面:$ vi myfile。不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command mode)」,您要切换到「插入模式(Insert mode)」才能够输入文字。初次使用vi的人都会想先用上下左右键移动光标,结果电脑一直哔哔叫,把自己气个半死,所以进入vi后,先不要乱动,转换到「插入模式(Insert mode)」再说吧!

b) 切换至插入模式(Insert mode)编辑文件
在「命令行模式(command mode)」下按一下字母「i」就可以进入「插入模式(Insert mode)」,这时候你就可以开始输入文字了。

c) Insert 的切换
您目前处于「插入模式(Insert mode)」,您就只能一直输入文字,如果您发现输错了字!想用光标键往回移动,将该字删除,就要先按一下「ESC」键转到「命令行模式(command mode)」再删除文字。

d) 退出vi及保存文件
在「命令行模式(command mode)」下,按一下「:」冒号键进入「Last line mode」,例如:
: w filename (输入 「w filename」将文章以指定的文件名filename保存)
: wq (输入「wq」,存盘并退出vi)
: q! (输入q!, 不存盘强制退出vi)

3、命令行模式(command mode)功能键
1). 插入模式
按「i」切换进入插入模式「insert mode」,按“i”进入插入模式后是从光标当前位置开始输入文件;
按「a」进入插入模式后,是从目前光标所在位置的下一个位置开始输入文字;
按「o」进入插入模式后,是插入新的一行,从行首开始输入文字。

2). 从插入模式切换为命令行模式
按「ESC」键。

3). 移动光标
vi可以直接用键盘上的光标来上下左右移动,但正规的vi是用小写英文字母「h」、「j」、「k」、「l」,分别控制光标左、下、上、右移一格。
按「ctrl」+「b」:屏幕往“后”移动一页。
按「ctrl」+「f」:屏幕往“前”移动一页。
按「ctrl」+「u」:屏幕往“后”移动半页。
按「ctrl」+「d」:屏幕往“前”移动半页。
按数字「0」:移到文章的开头。
按「G」:移动到文章的最后。
按「$」:移动到光标所在行的“行尾”。
按「^」:移动到光标所在行的“行首”
按「w」:光标跳到下个字的开头
按「e」:光标跳到下个字的字尾
按「b」:光标回到上个字的开头
按「#l」:光标移到该行的第#个位置,如:5l,56l。

4). 删除文字
「x」:每按一次,删除光标所在位置的“后面”一个字符。
「#x」:例如,「6x」表示删除光标所在位置的“后面”6个字符。
「X」:大写的X,每按一次,删除光标所在位置的“前面”一个字符。
「#X」:例如,「20X」表示删除光标所在位置的“前面”20个字符。
「dd」:删除光标所在行。
「#dd」:从光标所在行开始删除#行

5). 复制
「yw」:将光标所在之处到字尾的字符复制到缓冲区中。
「#yw」:复制#个字到缓冲区
「yy」:复制光标所在行到缓冲区。
「#yy」:例如,「6yy」表示拷贝从光标所在的该行“往下数”6行文字。
「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与“y”有关的复制命令都必须与“p”配合才能完成复制与粘贴功能。

6). 替换
「r」:替换光标所在处的字符。
「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

7). 回复上一次操作
「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次“u”可以执行多次回复。

8). 更改
「cw」:更改光标所在处的字到字尾处
「c#w」:例如,「c3w」表示更改3个字

9). 跳至指定的行
「ctrl」+「g」列出光标所在行的行号。
「#G」:例如,「15G」,表示移动光标至文章的第15行行首。

4、Last line mode下命令简介
  在使用「last line mode」之前,请记住先按「ESC」键确定您已经处于「command mode」下后,再按「:」冒号即可进入「last line mode」。

A) 列出行号
「set nu」:输入「set nu」后,会在文件中的每一行前面列出行号。

B) 跳到文件中的某一行
「#」:「#」号表示一个数字,在冒号后输入一个数字,再按回车键就会跳到该行了,如输入数字15,再回车,就会跳到文章的第15行。

C) 查找字符
「/关键字」:先按「/」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往后寻找到您要的关键字为止。
「?关键字」:先按「?」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往前寻找到您要的关键字为止。

D) 保存文件
「w」:在冒号输入字母「w」就可以将文件保存起来。

E) 离开vi
「q」:按「q」就是退出,如果无法离开vi,可以在「q」后跟一个「!」强制离开vi。
「qw」:一般建议离开时,搭配「w」一起使用,这样在退出的时候还可以保存文件。

5、vi命令列表
1、下表列出命令模式下的一些键的功能:

h左移光标一个字符
l右移光标一个字符
k光标上移一行
j光标下移一行
^光标移动至行首
0数字“0”,光标移至文章的开头
G光标移至文章的最后
$光标移动至行尾
Ctrl+f向前翻屏
Ctrl+b向后翻屏
Ctrl+d向前翻半屏
Ctrl+u向后翻半屏
i在光标位置前插入字符
a在光标所在位置的后一个字符开始增加
o插入新的一行,从行首开始输入
ESC从输入状态退至命令状态
x删除光标后面的字符
#x删除光标后的#个字符
X(大写X),删除光标前面的字符
#X删除光标前面的#个字符
dd删除光标所在的行
#dd删除从光标所在行数的#行
yw复制光标所在位置的一个字
#yw复制光标所在位置的#个字
yy复制光标所在位置的一行
#yy复制从光标所在行数的#行
p粘贴
u取消操作
cw更改光标所在位置的一个字
#cw更改光标所在位置的#个字


2、下表列出行命令模式下的一些指令
w filename储存正在编辑的文件为filename
wq filename储存正在编辑的文件为filename,并退出vi
q!放弃所有修改,退出vi
set nu显示行号
/或?查找,在/后输入要查找的内容
n与/或?一起使用,如果查找的内容不是想要找的关键字,按n或向后(与/联用)或向前(与?联用)继续查找,直到找到为止。

原文链接:http://hi.baidu.com/navyran/blog/item/a070922c02c3273e359bf77b.html

 

Modes


Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.

 


Quitting


:x Exit, saving changes
:q Exit as long as there have been no changes
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes

 


Inserting Text


i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters

 


Motion


h Move left
j Move down
k Move up
l Move right
w Move to next word
W Move to next blank delimited word
b Move to the beginning of the word
B Move to the beginning of blank delimted word
e Move to the end of the word
E Move to the end of Blank delimited word
( Move a sentance back
) Move a sentance forward
{ Move a paragraph back
} Move a paragraph forward
0 Move to the begining of the line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file
:n Move to nth line of the file
fc Move forward to c
Fc Move back to c
H Move to top of screen
M Move to middle of screen
L Move to botton of screen
% Move to associated ( ), { }, [ ]

 


Deleting Text


Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are:
x Delete character to the right of cursor
X Delete character to the left of cursor
D Delete to the end of the line
dd Delete current line
:d Delete current line

 


Yanking Text


Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are:
yy Yank the current line
:y Yank the current line

 


Changing text


The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For wxample cw changes a word. A few other change commands are:
C Change to the end of the line
cc Change the whole line

 


Putting text


p Put after the position or after the line
P Put before the poition or before the line

 


Buffers


Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.

 


Markers


Named markers may be set on any line in a file. Any lower case letter may be a marker name. Markers may also be used as limits for ranges.
mc Set marker c on this line
`c Go to beginning of marker c line.
'c Go to first non-blank character of marker c line.

 


Search for strings


/string Search forward for string
?string Search back for string
n Search for next instance of string
N Search for previous instance of string

 


Replace


The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flags Replace pattern with string according to flags.
g Flag - Replace all occurences of pattern
c Flag - Confirm replaces.
& Repeat last :s command

 


Regular Expressions


. (dot) Any single character except newline
* zero or more occurances of any character
[...] Any single character specified in the set
[^...] Any single character not specified in the set
^ Anchor - beginning of the line
$ Anchor - end of line
\< Anchor - begining of word
\> Anchor - end of word
\(...\) Grouping - usually used to group conditions
\n Contents of nth grouping

 

[...] - Set Examples
[A-Z] The SET from Capital A to Capital Z
[a-z] The SET from lowercase a to lowercase z
[0-9] The SET from 0 to 9 (All numerals)
[./=+] The SET containing . (dot), / (slash), =, and +
[-A-F] The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z] The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z] In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters

 

Regular Expression Examples
/Hello/ Matches if the line contains the value Hello
/^TEST$/ Matches if the line contains TEST by itself
/^[a-zA-Z]/ Matches if the line starts with any letter
/^[a-z].*/ Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/ Matches if line ends with 2134
/\(21|35\)/ Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/ Matches if there are zero or more numbers in the line
/^[^#]/ Matches if the first character is not a # in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specified

 


Counts


Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with thismethod, say to insert the same line 100 times.

 


Ranges


Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:n,m Range - Lines n-m
:. Range - Current line
:$ Range - Last line
:'c Range - Marker c
:% Range - All lines in file
:g/pattern/ Range - All lines that contain pattern

 


Files


:w file Write to file
:r file Read file in after line
:n Go to next file
:p Go to previos file
:e file Edit file
!!program Replace line with output from program

 


Other


~ Toggle upp and lower case
J Join lines
. Repeat last text-changing command
u Undo last change
U Undo all changes to line

 

 

 

To Get Into and Out Of vi

 

To Start vi

 

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

 

*vi filename edit filename starting at line 1
 vi -r filename recover filename that was being edited when system crashed

 

To Exit vi

 

Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.

 

Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return>(or <Enter>) key.

 

*:x<Return> quit vi, writing out modified file to file named in original invocation
 :wq<Return> quit vi, writing out modified file to file named in original invocation
 :q<Return> quit (or exit) vi
*:q!<Return> quit vi even though latest changes have not been saved for this vi call

 


 

Moving the Cursor

 

Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.

 

If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.

 

In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.

 

*j or <Return> 
  [or down-arrow]
move cursor down one line
*k [or up-arrow] move cursor up one line
*h or <Backspace> 
  [or left-arrow]
move cursor left one character
*l or <Space> 
  [or right-arrow]
move cursor right one character
*0 (zero) move cursor to start of current line (the one with the cursor)
*$ move cursor to end of current line
 w move cursor to beginning of next word
 b move cursor back to beginning of preceding word
 :0<Return> or 1G move cursor to first line in file
 :n<Return> or nG move cursor to line n
 :$<Return> or G move cursor to last line in file

 


 

Screen Manipulation

 

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.

 

 ^f move forward one screen
 ^b move backward one screen
 ^d move down (forward) one half screen
 ^u move up (back) one half screen
 ^l redraws the screen
 ^r redraws the screen, removing deleted lines

 


 

Adding, Changing, and Deleting Text

 

Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.

 

Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.

 

*u UNDO WHATEVER YOU JUST DID; a simple toggle

 

The main purpose of an editor is to create, add, or modify text for a file.

 

Inserting or Adding Text

 

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc>key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

 

*i insert text before cursor, until <Esc> hit
 I insert text at beginning of current line, until <Esc> hit
*a append text after cursor, until <Esc> hit
 A append text to end of current line, until <Esc> hit
*o open and put text in a new line below current line, until <Esc> hit
*O open and put text in a new line above current line, until <Esc> hit

 

Changing Text

 

The following commands allow you to modify text.

 

*r replace single character under cursor (no <Esc> needed)
 R replace characters, starting with current cursor position, until <Esc> hit
 cw change the current word with new text, 
starting with the character under cursor, until <Esc> hit
 cNw change N words beginning with character under cursor, until <Esc> hit; 
  e.g., c5w changes 5 words
 C change (replace) the characters in the current line, until <Esc> hit
 cc change (replace) the entire current line, stopping when <Esc> is hit
 Ncc or cNc change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit

 

Deleting Text

 

The following commands allow you to delete text.

 

*x delete single character under cursor
 Nx delete N characters, starting with character under cursor
 dw delete the single word beginning with character under cursor
 dNw delete N words beginning with character under cursor; 
  e.g., d5w deletes 5 words
 D delete the remainder of the line, starting with current cursor position
*dd delete entire current line
 Ndd or dNd delete N lines, beginning with the current line; 
  e.g., 5dd deletes 5 lines

 

Cutting and Pasting Text

 

The following commands allow you to copy and paste text.

 

 yy copy (yank, cut) the current line into the buffer
 Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer
 p put (paste) the line(s) in the buffer into the text after the current line

 


 

Other Commands

 

Searching Text

 

A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.

 

 /string search forward for occurrence of string in text 
 ?string search backward for occurrence of string in text 
 n move to next occurrence of search string
 N move to next occurrence of search string in opposite direction

 

Determining Line Numbers

 

Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.

 

 :.= returns line number of current line at bottom of screen 
 := returns the total number of lines at bottom of screen 
 ^g provides the current line number, along with the total number of lines,
in the file at the bottom of the screen

 


 

Saving and Reading Files

These commands permit you to input and output files other than the named file with which you are currently working.

 

 :r filename<Return> read file named filename and insert after current line 
(the line with cursor)
 :w<Return> write current contents to file named in original vi call
 :w newfile<Return> write current contents to a new file named newfile
 :12,35w smallfile<Return> write the contents of the lines numbered 12 through 35 to a new file named smallfile
 :w! prevfile<Return> write current contents over a pre-existing file named prevfile
posted on 2010-03-03 21:46  虎克  阅读(550)  评论(0编辑  收藏  举报