免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1754 | 回复: 0
打印 上一主题 下一主题

Basics of the vi Editor [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-02 11:28 |只看该作者 |倒序浏览


Basics of the vi Editor

Prologue
The vi editor is the standard text editor on all UNIX machines. It is simply an ascii text editor, rather than a word processing package (e.g., the edited files are not appended with ".vi")
Some basic vi commands
o (open a new line below)
O (open a new line above)
a (append characters after cursor)
i (insert characters before cursor)
dd (delete current line)
x (delete a char)
r (replace 1 char)
R (retype (typeover) mode)
u (undo last edit)
. (perform the last edit again)
(to exit open mode, append mode, insert mode)
:w (save)
:wq (save and exit)
:q (exit)
:q! (exit and abort changes)
"Magic Seven + one"
dd (delete line)
x (delete char)
o (open a new line)
i (insert char)
a (append char)
:wq (save changes and exit)
:q! (quit with out saving changes)
u (undo last edit)
Modes
Certain commands will place cause vi to enter a mode. The most common modes are "OPEN MODE", "INSERT MODE", "APPEND MODE", and "REPLACE MODE". These modes are used when typing text into thedocument. To exit from one of these modes, press the Esc key. This will return the user to the COMMAND MODE. On the Suns, placing the line "set showmode" in a users .exrc file, will cause the current modeto be displayed in the lower right-hand corner of the window. COMMAND MODE is not display, as it is the default mode. From COMMAND MODE is where the user enters in edits/commands to be executed.
Movements
There are many quick keys that allow the user to move to different areas of the file with ease.
j Move down a line
k Move up a line
h Move left one char
l Move right one char
^F Forward a page
^B Back a page
^D Down a half page
^U Up a half page
H,M,L move to the High, Middle, or Low part of the screen
0 Beginning of line (you can also use ^)
$ End of line
G Bottom of file
nG Line number "n"
w Beginning of next word
b Beginning of last word
tc Move to the next occurance of "c"
/str^M More to the next occurance of "str"
n Move to the next occurance of "str"
(assuming /str^M has been done)
The / key, and what "str" can be.
This will look for a string, both simple and a regular expression. A regular expression is a combination of regular characters and special characters. To search for any special charcter as a regular character, place a \ before the specialcharacter.
/abcefg/ matches anything that contains "abcefg". This is
a simple regular expression that contains no
special characters.
Special characters:
. matches any character
* matches zero or more of previous characters
^ matches beginning of line
$ matches end of line
/ delimiter-marks beginning and end of search item.
Examples of searches:
/c.t/ would match: cat cot cbt cct cit cut
/c\.t/ would only match: c.t
/ba*d/ would match: bd bad baad baaad baaaad
/ba\*d/ would only match: ba*d
/b.*p/ would match: pd pid pad paid pod
/b\.\*p/ would only match: b.*p
Movements with Markers
A marker is place in a file by the command m, followed by the letter to be associated with that marker. The cursor can be move to a marker with useing the ' command followed by the marker letter.
ma place the marker "a"
'a move to marker "a"
Single key edits
p place text from buffer after line
P place text from buffer before line
. last edit again
Edits - simple
There are several simple edits. They have the form: (n)(e) Where (n) is the number of times to perform the edit, and (e) is the edit.
x delete character
r replace 1 char with a char
s replace 1 char with a string
for example: "5x" deletes 5 characters
R retype
u undo last edir
i insert before cursor
a append after cursor
I insert before first char on line
A append after last char on line
Edits - compound
With compound edits, the form is (n)(e)(m). (n) and (e) as above, and (m) is a movement.
d delete, and place it in the buffer
c change
y yank, and place it in the buffer
If a compound edit is typed twice, then is taken to mean
current line. (ie: dd delete current line)
for example, "5dd2G" deletes 5 lines, and brings the cursor to line 2 of the file.
Reading and Writing
:r file read file into editor
:r !command inputs results of command into editor
:w save file, continue editing
:w file write to new file
:q quit
:q! quit with out saving changes
:'a,'bw file write from marker 'a to marker 'b
Search and Replace
This can be an extremely powerful and time-saving command. Say you are trying to input an ascii file into a spreadsheet, and you notice that there is a comma separating values, and you need to replace that with a space. Well for a small file this is no problem, but if you've got a thousand cases this could take quite a while, plus you might not catch all of the commas.
And before we get into this, remember how to UNDO a command: type "u" and return. (in case you make a mistake with your search and replace)
Searching: /string (where string is a character or series of characters)
To repeat the search: n (will keep finding "string"; N will look for "string" in the opposite direction)
For example, you want to go straight to "Chapter 6" in your thesis text file. Type "/Chapter 6" and return - note that search is case sensitive.
or,
"/^Chap" will take you to the next occurence of the string Chap that begin on the left-hand side of the page. "n" will take you to the next occurrence.
Searching and Replacing
:g/string1/s//string2/g (there are other ways to do this search and replace, as you will find if you get more involved with vi)
Replace all occurences of string1 with string2. The combinations of / and . can be used for a walk through search and replace.
For example, want to replace forearm with forearc:
;g/forearm/s//forearc/g
For example, you want to replace all periods in a file with commas:
:g/\./s//\,/g


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/30557/showart_290509.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP