我已经使用 Vim 作为文本编辑器超过 20 年了,但大约两年前我决定将其作为我的主要文本编辑器。我使用 Vim 编写代码、配置文件、博客文章以及几乎所有我可以用纯文本完成的事情。Vim 有许多强大的功能,一旦你习惯了它,你就会变得非常高效。
我倾向于使用 Vim 强大的原生功能来完成我所做的大部分工作,但开源社区开发了许多插件,它们扩展了 Vim 的功能,改进了您的工作流程,并使您更加高效。
以下是五个在 Vim 中使用任何编程语言编写代码时有用的插件。
1. Auto Pairs
Auto Pairs 插件帮助插入和删除成对的字符,例如括号、圆括号或引号。这对于编写代码非常有用,因为大多数编程语言在其语法中使用成对的字符——例如用于函数调用的圆括号或用于字符串定义的引号。
在其最基本的功能中,当您键入一个开始字符时,Auto Pairs 会插入相应的结束字符。例如,如果您输入一个方括号 [,Auto-Pairs 会自动插入结束方括号 ]。相反,如果您使用退格键删除开始方括号,Auto Pairs 会删除相应的结束方括号。
如果您启用了自动缩进,当您按下 Return/Enter 键时,Auto Pairs 会在正确的缩进位置插入配对字符,从而使您免于查找正确的位置并键入所需的空格或制表符。
以这段 Go 代码块为例
package main
import "fmt"
func main() {
x := true
items := []string{"tv", "pc", "tablet"}
if x {
for _, i := range items
}
}
在 items 之后插入一个开始花括号 { 并按下 Return/Enter 键会产生以下结果
package main
import "fmt"
func main() {
x := true
items := []string{"tv", "pc", "tablet"}
if x {
for _, i := range items {
| (cursor here)
}
}
}
Auto Pairs 提供了许多其他选项(您可以在 GitHub 上阅读相关内容),但即使是这些基本功能也可以节省时间。
2. NERD Commenter
NERD Commenter 插件向 Vim 添加了代码注释功能,类似于集成开发环境 (IDE) 中找到的功能。安装此插件后,您可以选择一行或多行代码,然后按一下按钮即可将它们更改为注释。
NERD Commenter 与标准的 Vim filetype 插件集成,因此它了解多种编程语言,并为单行或多行注释使用适当的注释字符。
最简单的入门方法是按 Leader+Space 键在注释和非注释之间切换当前行。标准的 Vim Leader 键是 \ 字符。
在可视化模式下,您可以选择多行并同时切换其状态。NERD Commenter 还理解计数,因此您可以提供计数 n,后跟命令以一起更改 n 行。
其他有用的功能是“Sexy Comment”,由 Leader+cs 触发,它使用多行注释字符创建一个花哨的注释块。例如,考虑以下代码块
package main
import "fmt"
func main() {
x := true
items := []string{"tv", "pc", "tablet"}
if x {
for _, i := range items {
fmt.Println(i)
}
}
}
选择 function main 中的所有行并按下 Leader+cs 会产生以下注释块
package main
import "fmt"
func main() {
/*
* x := true
* items := []string{"tv", "pc", "tablet"}
*
* if x {
* for _, i := range items {
* fmt.Println(i)
* }
* }
*/
}
由于所有行都在一个块中注释掉,您可以通过使用 Leader+Space 切换块中的任何行来取消注释整个块。
对于任何使用 Vim 编写代码的开发人员来说,NERD Commenter 都是必备工具。
3. VIM Surround
Vim Surround 插件可帮助您用成对的字符(例如圆括号或引号)或标签(例如 HTML 或 XML 标签)“环绕”现有文本。它类似于 Auto Pairs,但它不是在您插入文本时工作,而是在您编辑文本时更有用。
例如,如果您有以下句子
"Vim plugins are awesome !"
您可以通过在引号之间任意位置按下组合键 ds" 来删除句子周围的引号
Vim plugins are awesome !
您还可以使用命令 cs"' 将双引号更改为单引号
'Vim plugins are awesome !'
或者通过按 cs'[ 将它们替换为方括号
[ Vim plugins are awesome ! ]
虽然它对文本对象非常有帮助,但当处理 HTML 或 XML 标签时,此插件真正大放异彩。考虑以下 HTML 行
<p>Vim plugins are awesome !</p>
您可以通过在光标位于单词“awesome”上的任意位置时按下组合键 ysiw<em> 来强调单词“awesome”
<p>Vim plugins are <em>awesome</em> !</p>
请注意,该插件足够智能,可以使用正确的结束标签 </em>。
Vim Surround 还可以使用 ySS 缩进文本并在其自己的行中添加标签。例如,如果您有
<p>Vim plugins are <em>awesome</em> !</p>
使用此组合添加 div 标签:ySS<div class="normal">,并注意段落行会自动缩进。
<div class="normal">
<p>Vim plugins are <em>awesome</em> !</p>
</div>
Vim Surround 还有许多其他选项。试一试——并查阅 GitHub 以获取更多信息。
4. Vim Gitgutter
对于任何使用 Git 进行版本控制的人来说,Vim Gitgutter 插件都非常有用。它将 Git diff 的输出显示为“gutter”中的符号——符号列,Vim 在其中显示附加信息,例如行号。例如,将以下内容视为 Git 中已提交的版本
1 package main
2
3 import "fmt"
4
5 func main() {
6 x := true
7 items := []string{"tv", "pc", "tablet"}
8
9 if x {
10 for _, i := range items {
11 fmt.Println(i)
12 }
13 }
14 }
在进行一些更改后,Vim Gitgutter 在 gutter 中显示以下符号
1 package main
2
3 import "fmt"
4
_ 5 func main() {
6 items := []string{"tv", "pc", "tablet"}
7
~ 8 if len(items) > 0 {
9 for _, i := range items {
10 fmt.Println(i)
+ 11 fmt.Println("------")
12 }
13 }
14 }
- 符号表示在第 5 行和第 6 行之间删除了一行。~ 符号表示第 8 行被修改,符号 + 表示添加了第 11 行。
此外,Vim Gitgutter 允许您使用 [c 和 ]c 在“hunks”(文件中进行的各个更改)之间导航,甚至可以通过按 Leader+hs 来暂存单个 hunk 以进行提交。
如果您使用 Git,此插件可为您提供更改的即时视觉反馈,并且是您工具箱的绝佳补充。
5. VIM Fugitive
Vim Fugitive 是另一个很棒的插件,适用于任何将 Git 融入 Vim 工作流程的人。它是一个 Git 包装器,允许您直接从 Vim 执行 Git 命令,并与 Vim 的界面集成。此插件具有许多功能——查看其 GitHub 页面以获取更多信息。
这是一个使用 Vim Fugitive 的基本 Git 工作流程示例。考虑到我们在第 4 节中对 Go 代码块所做的更改,您可以通过键入命令 :Gblame 来使用 git blame
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 1 package main
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 2
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 3 import "fmt"
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 4
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│_ 5 func main() {
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 6 items := []string{"tv", "pc", "tablet"}
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 7
00000000 (Not Committed Yet 2018-12-05 18:55:00 -0500)│~ 8 if len(items) > 0 {
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 9 for _, i := range items {
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 10 fmt.Println(i)
00000000 (Not Committed Yet 2018-12-05 18:55:00 -0500)│+ 11 fmt.Println("------")
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 12 }
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 13 }
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 14 }
您可以看到第 8 行和第 11 行尚未提交。通过键入 :Gstatus 检查存储库状态
1 # On branch master
2 # Your branch is up to date with 'origin/master'.
3 #
4 # Changes not staged for commit:
5 # (use "git add <file>..." to update what will be committed)
6 # (use "git checkout -- <file>..." to discard changes in working directory)
7 #
8 # modified: vim-5plugins/examples/test1.go
9 #
10 no changes added to commit (use "git add" and/or "git commit -a")
--------------------------------------------------------------------------------------------------------
1 package main
2
3 import "fmt"
4
_ 5 func main() {
6 items := []string{"tv", "pc", "tablet"}
7
~ 8 if len(items) > 0 {
9 for _, i := range items {
10 fmt.Println(i)
+ 11 fmt.Println("------")
12 }
13 }
14 }
Vim Fugitive 打开一个拆分窗口,其中包含 git status 的结果。您可以通过按文件名所在行上的 - 键来暂存文件以进行提交。您可以再次按 - 来重置状态。消息更新以反映新状态
1 # On branch master
2 # Your branch is up to date with 'origin/master'.
3 #
4 # Changes to be committed:
5 # (use "git reset HEAD <file>..." to unstage)
6 #
7 # modified: vim-5plugins/examples/test1.go
8 #
--------------------------------------------------------------------------------------------------------
1 package main
2
3 import "fmt"
4
_ 5 func main() {
6 items := []string{"tv", "pc", "tablet"}
7
~ 8 if len(items) > 0 {
9 for _, i := range items {
10 fmt.Println(i)
+ 11 fmt.Println("------")
12 }
13 }
14 }
现在您可以使用命令 :Gcommit 来提交更改。Vim Fugitive 打开另一个拆分窗口,您可以在其中输入提交消息
1 vim-5plugins: Updated test1.go example file
2 # Please enter the commit message for your changes. Lines starting
3 # with '#' will be ignored, and an empty message aborts the commit.
4 #
5 # On branch master
6 # Your branch is up to date with 'origin/master'.
7 #
8 # Changes to be committed:
9 # modified: vim-5plugins/examples/test1.go
10 #
使用 :wq 保存文件以完成提交
[master c3bf80f] vim-5plugins: Updated test1.go example file
1 file changed, 2 insertions(+), 2 deletions(-)
Press ENTER or type command to continue
您可以再次使用 :Gstatus 查看结果,并使用 :Gpush 将新提交更新到远程存储库。
1 # On branch master
2 # Your branch is ahead of 'origin/master' by 1 commit.
3 # (use "git push" to publish your local commits)
4 #
5 nothing to commit, working tree clean
如果您喜欢 Vim Fugitive 并想了解更多信息,GitHub 存储库中有指向截屏视频的链接,其中显示了其他功能和工作流程。去看看!
接下来是什么?
这些 Vim 插件可以帮助开发人员使用任何编程语言编写代码。还有另外两类插件可以帮助开发人员:代码完成插件和语法检查插件。它们通常与特定的编程语言相关,因此我将在后续文章中介绍它们。
您是否有在编写代码时使用的其他 Vim 插件?请在下面的评论中分享。
3 条评论