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

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

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


当前回答

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

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.

其他回答

:qall和:wqall关闭所有分屏。

ZZ(工作方式:wq)

关于光标的位置:我发现光标总是停留在屏幕中间是很酷的

set scrolloff=9999

: % s /搜索/替换,

您可以使用除/(斜杠)以外的其他字符分隔模式以进行替换。这样就不必转义文件路径的斜杠。

ma
move cursor down
:'a,.!program

这将获取从您设置的a标记(ma)到当前行(.)之间的所有文本,通过程序运行它,并用结果替换标记区域的内容。您甚至可以使用它来查看目录的内容(例如),通过创建一个空行,然后将光标放在该行上,

:.!ls

哦,你可以像这样为a-z(即ma)设置标记,以及

'a

会跳到你标记为"a "的位置

/向前搜索,和?重复向后搜索,而不需要补充搜索模式。

Groovy的东西。Vi被严重低估了。一旦你掌握了它的窍门,你就再也不想使用IDE提供的编辑器了。

选项卡式界面

除了分开的窗口,您还可以有选项卡窗口。在转义模式下,输入tabnew。您可以像这样打开多个选项卡。要在选项卡之间导航,输入tabn。这将移动到下一个选项卡。要移动到选项卡窗口,键入tabn2以移动第二个选项卡,依此类推。

要关闭制表符,输入tabc、tabclose或直接关闭。

如果你在终端模拟器(基本上是GUI中的终端)中,那么你可以尝试执行set mouse=a。完成后,您可以用鼠标在编辑器内单击。这也将帮助您通过单击在选项卡之间导航,并通过单击右侧的关闭按钮关闭。

对齐您的代码-完整的文件

只需在转义模式下输入G=gg。

折叠代码

假设您有一个完成的函数。您希望最小化(或折叠)这部分代码,以便可以释放一些空间并减少混乱。只需选择代码。然后输入折叠。

这将折叠代码。如果您想展开代码,只需转到那里,并键入zo。要再次折叠,输入zc。