我一直在Vim中学习新的命令,但我相信每个人都偶尔会学习一些新东西。我最近才知道:
Zz, zt, zb -位置光标在中间,顶部或底部的屏幕
还有什么其他有用或优雅的命令是你希望很久以前就学会的?
我一直在Vim中学习新的命令,但我相信每个人都偶尔会学习一些新东西。我最近才知道:
Zz, zt, zb -位置光标在中间,顶部或底部的屏幕
还有什么其他有用或优雅的命令是你希望很久以前就学会的?
当前回答
我最近学到的令人惊叹的技巧是一种进行复杂搜索和替换的方法。在过去,我经常有一个非常复杂的regexp来进行替换,但它不起作用。有一个更好的方法:
:set incsearch " I have this in .vimrc
/my complicated regexp " Highlighted as you enter characters
:%s//replace with this/ " You don't have to type it again
这里的“技巧”(找不到更好的词)是使用搜索来创建regexp(在输入字符时'incsearch'突出显示它),然后在替换中使用空模式:空模式默认为最后一个搜索模式。
例子:
/blue\(\d\+\)
:%s//red\1/
等价于:
:%s/blue\(\d\+\)/red\1/
See:
:help 'incsearch'
:help :substitute
其他回答
Don't press Esc ever. See this answer to learn why. As mentioned above, Ctrl + C is a better alternative. I strongly suggest mapping your Caps Lock key to escape. If you're editing a Ctags compatible language, using a tags file and :ta, Ctrl + ], etc. is a great way to navigate the code, even across multiple files. Also, Ctrl + N and Ctrl + P completion using the tags file is a great way to cut down on keystrokes. If you're editing a line that is wrapped because it's wider than your buffer, you can move up/down using gk and gj. Try to focus on effective use of the motion commands before you learn bad habits. Things like using 'dt' or 'd3w' instead of pressing x a bunch of times. Basically, any time that you find yourself pressing the same key repeatedly, there's probably a better/faster/more concise way of accomplishing the same thing.
进一步使用xcramps的建议,我无法告诉你我用了多少次:
: % !排序
对文件中的项列表进行排序。
细节:
:范围!命令
将在指定的行范围上执行shell命令。一个范围通常被指定为开始,结束
例子: 1,3指定前3行 'a,'b选择书签a和b之间的文本 ,$选择整个文档(。=第一线;$ =最后一行) %是.,$的快捷方式,还可以选择整个文档。
您可以随意混合和匹配数字、书签、.和$。
我真的希望我知道你可以使用Ctrl+C而不是Esc来切换插入模式。这对我来说是一个真正的生产力提升。
使用选项卡打开多个文件
:tabe filepath
在打开的文件之间导航
gt and gT or :tabn and :tabp
保存打开的会话,以便稍后可以返回打开的文件列表
:mksession session_file_name.vim
打开已创建的会话
vim -S session_file_name.vim
立即关闭所有文件
:qa
这是我最近学到的另一个命令
autocmd
它允许你在一个事件上运行命令,所以你可以在保存文件时使用如下命令:
:autocmd BufWritePost *.cpp :make
Q <字母> -记录一个宏。
and
@<相同字母> -回放。
这些是到目前为止在Vim中最有用的命令,因为你可以让计算机为你做很多工作,你甚至不需要写一个程序或任何东西。