`
king_tt
  • 浏览: 2191040 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

超过 130 个你需要了解的 vim 命令 - 转自开源中国

 
阅读更多

从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为 “每个程序员都应该知道的 100 个 vim 命令” 这次算是之前那篇文章的改进版,希望你会喜欢。

基础

:e filename Openfilenamefor edition
:w Save file
:q Exit Vim
:q! Quit without saving
:x Write file (if changes has been made) and exit
:sav filename Saves file asfilename
. Repeats the last change made in normal mode
5. Repeats 5 times the last change made in normal mode


在文件中移动

k or Up Arrow move the cursor up one line
j or Down Arrow move the cursor down one line
e move the cursor to the end of the word
b move the cursor to the begining of the word
0 move the cursor to the begining of the line
G move the cursor to the end of the line
gg move the cursor to the begining of the file
L move the cursor to the end of the file
:59 move cursor to line59. Replace59by the desired line number.
20| move cursor to column20.
% Move cursor to matching parenthesis
[[ Jump to function start
[{ Jump to block start


剪切、复制和粘贴

y Copy the selected text to clipboard
p Paste clipboard contents
dd Cut current line
yy Copy current line
y$ Copy to end of line
D Cut to end of line


搜索

/word Searchwordfrom top to bottom
?word Searchwordfrom bottom to top
* Search the word under cursor
/\cstring SearchSTRINGorstring, case insensitive
/jo[ha]n Searchjohnorjoan
/\< the Search the, theatre orthen
/the\> Searchtheorbreathe
/\< the\> Searchthe
/\< ¦.\> Search all words of 4 letters
/\/ Searchfredbut notalfredorfrederick
/fred\|joe Searchfredorjoe
/\<\d\d\d\d\> Search exactly 4 digits
/^\n\{3} Find 3 empty lines
:bufdo /searchstr/ Search in all open files
bufdo %s/something/somethingelse/g Searchsomethingin all the open buffers and replace it withsomethingelse


替换

:%s/old/new/g Replace all occurences ofoldbynewin file
:%s/onward/forward/gi Replace onward by forward, case unsensitive
:%s/old/new/gc Replace all occurences with confirmation
:2,35s/old/new/g Replace all occurences between lines 2 and 35
:5,$s/old/new/g Replace all occurences from line 5 to EOF
:%s/^/hello/g Replace the begining of each line byhello
:%s/$/Harry/g Replace the end of each line byHarry
:%s/onward/forward/gi Replaceonwardbyforward, case unsensitive
:%s/ *$//g Delete all white spaces
:g/string/d Delete all lines containingstring
:v/string/d Delete all lines containing which didn’t containstring
:s/Bill/Steve/ Replace the first occurence ofBillbyStevein current line
:s/Bill/Steve/g ReplaceBillbyStevein current line
:%s/Bill/Steve/g ReplaceBillbyStevein all the file
:%s/^M//g Delete DOS carriage returns (^M)
:%s/\r/\r/g Transform DOS carriage returns in returns
:%s#<[^>]\+>##g Delete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/ Delete lines which appears twice
Ctrl+a Increment number under the cursor
Ctrl+x Decrement number under cursor
ggVGg? Change text to Rot13


大小写

Vu Lowercase line
VU Uppercase line
g~~ Invert case
vEU Switch word to uppercase
vE~ Modify word case
ggguG Set all text to lowercase
gggUG Set all text to uppercase
:set ignorecase Ignore case in searches
:set smartcase Ignore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/g Sets first letter of each word to uppercase
:%s/\<./\l&/g Sets first letter of each word to lowercase
:%s/.*/\u& Sets first letter of each line to uppercase
:%s/.*/\l& Sets first letter of each line to lowercase


读写文件

:1,10 w outfile Saves lines 1 to 10 inoutfile
:1,10 w >> outfile Appends lines 1 to 10 tooutfile
:r infile Insert the content ofinfile
:23r infile Insert the content ofinfileunder line 23


文件浏览器

:e . Open integrated file explorer
:Sex Split window and open integrated file explorer
:Sex! Same as:Sexbut split window vertically
:browse e Graphical file explorer
:ls List buffers
:cd .. Move to parent directory
:args List files
:args *.php Open file list
:grep expression *.php Returns a list of .php files conteningexpression
gf Open file name under cursor


和 Unix 系统交互

:!pwd Execute thepwdunix command, then returns to Vi
!!pwd Execute thepwdunix command and insert output in file
:sh Temporary returns to Unix
$exit Retourns to Vi


对齐

:%!fmt Align all lines
!}fmt Align all lines at the current position
5!!fmt Align the next 5 lines


Tabs/Windows

:tabnew Creates a new tab
gt Show next tab
:tabfirst Show first tab
:tablast Show last tab
:tabm n(position) Rearrange tabs
:tabdo %s/foo/bar/g Execute a command in all tabs
:tab ball Puts all open files in tabs
:new abc.txt Editabc.txtin new window


分屏显示

:e filename Editfilenamein current window
:split filename Split the window and openfilename
ctrl-w up arrow Puts cursor in top window
ctrl-w ctrl-w Puts cursor in next window
ctrl-w_ Maximize current window vertically
ctrl-w| Maximize current window horizontally
ctrl-w= Gives the same size to all windows
10 ctrl-w+ Add 10 lines to current window
:vsplit file Split window vertically
:sview file Same as:splitin readonly mode
:hide Close current window
:­nly Close all windows, excepted current
:b 2 Open #2 in this window


自动完成

Ctrl+n Ctrl+p (in insert mode) Complete word
Ctrl+x Ctrl+l Complete line
:set dictionary=dict Definedictas a dictionnary
Ctrl+x Ctrl+k Complete with dictionnary


Marks

m {a-z} Marks current position as{a-z}
' {a-z} Move to position{a-z}
'' Move to previous position


缩写

:ab mail mail@provider.org Definemailas abbreviation ofmail@provider.org


文本缩进

:set autoindent Turn on auto-indent
:set smartindent Turn on intelligent auto-indent
:set shiftwidth=4 Defines 4 spaces as indent size
ctrl-t, ctrl-d Indent/un-indent in insert mode
>> Indent
<< Un-indent
=% Indent the code between parenthesis
1GVG= Indent the whole file


语法高亮

:syntax on Turn on syntax highlighting
:syntax off Turn off syntax highlighting
:set syntax=perl Force syntax highlighting

viacatswhocode


分享到:
评论

相关推荐

    ideavimrc:IntelliJ IDEA的VIM键位配置,快速配置USTC源

    标题"ideavimrc:IntelliJ IDEA的VIM键位配置,快速配置USTC源"表明这是一个关于在IntelliJ IDEA中集成VIM编辑器键位配置的资源,特别是涉及到一个名为`ideavimrc`的配置文件,以及如何快速设置USTC(中国科学技术...

    Linux-+-Apache-+-MySQL-+-PHP-(LAMP)环境搭建推荐教程.docx

    在IT行业中,LAMP(Linux, Apache, MySQL, PHP)是...完成以上步骤后,你就成功地搭建了一个基本的LAMP环境,可以在此基础上部署网站和应用程序。记得在生产环境中,还要考虑安全策略、性能优化、备份恢复等更多细节。

    Linux常用命令全集

    本资源"Linux常用命令全集"是中国IT实验室提供的一份详尽指南,涵盖了Linux终端中常用的命令,对于学习和掌握Linux操作至关重要。以下是其中一些关键知识点的详细解释: 1. **ls**:列出目录内容。`ls`命令用于查看...

    Gitlab安装手册2

    GitLab 是一个开源的版本控制系统,它集成了代码仓库、持续集成、问题跟踪和项目管理等功能,广泛用于软件开发团队协作。在本文中,我们将深入探讨如何在CentOS 7.x 64位系统上安装GitLab 11.1.0版本。 首先,确保...

    Gitlab安装手册1

    GitLab 是一个开源的版本控制系统,它提供了与GitHub类似的代码托管功能,同时也支持持续集成、代码审查、问题追踪等高级特性。本篇安装手册主要针对在CentOS 7.x 64位系统上安装GitLab 11.1.0的详细步骤。 首先,...

    网络监控软件zabbix的安装教程

    Zabbix是一款开源的企业级网络监控工具,主要用于对服务器、网络设备及应用系统等进行实时监控,包括但不限于CPU负载、内存使用情况、磁盘空间利用率以及网络流量等指标。通过Zabbix,用户能够及时发现并解决系统中...

    Linux学习笔记.pdf

    Linux是开源的操作系统,广泛应用在服务器、嵌入式设备以及个人电脑上。在Linux系统中,一切皆文件,包括硬件设备如串口和LED等,它们都被抽象为文件来管理和操作。这使得Linux具有高度的统一性和灵活性。 Ubuntu是...

    Linux 5中文显示乱码

    对于文本编辑器如Vim、Emacs,也需要确保它们支持UTF-8编码,可以在配置文件中设置。 9. **文件内容显示**: 如果是查看文件内容出现乱码,可以尝试用`iconv`转换文件编码,或者使用能指定编码的命令行工具如`cat...

    linux 学习的简单参考

    Linux学习的简单参考是针对初学者的一份指南,旨在帮助那些刚开始接触Linux编程的人更好地理解和掌握这个开源操作系统的基础知识。Linux作为一款强大的操作系统,广泛应用于服务器、嵌入式设备以及各种开发环境中。...

    anaconde-conda安装清华源.pdf

    Conda是Anaconda项目中的一个开源包管理系统和环境管理系统。它不仅适用于Anaconda本身,还可以用于任何Python或R环境。Conda可以跨平台运行,在多个环境中安装、更新、卸载软件包及其依赖项,同时支持Python和R语言...

    windows搭建gcc开发环境(msys2)

    MSYS2 (Minimal SYStem 2) 是一个用于Windows操作系统的开源软件包管理系统和构建系统。它基于MSYS (Minimal SYStem),并继承和发展了MSYS的功能。MSYS2的主要优势在于它的包管理系统Pacman,使得用户能够方便地安装...

    中国电大Linux教程

    Linux教程是中国电大提供的一套全面的教育资源,旨在帮助学习者快速理解和掌握Linux操作系统的核心概念、功能以及实际操作技巧。Linux是一种开源的操作系统,它的开放性使得开发者和用户可以自由地使用、修改和分发...

    小白入门linux入门到高级全套案例

    - **分区设置**: 在安装过程中,需要合理规划磁盘分区,如根分区(/)、交换分区(swap)等。 - **环境配置**: 安装完成后,还需进行必要的网络配置、用户管理等操作。 ##### 1.4 Linux学习技巧 - **学习资源**: 利用...

    Ubuntu 9.04 源更新,欢迎大家使用

    在Linux世界中,Ubuntu是一个非常受欢迎的开源操作系统,它的魅力在于其用户友好性和强大的软件库。标题中的"Ubuntu 9.04 源更新,欢迎大家使用"指的是Ubuntu 9.04版本的一个重要操作——更新系统软件源。Ubuntu的...

Global site tag (gtag.js) - Google Analytics