基本的vi编辑器命令

本文介绍了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


:xExit, saving changes
:qExit as long as there have been no changes
ZZExit and save changes if any have been made
:q!Exit and ignore any changes

 


Inserting Text


iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters

 


Motion


hMove left
jMove down
kMove up
lMove right
wMove to next word
WMove to next blank delimited word
bMove to the beginning of the word
BMove to the beginning of blank delimted word
eMove to the end of the word
EMove to the end of Blank delimited word
(Move a sentance back
)Move a sentance forward
{Move a paragraph back
}Move a paragraph forward
0Move to the begining of the line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
MMove to middle of screen
LMove 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:
xDelete character to the right of cursor
XDelete character to the left of cursor
DDelete to the end of the line
ddDelete current line
:dDelete 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:
yyYank the current line
:yYank 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:
CChange to the end of the line
ccChange the whole line

 


Putting text


pPut after the position or after the line
PPut 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.
mcSet marker c on this line
`cGo to beginning of marker c line.
'cGo to first non-blank character of marker c line.

 


Search for strings


/stringSearch forward for string
?stringSearch back for string
nSearch for next instance of string
NSearch 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/flagsReplace pattern with string according to flags.
gFlag - Replace all occurences of pattern
cFlag - 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
\nContents 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,mRange - Lines n-m
:.Range - Current line
:$Range - Last line
:'cRange - Marker c
:%Range - All lines in file
:g/pattern/Range - All lines that contain pattern

 


Files


:w fileWrite to file
:r fileRead file in after line
:nGo to next file
:pGo to previos file
:e fileEdit file
!!programReplace line with output from program

 


Other


~Toggle upp and lower case
JJoin lines
.Repeat last text-changing command
uUndo last change
UUndo 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 filenameedit filename starting at line 1
 vi -r filenamerecover 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
 wmove cursor to beginning of next word
 bmove cursor back to beginning of preceding word
 :0<Return> or 1Gmove cursor to first line in file
 :n<Return> or nGmove cursor to line n
 :$<Return> or Gmove 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.

 

 ^fmove forward one screen
 ^bmove backward one screen
 ^dmove down (forward) one half screen
 ^umove up (back) one half screen
 ^lredraws the screen
 ^rredraws 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.

 

*uUNDO 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.

 

*iinsert text before cursor, until <Esc> hit
 Iinsert text at beginning of current line, until <Esc> hit
*aappend text after cursor, until <Esc> hit
 Aappend text to end of current line, until <Esc> hit
*oopen and put text in a new line below current line, until <Esc> hit
*Oopen and put text in a new line above current line, until <Esc> hit

 

Changing Text

 

The following commands allow you to modify text.

 

*rreplace single character under cursor (no <Esc> needed)
 Rreplace characters, starting with current cursor position, until <Esc> hit
 cwchange the current word with new text, 
starting with the character under cursor, until <Esc> hit
 cNwchange N words beginning with character under cursor, until <Esc> hit; 
  e.g., c5w changes 5 words
 Cchange (replace) the characters in the current line, until <Esc> hit
 ccchange (replace) the entire current line, stopping when <Esc> is hit
 Ncc or cNcchange (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.

 

*xdelete single character under cursor
 Nxdelete N characters, starting with character under cursor
 dwdelete the single word beginning with character under cursor
 dNwdelete N words beginning with character under cursor; 
  e.g., d5w deletes 5 words
 Ddelete the remainder of the line, starting with current cursor position
*dddelete entire current line
 Ndd or dNddelete 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.

 

 yycopy (yank, cut) the current line into the buffer
 Nyy or yNycopy (yank, cut) the next N lines, including the current line, into the buffer
 pput (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.

 

 /stringsearch forward for occurrence of string in text 
 ?stringsearch backward for occurrence of string in text 
 nmove to next occurrence of search string
 Nmove 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 
 ^gprovides 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

转载于:https://www.cnblogs.com/apiaceae/archive/2010/03/03/1677540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值