`
oaklet
  • 浏览: 107645 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Vim高级用法速查

阅读更多
Chapter 5. Introducing the ex Editor
几个常用的扩展命令
命令 简写 解释
delete d Delete lines.
move m Move lines.
copy co Copy lines.
  t Copy lines (a synonym for co).

示例:
:3,18d 删除第3行到第18行
:160,224m23 将第160行到第224行移动到第23行下。
:23,29co100 复制第23行到第29行到第100行下。

几个位置标志符号
一个点 (.) 代表当前行;
$ 代表最后一行;
% 代表每一行,相当于 1,$ 。
+ 和 - 可以指定相对位置。
示例:
:.,$d 删除从当前行到最后一行。
:20,.m$ 将第20行到当前行移动到最后一行之后。
:%d 删除所有行。
:%t$ 复制所有行到最后一行之后。

使用相对位置:
:.,.+20d 删除20行,从当前行向下。
:226,$m.-2 将第226行到最后一行移到到当前行的下面的两行之下。
:-,+t0 复制三行数据到第一行之前。(当前行,当前行的上一行,当前行的下一行)

使用查找模式:
:/pattern/d 删除指定模式的行。(光标向下找到的第一行)
:/pattern/+d 删除指定模式的行的下一行。(相当于:/pattern/+1d)
:/pattern1/,/pattern2/d 删除指定pattern1的行到指定pattern2的行。
:.,/pattern/m23 移动当前行到指定模式的行到第23行下。

d/whileThe vi delete to pattern command deletes from the cursor up to the word while, but leaves the remainder of both lines.
:.,/while/dThe 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.

重定义当前行查找:
:10;+5 p 输出第10行到第15行这六行。(prints lines 10 through 15.)
:/pattern/;+10 p 输出指定模式到指定模式下10行。

Global Searches
: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.

扩展命令联合使用:
:1,5m10|g/pattern/nu
    Move lines 1 through 5 after line 10, and then display all lines (with numbers) containing pattern.

基本的保存和退出:
: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.

文件另存和追加:
: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.

文件的追加式读取:
: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.

Chapter 6. Global Replacement
示例:
: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.

依赖上下文的替换:
: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

替换用的几个标记:
\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&/

一些典型示例:
: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.

继续补充:
6.4.3 More Examples 
Chapter 7. Advanced Editing
Chapter 11. vim—vi Improved
\∣ 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.

分享到:
评论

相关推荐

    vim常用命令速查

    vim常用命令速查 ,思维导图表示,方便快速记忆

    vim常用命令速查表

    vim常用命令速查手册,是png图片格式的,使用方便。

    vim速查命令

    自己整理的关于超强编辑器gVim的命令速查一览表!

    vim命令速查卡

    里面是vim命令的键盘分布图,还有一些频率很高的命令。

    linux vim高级应用,vim浏览C代码比source code还高效

    vim 高级应用

    vim高级使用技巧

    vim的高级使用技巧,适合有一定功底的人使用,实例: ;:------------------------------------------------------------------------- ;:" 在文件中插入行号 :g/^/exec "s/^/".strpart(line(".")."", 0, 4) :%s/^/\=...

    vim速查图 速查表(全网最高清,适合打印)

    #vim速查图(全网最高清),#vim速查表,#vim速查卡,具体见博客vim速查图(可能是全网最高清版本)_兴趣斗士的博客-CSDN博客 https://blog.csdn.net/BjarneCpp/article/details/110923086

    VIM的高级应用和配置

    VIM高级应用,包括各种编程辅助插件的配置和使用方法等。Unix程序员必备

    vim高级配置

    vim高级配置

    vim中文速查表

    VIM CHEATSHEET (中文速查表) - by skywind (created on 2017/10/12) # Version: 42, Last Modified: 2018/07/19 19:04 # https://github.com/skywind3000/awesome-cheatsheets

    Vim命令速查手册Vim

    命令还是比较全,而且关系整理的挺好的,不过是全英文的,不知道到合不合大家胃口

    vim常用命令vim常用命令vim常用命令

    vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令vim常用命令

    vim高级实用手册

    对vim快捷、技巧的总结,忘了的时候可以看看,推新手

    vim速查表 Beautiful Vim Cheat-Sheet by Max Cantor

    Beautiful Vim Cheat-Sheet by Max Cantor 非常NICE的vim速查表,高清5925*4125,在作者博客上售价25刀,参见:http://vimcheatsheet.com/

    vim实用技巧快速入门与速查手册

    《vim实用技巧快速入门与速查手册》,根据经验编写成册,是一本非常好的入门手册和命令速查手册。帮助新手快速上手。

    vim 高级实例技巧

    VIM 实例技巧 英文版的,页数不多,但看完后,VIM/VI使用将大有长进 不是基础教程,如果不知道VI是什么,怎么复制,粘贴,删除,移动光标还是不要看好 适用于熟悉VI并想进一步提高的朋友们

    5个最好的vim指南图(速查表)

    这里找来了 5 个最好的 Vim Cheat Sheet, 不仅每个按键本身的操作,还有组合健, 窗口,缓冲区,寄存器等操作。

    vim速查表教程,共7课

    图形vi vim速查表和教程 学习vi或vim并不容易。但也不那么难。在任何情况下,它都比编辑任何其他编辑器更快、更强大、更高效,因此,您可以很好地投入时间和精力来学习它。 作为一个vi爱好者,我提出了一个想法,为...

    速查表-vim,去新的地方下载

    速查表-vim包含内容: vim-cheatsheet.png 更好的内容在这里:vim速查图速查表(全网最高清,适合打印)-其它文档类资源-CSDN下载 https://download.csdn.net/download/BjarneCpp/13608675 VIM.docx

    Easwy大牛的高级VIM技巧网页版

    Easwy大牛的高级VIM技巧网页版,学习VIM的好教材,感谢easwy!

Global site tag (gtag.js) - Google Analytics