LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1612|回复: 8

强大的 vi

[复制链接]
发表于 2003-6-15 08:04:49 | 显示全部楼层 |阅读模式
Search

search for string "john" or "joan"
/jo[ha]n

search for strings "the", "theatre" or "then"
/\< the

search for strings "the" or "breathe"
/the\>

search for word "the"
/\< the\>

search for all 4 lettered words
/\< ....\>

search for string "pay up" in the forward direction
/pay up

search for string "pay up" in the backward direction
?pay up

Search and Replace
replace begining of every line with "hello"
:%s/^/hello/g

replace end of every line with "Harry"
:%s/$/Harry/g

replace every "onward" with "forward"
irrespective of case
:%s/onward/forward/gi

search for "Ted Bundy" and replace "Bundy" with "Danson"
but do not change any "Al Bundy"
:g/Ted Bundy/s/Bundy/Danson/g

remove trailing blanks
:%s/ *$//g

Replace the following lines
create user user103 identitified by user103
create user user104 identitified by user104
create user user105 identitified by user105
with
grant connect to user103
grant connect to user104
grant connect to user105
:%s/^.*\(user.*$\)/grant connect to \1/g

deletes every line that contains "string"
:g/string/d

deletes every line that does not contain "string"
:v/string/d


Convert Case

turn all letters to uppercase
:%s/.*/\U&

turn all letters to lowercase
:%s/.*/\L&

turn the first letter of all words to uppercase
:%s/\<./\u&/g

turn the first letter of all words to lowercase
:%s/\<./\l&/g

turn the first letter of all lines to uppercase
:%s/.*/\u&

turn the first letter of all lines to lowercase
:%s/.*/\l&

Read / Write from another file

Write lines 1 through 10 from the current file to outfile
:1,10 w outfile

Write lines 1 through 10 from the current file to outfile forcefully
:1,10 w! outfile

Append lines 1 through 10 from the current file to outfile
:1,10 w >> outfile

Read from infile to the current file below current line
:r infile

Read from infile to the current file below line 23 of current file
:23r infile

Open a file called hitlist from unix prompt using vi
and then have cursor automatically at line 62
vi +62 hitlist

Open a file called hitlist from unix prompt using vi
and then have cursor automatically at line containing Bugs Moran
vi +/"Bugs Moran" hitlist

Interact with unix

execute the command "pwd" on unix and return to vi
:!pwd

execute the command "pwd" on unix and capture
the output into the file being edited
!!pwd

exit out of the file being edited into unix temporarily
:sh

return back to the file from unix
$exit

Setting line numbers

set line numbers to visible
:se nu

set line numbers to invisible
:se nonu

Advanced commands

copy lines 1 through 5 after line 35
:1,5t35

copy lines 1 through 5 at the end of the file
:1,5t$

remove null lines in the file
:g/^$/d

align all lines
:%!fmt

align all lines starting from current location downward
!}fmt

align next 5 lines
5!!fmt

Append "son" to "john" on the first 5 lines
:1,5s/john/&son/g

Double the number of spaces between the words
:%s/ */&&/g

Delete everything after and including the colon.
:s/^\(.*\):.*/\1/g

Swap the words either side of the colon.
:s/\(.*\):\(.*\)/\2:\1/g

move down 3 lines from the current line
:+3

move up 7 lines from the current line
:-7
发表于 2003-6-15 08:30:45 | 显示全部楼层

gooooooood
发表于 2003-6-15 11:05:28 | 显示全部楼层
good
发表于 2003-6-15 16:56:58 | 显示全部楼层
the power of regular expression
发表于 2003-6-16 03:30:38 | 显示全部楼层
强,多谢charley兄
发表于 2003-6-16 03:40:19 | 显示全部楼层
强啊~~:!! :!! :!! :!! :!! :!!
谢谢~~
发表于 2003-6-16 10:22:51 | 显示全部楼层
ding! ding !
发表于 2004-12-18 14:43:54 | 显示全部楼层
good
发表于 2004-12-18 17:55:06 | 显示全部楼层
顶一下
方便以后回来查看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表