Vim高级用法速查

本文详细介绍了ex编辑器的基本命令及使用方法,包括扩展命令、位置标志符号、全局搜索、文件操作等,并提供了多个实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]Chapter 5. Introducing the ex Editor[/b]
几个常用的扩展命令
[table]
|命令 | 简写 | 解释|
|delete | d | Delete lines. |
|move | m | Move lines. |
|copy | co | Copy lines. |
| | t | Copy lines (a synonym for co). |
[/table]
示例:
[table]
|:3,18d | 删除第3行到第18行|
|:160,224m23 | 将第160行到第224行移动到第23行下。|
|:23,29co100 | 复制第23行到第29行到第100行下。|
[/table]
[b]几个位置标志符号[/b]
一个点 (.) 代表当前行;
$ 代表最后一行;
% 代表每一行,相当于 1,$ 。
+ 和 - 可以指定相对位置。
示例:
[table]
|:.,$d | 删除从当前行到最后一行。 |
|:20,.m$ | 将第20行到当前行移动到最后一行之后。 |
|:%d | 删除所有行。 |
|:%t$ | 复制所有行到最后一行之后。 |
[/table]
使用相对位置:
[table]
|:.,.+20d | 删除20行,从当前行向下。 |
|:226,$m.-2 | 将第226行到最后一行移到到当前行的下面的两行之下。 |
|:-,+t0 | 复制三行数据到第一行之前。(当前行,当前行的上一行,当前行的下一行) |
[/table]
使用查找模式:
[table]
|:/pattern/d | 删除指定模式的行。(光标向下找到的第一行) |
|:/pattern/+d | 删除指定模式的行的下一行。(相当于:/pattern/+1d) |
|:/pattern1/,/pattern2/d | 删除指定pattern1的行到指定pattern2的行。 |
|:.,/pattern/m23 | 移动当前行到指定模式的行到第23行下。 |
[/table]
[table]
|d/while|The vi delete to pattern command deletes from the cursor up to the
word while, but leaves the remainder of both lines. |
|:.,/while/d|The ex command deletes the entire range of addressed lines; in this
case both the current line and the line containing the pattern. All
lines are deleted in their entirety. |
[/table]
重定义当前行查找:
[table]
|:10;+5 p | 输出第10行到第15行这六行。(prints lines 10 through 15.) |
|:/pattern/;+10 p | 输出指定模式到指定模式下10行。 |
[/table]
[b]Global Searches [/b]
[table]
|:g/pattern | Finds (moves to) the last occurrence of pattern in the file. |
|:g/pattern/p | Finds and displays all lines in the file containing pattern. |
|:g!/pattern/nu | Finds and displays all lines in the file that don't contain pattern; also displays the line number for each line found. |
|:60,124g/pattern/p | Finds and displays any lines between lines 60 and 124 containing pattern. |
[/table]
扩展命令联合使用:
:1,5m10|g/pattern/nu
Move lines 1 through 5 after line 10, and then display all lines (with numbers) containing pattern.

基本的保存和退出:
[table]
|:w | Writes (saves) the buffer to the file but does not exit. |
|:q | Quits the editor. |
|:wq | Both writes the file and quits the editor. The write happens unconditionally, even if the file was not changed. |
|:x | Both writes the file and quits (exits) the editor. The file is written only if it has been modified. |
|:w! | can also be used to save edits in a file that was opened in read-only mode. |
|:q! | is an essential editing command that allows you to quit without affecting the original file, regardless of any changes you made in this session. The contents of the buffer are discarded. |
[/table]
文件另存和追加:
[table]
|:230,$w newfile | Saves file in newfile. |
|:230,$w newfile | Saves from line 230 to end of file in newfile. |
|:.,600w newfile | Saves from the current line to line 600 in newfile.|
|:340,$w >>newfile | append from line 340 to the end of the buffer to newfile.|
[/table]
文件的追加式读取:
[table]
|:read filename | 读取文件内容插入到当前行下面。|
|:r filename | This command inserts the contents of filename starting on the line after the cursor position in the file.|
|:185r filename | To read in the same file and place it after line 185.|
|:$r filename | Place the read-in file at the end of the current file.|
|:0r filename | Place the read-in file at the very beginning of the current file.|
|:/pattern/r filename | Place the read-in file in the current file, after the line containing pattern.|
[/table]
[b]Chapter 6. Global Replacement [/b]
示例:
[table]
|:s/old/new/ | This changes the first occurrence of the pattern old to new on the current line.|
|:s/old/new/g | This changes every occurrence of old to new on the current line.|
|:50,100s/old/new/g | This change every occurrence of old to new from line 50 to line 100.|
|:1,$s/old/new/g | This command will change every occurrence of old to new within the entire file.|
|:%s/old/new/g | You can also use % instead of 1,$ to specify every line in a file.|
[/table]
依赖上下文的替换:
[table]
|:g/pattern/s/old/new/g | |
|:g/<keycap>/s/Esc/ESC/g | To change instances of Esc to ESC only when Esc is on a line that contains the <keycap> directive.|
|:g/string/s//new/g | This would search for lines containing string and substitute for that same string.|
|:g/editer/s//editor/g | has the same effect as: :%s/editer/editor/g|
[/table]
替换用的几个标记:
[table]
|\n | Is replaced with text matched by the nth pattern previously saved by \( and \), where n is a number from 1 to 9.|
|& | Is replaced with the entire text matched by the search pattern when used in a replacement string. :%s/Yazstremski/&, Carl/ ; :1,10s/.*/(&)/ |
|~ | This is useful for repeating an edit. :s/his/their/ → :s/her/~/|
|\u or \l | Causes the next character in the replacement string to be changed to uppercase or lowercase, respectively. :%s/yes, doctor/\uyes, \udoctor/ ; :s/\(That\) or \(this\)/\u\2 or \l\1/ |
|\U or \L and \e or \E | \U and \L are similar to \u or \l, but all following characters are converted to uppercase or lowercase until the end of the replacement string or until \e or \E is reached. :%s/Fortran/\UFortran/ ; :%s/Fortran/\U&/|
[/table]
一些典型示例:
[table]
|:s/red/blue/ | :/green :~ equivalent to :s/green/blue/.|
|:%s;/user1/tim;/home/tim;g|
|:%s:RETURN:<I>&</I>:g |
|:%s/[Hh]elp/\U&/g|
|:g/^$/d | Delete all blank lines.|
|:%s/^/ / | Insert two spaces at the start of every line in a file.|
|:.,+5s/$/./ | Add a period to the end of the next six lines.|
|:%s/.*/\U&/ | Change every word in a file to uppercase.|
|:g/.*/m0 | Reverse the order of lines in a file.|
|:g!/^[0-9]/m$ | For any line that doesn't begin with a number, move the line to the end of the file.|
[/table]
继续补充:
[b]6.4.3 More Examples [/b]
[b]Chapter 7. Advanced Editing[/b]
[b]Chapter 11. vim—vi Improved [/b]
[table]
|\∣ |Indicates alternation, house\∣home. |
|\+ |Matches one or more of the preceding regular expression.|
|\= |Matches zero or one of the preceding regular expression. |
|\{ n, m} |Matches n to m of the preceding regular expression, as much as possible. n and m are numbers between 0 and 32000; vim requires only the left brace to be preceded by a backslash, not the right brace.|
|\{ n} |Matches n of the preceding regular expression.|
|\{ n,} |Matches at least n of the preceding regular expression, as much as possible.|
|\{, m} |Matches 0 to m of the preceding regular expression, as much as possible.|
|\{} |Matches 0 or more of the preceding regular expression, as much as possible (same as *).|
|\{- n, m} |Matches n to m of the preceding regular expression, as few as possible.|
|\{- n} |Matches n of the preceding regular expression.|
|\{- n,}|Matches at least n of the preceding regular expression, as few as possible.
|\{-, m} |Matches 0 to m of the preceding regular expression, as few as possible.|
|\i |Matches any identifier character, as defined by the isident option.|
|\I |Like \i, but excluding digits.|
|\k |Matches any keyword character, as defined by the iskeyword option. |
|\K |Like \k, but excluding digits.|
|\f |Matches any filename character, as defined by the isfname option.|
|\F |Like \f, but excluding digits.|
|\p |Matches any printable character, as defined by the isprint option.|
|\P |Like \p, but excluding digits.|
|\s |Matches a whitespace character (exactly space and tab). |
|\S |Matches anything that isn't a space or a tab.|
|\b |Backspace. |
|\e |Escape. |
|\r |Carriage return. |
|\t |Tab. |
|\n |Reserved for future use. Eventually, it will be used for matching multi-line patterns. See the vim documentation for more details.|
|~ |Matches the last given substitute (i.e., replacement) string.|
|\(...\) |Provides grouping for *, \+, and \=, as well as making matched sub-texts available in the replacement part of a substitute command (\1, \2, etc.).|
|\1 |Matches the same string that was matched by the first sub-expression in \( and \). For example: \([a-z]\).\1 matches ata, ehe, tot, etc. \2, \3, and so on may be used to represent the second, third, and so forth subexpressions.|
[/table]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值