vi命令学习

(1) :set 空格键 然后按enter 可以显示当前设置与默认设置的差别

    :set all 然后按enter 可以显示所有设置

    :set Option

    :set noOption

    :set Option=Value

    以上设置方法只对当前session有效

    如果需要长期有效需要设置 exrc资源文件 或者环境变量EXINIT in the .profile file (read by the shell on login)

1. 自动缩进 :set ai

2. 是否允许当前文件夹下的exrc文件有效 :set exrc

3. 搜索正则表达式的时候是否对大小写敏感  :set ic

 

(2) 宏的使用

1. 有名缓冲区的建立

输入命令,例一

o /coren/+3  #这一句建立新行,输入/coren/+3文本

"add         #这句把上面建立的文本删除放入有名缓冲区a当中

             #小写a代表覆盖,大写代表追加,可以a-z,A-Z

例二

o -dd        #

"Tdd         #将文本-dd追加到有名缓冲区t的末尾,该宏完成任务是:上移一行,然后删除该行

 

2. map 的方法

1.  :map # 查看当前有的map命令

    :map Letter viSubcommand #这是在命令模式下发生的字符与命令的映射

    :unmap Letter

 

    :map! Letter String

    :unmap! Letter

    :map!     #这是在文本模式下发生的映射,被映射的键不能再用于文本输入

    allows you to run a specified command or sequence of commands by pressing a single key

2.  下面这个语句,将字母v的含义定义为 MAP替换map一次

    Maps character strings to single keys while in text input mode.

  :map v /map<Ctrl-V><Enter>cwMAP<Ctrl-V><Esc><Ctrl-V><Enter> # <Ctrl-V>的作用是To prevent the vi editor from interpreting使得<Enter><Esc>按键转义为可输入形式,而不是立即生效

  根据功能键的不同,可能需要一到两个<Ctrl-V>协助其转义为输入字符

  :map <Ctrl-A> :w<Ctrl-V><Enter>:n<Ctrl-V><Enter> #写入文件并转到下一个文件

  :map g :!cat /etc/motd <Ctrl-V><Ctrl-V>| wc<Ctrl-V><Enter>

  :map #1 dwwww         #F1键功能设置为删除一个单词,然后移动三个单词位置

  :map! % <Ctrl-V><Esc>bbwcw          #把百分号映射为命令序列

  :map! city Austin, Texas 78759

3. :ab  Maps a key or sequence of keys to a string of characters for use in the text input mode.

   :unab

   :ab city Austin, Texas 78759  #文本模式,系统自动替换单词city为后面一串字符串

                                 #此处替换是以单词为单位的city可以, ascity不行

                                 #map! 也可以完成类似功能,不过ascity也同样有效

在文本中移动

1. 5G  #Go to the line specified by the Number parameter

   G -- 缓冲区起始位置

   nG  -- 到第n

2. 25| #Go to the column specified by the Number parameter

3. 10Ctrl-U #Scroll the number of lines up or down specified by the Number

   10Ctrl-D

4. -  #向上移动一行

   +  #单行移动,向下

   H   Moves the cursor to the top line on the screen.

   L   Moves the cursor to the last line on the screen.

   M   Moves the cursor to the middle line on the screen.

5. 几个方向键  h j k l

              左下上右

              5h - 左移5个字符

6.     ^    Moves the cursor to the first nonblank character.

       0    Moves the cursor to the beginning of the line.

       $    Moves the cursor to the end of the line.

       ( )    句首

       {}     段首尾

       [[   ]]节首尾

7.     w    Moves the cursor to the next small word.

       b    Moves the cursor to the previous small word.

       e    Moves the cursor to the next end of a small word.

       W    Moves the cursor to the next big word.

       B    Moves the cursor to the previous big word.

       E    Moves the cursor to the next end of a big word.

------------------------------------------------------------

1. /Pattern  向后搜索模式

2. ?Pattern  向前搜索模式

   //  ??  -- 重复前一次查找

3. n  重复前一个搜索,方向相同

4. N  重复后一个搜索,相反方向

5. /Pattern/+Number  搜索,位置定位在其后Num

6. ?Pattern?-Number  搜索,位置定位在其前Num

7. %  寻找匹配的符号

i或者a键进入插入或者追加模式

A -- 行尾附加

I -- 当前行第一个非空字符前插入文本

o -- 打开新行

O -- 在上面打开新行

78i*ESC   -- 插入78*

 

cc -- 改变整行内容

c$ -- 改变当前光标之后的行字符

C -- 改变至行尾

cw -- Changes a word.

dd -- Deletes a line.

dw -- Deletes a word.

r -- 替换单个字符

4rx -- 当前光标往后4个替换为x

R -- 替换至行尾,直到按下esc

x -- 删除一个字符

X -- 删除前一个字符

~  -- Changes letter at the cursor to the opposite case.

p -- 放置Puts back text from the undo buffer

P -- 放置在光标前

y -- Places the object that follows (for example, w for word) into the undo buffer.

Y -- Places the line in the undo buffer.

"n p -- Retrieves the nth last delete of a complete line or block of lines. 从删除缓冲区当中恢复数据,n是数字

例如:"1p

"xp -- Puts back text from the x buffer.

"xd -- Deletes text into the x buffer.

"xy -- Places the object that follows into the x buffer, where x is any letter.

"z2yy -- z缓冲区放删除的两行数据

"a2dw -- 删除后两个字,放入缓冲区a

"jD -- 删除光标至行尾内容放入j缓冲区

"jp -- 光标后放缓冲区j的内容

"by) -- 光标到句尾的内容放入b缓冲区

"Ad4w --  删除光标后4个字,追加到a缓冲区

"2p  -- 把倒数第2次删除的内容放入光标位置

y$ --  取样到行尾

Y -- 整行取样

yw -- 取样一个字

3yy  -- 取样三行

yy -- 取样一行

. -- Repeats the last change

u -- 撤销

U -- 撤销从光标最近一次移至该行起时对光标所在行做的全部改动

 

J -- Joins lines.

<< -- Shifts one line to the left.

<L -- Shifts all lines from the cursor to the end of the screen to the left.

>> -- Shifts one line to the right.

>L -- Shifts all lines from the cursor to the end of the screen to the right.

 

Ctrl-G 显示当前文件信息

:w  -- 将缓冲区用原文件名保存

:w file  -- 将缓冲区写入文件file

:addr w file --addr指定的行写入file

:w!  -- 写入缓冲区,覆盖原文件

:w! file  -- 将缓冲区写入文件file,覆盖

ZZ  -- 保存退出

 

:e file  -- 编辑文件

:e!  从上一次保存的文件开始编辑,强制载入上次保存的版本,放弃所有修改

:e + file  -- 编辑file,光标在最下一行

:e + nbr file  -- 编辑,光标在第nbr

:e #  --切换到前一个文件,在用e命令切换之后。#代表前一个文件This subcommand is the same as the Ctrl-A

 

:r file  -- 文件内容读入并插入光标下一行

:r! command  -- 执行shell命令并输出结果到光标下一行

:addr r file  --载入文件file进入缓冲区,放入addr行末尾

 

:n  -- 编辑下一个文件

:n! --放弃保存 ,编辑下一个文件

:n file file2 file3  -- 设定待编辑文件顺序

 

:sh  -- 运行shell并留下直到 输入 Ctrl-D命令退出或者exit

:!Command --Runs the specified command and then returns to the vi editor.

Tip: The # (alternate file), % (current file), and ! (previous command) special characters are expanded when following a :! subcommand. To prevent any of these characters from being expanded, use the / (backslash).

:!!  --Repeats the last :!Command subcommand.

:!ls  --输出当前dir下的文件list

:!ls -l hg* --输出当前dir下的文件list,长列表格式,以hg开头的

:!ls -l /!* ----输出当前dir下的文件list,长列表格式,以!开头的,其中!要进行转义,如果不转义,!会被上一次的以!开头的命令替换

:addr ! shellcmd  -- shell命令也即ex过滤文本,例如:100,108!Sort,排序后的文本替换排序前的

!Object Command -- shell命令输出替换object

!}tr [a-z] [A-Z] --用大写替换小写,当前行

!count! shellcmd  --当前开始往下count行,执行cmd

!object!  --  !}tr [a-z] [A-Z]命令之后,只要输入!}!就可以重复上一个命令,大小写替换了!

nbr !! comd -- shell输出替换当前行开始的nbr行文本

 

:addrd -- 删除addr指定的行

:addrmnbr -- 将由addr指定的行移到nbr之后

:addrconbr -- 将由addr指定的行复制到nbr之后

:addrtnbr -- 将由addr指定的行复制到nbr之后

. $ % -- 当前行,最后一行,每一行

:1,5d --删除15

.,20m$ --当前行到20行移至末尾

.,.+20co --从当前行开始的20行复制到文件开头

8,/pattern/t --从第8行到含pattern的行复制到本行行尾

:8  --定位到第八行

:s/plans/opps/  --在当前行用opps代替plans

:$s/plans/opps/g  --在缓冲区用opps代替所有的plans

:.,+20s/plans/opps/  --在当前行开始的20行里,用opps代替第一个plans

:g/22/d  --删除所有包括22的行

:g/plans/p  --显示所有包含plans的行

:g!/22/d   -- 删除所有不包括22的行

:8,12g/plans/p  --显示8-22行之间,包含plans的行

 

正则表达式

.  --任意单个字符,除换行符外

*  --前一个字符的0到多个匹配

^  --正则表达式的第一个字符

$  --正则表达式的最后一个字符

/< --字的第一字符

/> --字的最后一个字符

/  --转义字符

[] --与括号内任意字符匹配,如[a-z,A-Z],如要表示-可将它做为第一个字符[-az],超字符出现在括号中间不需要转义

[^] --括号内没有的单一字符

/(/) --将文本中与转义括弧匹配的括弧对中间的文字存入编号缓冲区内

/n  --用于样本替换,其中n1-9的整数,重显用转义括弧对保存的文本

& -- 使用生成匹配的样本,用于保存输入

/u /l  -- 用在替换样本中,使得后一字符改变大小写

/U  /L  -- 用在替换样本中,使得之后其余字符改变大小写,直到遇到/e  /E

/e  /E -- 用在替换样本中,终止/U  /L

~   --与前一正则表达式查找命令的查找样本匹配

g  --使得替换全局化,不同于全局命令

c  --提示确认

可以先用全局命令选定行,然后用替换命令做替换

:%s/ex/vi/g  --在缓冲区中用vi替换所有的ex

:.,$s/ex/vi/c  --在缓冲区中用vi替换所有的ex,需要确认每个替换

:%s//<author/>/contractor/g  --在缓冲区中,用contractor替换每个独立字author

:g/editor/s/line/full-screen/g  --把所有包含editor的行中的line全部用full-screen替换

:g/editor/s//full-screen/g  --把所有包含editor的行中的editor全部用full-screen替换

:%s/bb*/b/g  --用一个空格代替每一个或者多个匹配

:%s/[:.]bb*/([a-z])//.bb/u/1/g   -- 查找后面跟一个或者多个空格和一个小写字母的冒号或者句号,替换成一个句号两个空格和该字母的大写形式

:g/^$/d  --删除所有空白行

:g/^/m0  --缓冲区所有行反排序

:g!/comp/s/$/To be done  --附加To be done 到不含comp的行的末尾

 

Q --进入ex编辑器

:vi -- 进入viex

:wq

:q!

esc -- 进入命令模式

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值