在 Vim 中移动
目录
设置您的号码
角色导航
文字导航
匹配导航
阻止导航
文件行导航
当前行导航
屏幕导航
滚动屏幕
功能/模块导航
搜索导航
导航通用技巧
如何应用这份速查表
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
关注@learnvim获取更多 Vim 使用技巧!
能够在文件中自由移动是 Vim 的一项必备技能。
以下是我在文件中移动光标时常用的命令列表。我还会分享一些高效移动光标的技巧,以及如何应用这些技巧。
目录
设置您的号码
开始之前,我发现最好在 vim 中number设置好相关relativenumber选项。你可以通过运行以下命令:set relativenumber number或将以下内容添加到 vim 中.vimrc:
set relativenumber
set number
这表示我当前的位置,并显示我当前位置上方/下方的n行。
角色导航
浏览文本:
h left
j down
k up
l right
文字导航
w move forward to the beginning of next word
W move forward to the beginning of next WORD*
e move forward one word to the end of next word
E move forward one word to the end of next WORD
b move backward to beginning of previous word
B move backward to beginning of previous WORD
ge move backward to end of previous word
gE move backward to end of previous WORD
*定义:h WORD:一个单词由一系列非空白字符组成,字符之间用空格分隔。空行也被视为一个单词。
匹配导航
% Navigate to another match, usually works for (), [], {}
(启用matchit.vim后),我们现在可以在方法内部进行切换。
阻止导航
{ Jump to prev paragraph
} jump to next paragraph
( Jump to prev sentence
) Jump to next sentence
文件行导航
gg go to first line
G go to last line
nG go to line n
n% go to n% in file
`` go to last jump position
顺便说一下,你可以用句点查看文件中的行数CTRL-G。
当前行导航
0 go to first character of current line
^ go to first nonblank char of current line
n| go column n of current line
g_ go to last non-blank char of current line
$ go to last char of current line
屏幕导航
H go to top of screen
M go to medium screen
L go to bottom of screen
nH go n line from top
nL go n line from bottom
滚动屏幕
Ctrl-e scroll down lines
Ctrl-d scroll down half screen
Ctrl-f scroll down whole screen
Ctrl-y scroll up lines
Ctrl-u scroll up half screen
Ctrl-b scroll up whole screen
功能/模块导航
]m go to the start of next method
[m go to the start of previous method
]M go to the end of next method
[M go to the end of previous method
]] go to next class/ module
[[ go to previous class/module
有关功能/模块导航的更多信息,Drew Neil 的这段视频非常有帮助!
搜索导航
/ Search forward for a match
? Search backward for a match
n Repeat last search (same direction as previous search)
N Repeat last search (opposite direction as previous search)
f Search forward for a match in the same line
F Search backward for a match in the same line
t Search forward for a match in the same line, stopping before match
T Search backward for a match in the same line, stopping before match
; Repeat last search in the same line
, Repeat last search in the same line backwards
* Quickly search for word under cursor forward
# Quickly search for word under cursor backward
呼!上面有些东西我几乎每次都用,有些我很少用,但知道它们的存在总是好的。找到适合你的那一个吧。
导航通用技巧
在 Vim 中移动光标时,观察文件内部的规律非常重要。Vim 的移动方式让我想起绘画,先用你所能想到的最大笔触开始。
- 你要找的单词是否位于文件长度的一半之后?从开头开始,
50%然后向下移动j。 - 是在第 73 行吗?太棒了,直接跳转到那里!
73G - 文本是不是
}}}在前面三段?如果是,请直接阅读,不要乱按j按钮。 - 你知道它靠近一个独特的关键词吗
const uniqueKeyword = 'UNIQUE'?使用搜索跳转/uniqueKeyword
如果到达目标行时,目标词仍然很靠近句末,你可以用括号 ( w) 来接近它——或者,如果可能的话,寻找目标词周围的特殊字母。例如,如果句子是:
I ate a fried fish next to a zebra today
假设你的光标位于字母“I”上。你想编辑“zebra”之前的“a”。首先,使用“查找 z”(fz因为“z”不是常用字母),然后使用回溯b。直接输入fzb(3 个按键)比先找到末尾再回溯(4 个按键)更快$bbb,也比(7 个按键)更快wwwwwww。我们最不想做的就是按很多个l“s”。你能想到一种用更少按键就能完成操作的模式吗?这正是 Vim 的乐趣所在!
想要提高球技,不妨花几天时间玩玩Vimgolf。相信我,你会学到很多东西。
如何应用这份速查表
你可能会想:“哎呀,这么多啊!我怎么可能全部记住呢?”
以下是我个人的建议:
- 不要试图一次性记住所有内容。
- 今天学五六个,然后每天用一个星期!别着急——何必着急呢?
- 下周在运用之前学过的知识的同时,再学习 5-6 个新知识。
- 先尝试 vimgolf,看看自己能不能打好,然后再向其他高尔夫球手学习。
- 我花了将近一年时间才学会以上内容。直到现在,我每周仍然能学到关于 Vim 的新知识。学习 Vim 是一项长期投入。
- 学会利用帮助
:h。
如果你是 Vim 新手,感觉不知从何入手,以下是一些你应该首先学习的命令:
基本导航:
h
j
k
l
词语导航:
w
b
跳转到行n
nG // ex: 1G, G, 73G
搜索:
/
?
n
这10个技巧应该能让你在文件处理中游刃有余。然后慢慢地,再增加更多技巧。
感谢阅读!非常感谢您坚持读到这里。如果您有任何建议或疑问,或者发现了任何错误😅,欢迎在下方留言!
文章来源:https://dev.to/iggredible/vim-cheatsheet-to-move-around-in-a-file-plus-tips-to-use-them-bme
