我一直在Vim中学习新的命令,但我相信每个人都偶尔会学习一些新东西。我最近才知道:

Zz, zt, zb -位置光标在中间,顶部或底部的屏幕

还有什么其他有用或优雅的命令是你希望很久以前就学会的?


当前回答

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.

其他回答

:shell从Vim启动shell控制台。例如,当您想在不退出Vim的情况下测试一个脚本时,它很有用。当你完成shell控制台时,只需点击^d,然后你回到Vim和你编辑的文件。

我为我的一个朋友创建了我最常用的命令的参考:

select                                   v
select row(s)                            SHIFT + v
select blocks (columns)                  CTRL  + v
indent selected text                     >
unindent selected text                   <
list buffers                             :ls
open buffer                              :bN (N = buffer number)
print                                    :hardcopy
open a file                              :e /path/to/file.txt
                                         :e C:\Path\To\File.txt
sort selected rows                       :sort
search for word under cursor             *
open file under cursor                   gf
  (absolute path or relative)
format selected code                     =
select contents of entire file           ggVG
convert selected text to uppercase       U
convert selected text to lowercase       u
invert case of selected text             ~
convert tabs to spaces                   :retab
start recording a macro                  qX (X = key to assign macro to)
stop recording a macro                   q
playback macro                           @X (X = key macro was assigned to)
replay previously played macro *         @@
auto-complete a word you are typing **   CTRL + n
bookmark current place in file           mX (X = key to assign bookmark to)
jump to bookmark                         `X (X = key bookmark was assigned to
                                             ` = back tick/tilde key)
show all bookmarks                       :marks
delete a bookmark                        :delm X (X = key bookmark to delete)
delete all bookmarks                     :delm!
split screen horizontally                :split
split screen vertically                  :vsplit
navigating split screens                 CTRL + w + j = move down a screen
                                         CTRL + w + k = move up a screen
                                         CTRL + w + h = move left a screen
                                         CTRL + w + l = move right a screen
close all other split screens            :only

*  - As with other commands in vi, you can playback a macro any number of times.
     The following command would playback the macro assigned to the key `w' 100
     times: 100@w

** - Vim uses words that exist in your current buffer and any other buffer you may have open for auto-complete suggestions.

我最近学到的令人惊叹的技巧是一种进行复杂搜索和替换的方法。在过去,我经常有一个非常复杂的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

^y将复制光标上方的字符。

^r^w以命令模式将单词粘贴到光标下。

它在使用grep或replace命令时非常有用。