`
oaklet
  • 浏览: 108866 次
  • 性别: 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.

分享到:
评论

相关推荐

    Linux常用工具速查实用手册

    通过这本"Linux常用工具速查实用手册",用户不仅可以了解到这些工具的基本用法,还能学习到如何结合它们进行高效的工作。手册可能包括了命令行使用示例、配置技巧以及常见问题的解决方案,是Linux用户提升技能的好...

    vim_cheat_sheet_for_programmers_print

    这些只是Vim编辑器庞大功能库中的一部分,实际的Vim速查表会包含更多高级技巧和快捷键,如宏的编辑与应用、文件比较、自动补全、正则表达式等。通过熟练掌握这些知识,程序员可以充分利用Vim的强大功能,提升编程...

    Linux核心应用命令速查.pdf

    根据提供的信息,“Linux核心应用命令速查.pdf”这一资料旨在为用户提供一份清晰、实用的Linux命令查询手册。尽管具体的文档内容未被提供,但我们可以基于标题、描述以及常见的Linux应用场景来构建一系列重要的知识...

    Linux指令范例速查手册(第2版).pdf

    《Linux指令范例速查手册(第2版)》是一本专门为Linux用户和管理员准备的实用工具书。这本书详尽地列举了各种常用的Linux命令及其用法,旨在帮助读者快速查找并理解Linux操作系统中的基本操作和高级技巧。下面将...

    Linux系统指令速查手册

    以上只是Linux系统指令速查手册中的部分核心知识点,实际手册会包含更多细节和高级用法。学习并熟练掌握这些命令将极大地提高你在Linux环境中的工作效率。通过持续实践和查阅手册,你将能更好地驾驭这个开源操作系统...

    linux命令速查_查看文件.pdf

    ### Linux命令速查:查看文件的关键知识点 #### 标题解析 标题“Linux命令速查_查看文件.pdf”明确指出了文档的主要内容是关于Linux环境下查看文件的各种命令及其使用方法。这对于初学者或是有一定经验的用户来说都...

    Linux指令速查手册

    本文将围绕“Linux指令速查手册”这一主题,详细介绍一些常用的Linux命令及其应用场景。 #### 二、Linux基础知识 1. **文件系统结构** - Linux采用树状目录结构,根目录为“/”,所有文件都以根目录为基础进行...

    vim使用说明与快捷键一张图

    **vim使用说明与快捷键** vim(Vi Improved)是一款强大的文本编辑器,广泛应用...对于初学者来说,是一份极好的学习资料,对于老手来说,也是日常工作中不可或缺的速查工具。建议打印出来随时翻阅,提升vim操作效率。

    速查表:developers开发人员的资料库,用于各种编程备忘单

    SQL 速查表通常包括选择、插入、更新、删除语句,以及联接、子查询和聚合函数的用法。 8. **Matlab**: Matlab 是一种数值计算和可视化环境,适用于工程和科学计算。Matlab 速查表涵盖矩阵操作、函数、绘图和数据...

    Linux 命令 速查手册 影印版 pdf

    - **vi/vim**: 高级文本编辑器,用于创建和编辑文件。 - **head** 和 **tail**: 查看文件的开头或结尾部分。 - **find**: 搜索文件,可以根据文件名、大小、时间等条件查找。 3. **权限与所有权** - **chmod**:...

    Linux命令查询全集 chm

    在IT领域,Linux操作系统是广泛应用于服务器、嵌入式设备以及个人计算机的一种开源操作系统。Linux以其高度可定制性、稳定性及强大的命令行工具而著称。对于熟悉Linux系统的人来说,熟练掌握各种命令是提高工作效率...

    Linux常用软件速查表 (2).pdf

    以上只是速查表中部分软件的介绍,实际上Linux环境中还有许多其他工具和应用,如版本控制系统`Git`、系统监控工具`Gnome System Monitor`、办公套件`LibreOffice`等。这份速查表的价值在于它为Linux用户提供了一个...

    Qt Creator键盘快捷键速查

    16. **使用Vim风格编辑**:`Alt+V, Alt+V` — 切换到 Vim 风格的编辑模式。 17. **分栏**:`Ctrl+E, 2` — 分割编辑器为两部分。 18. **左右分栏**:`Ctrl+E, 3` — 左右分割编辑器。 19. **在新窗口打开**:`Ctrl+E...

    Linux命令速查表

    Linux命令速查表是Linux系统用户日常操作的重要参考...以上只是Linux命令速查表中的一小部分,实际的速查表通常涵盖更多细节,包括每个命令的完整参数列表和用法示例。熟练掌握这些命令,将使你在Linux环境中游刃有余。

    Linux学习笔记2.zip

    最后,压缩包中的"Linux速查备忘手册"可能会提供一些实际案例和解决常见问题的技巧,帮助学习者将理论知识应用到实践中,提升问题解决能力。总之,这个压缩包是一个全面的Linux学习资源,适合初学者入门,也对有经验...

    Linux编程很全的资料

    vi/vim是Linux系统中的经典文本编辑器,掌握其基本操作和高级功能对日常开发极其有用。C函数速查则是快速查找和理解C语言标准库函数的便捷工具。 总的来说,这个压缩包提供的资源全面覆盖了Linux环境下的编程学习...

    cheat.python

    `cheat-cheat-python`这个文件名可能是cheat.sh工具的一个Python版本,cheat.sh是一个在线的命令参考资料,提供各种编程语言和工具的命令速查。通过这样的工具,你可以快速查阅到Python的语法特性,同时获取到Linux...

    LinuxInANutshell6thEdition-英文原版.zip

    《Linux in a Nutshell》第六版不仅适合初学者入门,也为经验丰富的系统管理员提供了宝贵的速查资料。通过阅读这本书,读者可以深入理解Linux操作系统,提升在命令行环境下工作的效率和能力。无论你是开发人员、系统...

    ubuntu部落(适合初学者,可以作为初学者的手册)

    - **bash**:作为默认Shell,学习如何使用bash进行高级操作。 - **中止正在运行的程序**:掌握如何优雅地停止或杀死进程。 - **Ctrl+s**:解释如何使用Ctrl+s暂停终端输出。 - **键绑定**:自定义键盘快捷键以提高...

Global site tag (gtag.js) - Google Analytics