现在的位置: 首页 > 综合 > 正文

vi中执行shell命令

2013年10月22日 ⁄ 综合 ⁄ 共 1428字 ⁄ 字号 评论关闭
vi中执行shell命令
    在vi中也可以执行shell的命令。在line mode下执行指令!command, command 是 shell 命令。例如指令 !date 会把目前的时间和日期显示到屏幕上。
要是你想把 shell 命令的结果放到文件中,可以用 r !command。比如 r !date 会把现在的时间插入到游标所在行的下一行。可以指定行数,例如 20r !date 会把现在的时间放到第二十一行。
如果你想要执行一连串的的 shell命令但又不想离开vi,你可以在line mode下指令sh。这个指令会建立一个 shell,如果要离开,按【 ctrl-D 】,或者执行exit命令。
你也可以选择文档中的文字作为 shell 命令的输入。输出的结果会替换掉你输入的文字。例如 2,5!sort 会把第二列到第五列的文字依序排列,而原来的文字就会被覆盖。
 
实战:
#vi dosh.txt
 1      Love Ain’t Wait For You
 2      S Club 7
 3   Your heart beat's taking over
 4   And there's nowhere you can go
 5   And the love that you discover
 6   Is finally taking hold
 7   When it feels like the beginning
 8   And the story will unfold
 9   There's a time to take your chances
10   Don't be scared of the unknown
11   Cause you can't fight it
12   Don't deny it
13   Love is stronger than each other
14   Show me baby
15   What you're gonna do, do
16   Love ain't gonna wait for you
18   Love ain't gonna wait for you
 
执行
:1,5!sort
 
 1      Love Ain’t Wait For You
 2      S Club 7
 3   And the love that you discover
 4   And there's nowhere you can go
 5   Your heart beat's taking over
 6   Is finally taking hold
 7   When it feels like the beginning
 8   And the story will unfold
 9   There's a time to take your chances
10   Don't be scared of the unknown
11   Cause you can't fight it
12   Don't deny it
13   Love is stronger than each other
14   Show me baby
15   What you're gonna do, do
16   Love ain't gonna wait for you
17   Don't run, don't hide
18   Love ain't gonna wait for you
 
执行
:2r!date
 
 
1      Love Ain’t Wait For You
2      S Club 7
3 2007年07月05日 星期四 16时29分48秒 CST
4   And the love that you discover
5   And there's nowhere you can go
 
注:文件内容前的数组为行号(:set nu)。

抱歉!评论已关闭.