mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Merge pull request #12482 from pityonline/translate/12012
翻译完成 5 useful Vim plugins for developers.md
This commit is contained in:
commit
602db78ef0
@ -1,369 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (pityonline)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (5 useful Vim plugins for developers)
|
||||
[#]: via: (https://opensource.com/article/19/1/vim-plugins-developers)
|
||||
[#]: author: (Ricardo Gerardi https://opensource.com/users/rgerardi)
|
||||
|
||||
5 useful Vim plugins for developers
|
||||
======
|
||||
Expand Vim's capabilities and improve your workflow with these five plugins for writing code.
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/web_browser_desktop_devlopment_design_system_computer.jpg?itok=pfqRrJgh)
|
||||
|
||||
I have used [Vim][1] as a text editor for over 20 years, but about two years ago I decided to make it my primary text editor. I use Vim to write code, configuration files, blog articles, and pretty much everything I can do in plaintext. Vim has many great features and, once you get used to it, you become very productive.
|
||||
|
||||
I tend to use Vim's robust native capabilities for most of what I do, but there are a number of plugins developed by the open source community that extend Vim's capabilities, improve your workflow, and make you even more productive.
|
||||
|
||||
Following are five plugins that are useful when using Vim to write code in any programming language.
|
||||
|
||||
### 1. Auto Pairs
|
||||
|
||||
The [Auto Pairs][2] plugin helps insert and delete pairs of characters, such as brackets, parentheses, or quotation marks. This is very useful for writing code, since most programming languages use pairs of characters in their syntax—such as parentheses for function calls or quotation marks for string definitions.
|
||||
|
||||
In its most basic functionality, Auto Pairs inserts the corresponding closing character when you type an opening character. For example, if you enter a bracket **[** , Auto-Pairs automatically inserts the closing bracket **]**. Conversely, if you use the Backspace key to delete the opening bracket, Auto Pairs deletes the corresponding closing bracket.
|
||||
|
||||
If you have automatic indentation on, Auto Pairs inserts the paired character in the proper indented position when you press Return/Enter, saving you from finding the correct position and typing the required spaces or tabs.
|
||||
|
||||
Consider this Go code block for instance:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := true
|
||||
items := []string{"tv", "pc", "tablet"}
|
||||
|
||||
if x {
|
||||
for _, i := range items
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Inserting an opening curly brace **{** after **items** and pressing Return/Enter produces this result:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := true
|
||||
items := []string{"tv", "pc", "tablet"}
|
||||
|
||||
if x {
|
||||
for _, i := range items {
|
||||
| (cursor here)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Auto Pairs offers many other options (which you can read about on [GitHub][3]), but even these basic features will save time.
|
||||
|
||||
### 2. NERD Commenter
|
||||
|
||||
The [NERD Commenter][4] plugin adds code-commenting functions to Vim, similar to the ones found in an integrated development environment (IDE). With this plugin installed, you can select one or several lines of code and change them to comments with the press of a button.
|
||||
|
||||
NERD Commenter integrates with the standard Vim [filetype][5] plugin, so it understands several programming languages and uses the appropriate commenting characters for single or multi-line comments.
|
||||
|
||||
The easiest way to get started is by pressing **Leader+Space** to toggle the current line between commented and uncommented. The standard Vim Leader key is the **\** character.
|
||||
|
||||
In Visual mode, you can select multiple lines and toggle their status at the same time. NERD Commenter also understands counts, so you can provide a count n followed by the command to change n lines together.
|
||||
|
||||
Other useful features are the "Sexy Comment," triggered by **Leader+cs** , which creates a fancy comment block using the multi-line comment character. For example, consider this block of code:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := true
|
||||
items := []string{"tv", "pc", "tablet"}
|
||||
|
||||
if x {
|
||||
for _, i := range items {
|
||||
fmt.Println(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Selecting all the lines in **function main** and pressing **Leader+cs** results in the following comment block:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
/*
|
||||
* x := true
|
||||
* items := []string{"tv", "pc", "tablet"}
|
||||
*
|
||||
* if x {
|
||||
* for _, i := range items {
|
||||
* fmt.Println(i)
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
Since all the lines are commented in one block, you can uncomment the entire block by toggling any of the lines of the block with **Leader+Space**.
|
||||
|
||||
NERD Commenter is a must-have for any developer using Vim to write code.
|
||||
|
||||
### 3. VIM Surround
|
||||
|
||||
The [Vim Surround][6] plugin helps you "surround" existing text with pairs of characters (such as parentheses or quotation marks) or tags (such as HTML or XML tags). It's similar to Auto Pairs but, instead of working while you're inserting text, it's more useful when you're editing text.
|
||||
|
||||
For example, if you have the following sentence:
|
||||
|
||||
```
|
||||
"Vim plugins are awesome !"
|
||||
```
|
||||
|
||||
You can remove the quotation marks around the sentence by pressing the combination **ds"** while your cursor is anywhere between the quotation marks:
|
||||
|
||||
```
|
||||
Vim plugins are awesome !
|
||||
```
|
||||
|
||||
You can also change the double quotation marks to single quotation marks with the command **cs"'** :
|
||||
|
||||
```
|
||||
'Vim plugins are awesome !'
|
||||
```
|
||||
|
||||
Or replace them with brackets by pressing **cs'[**
|
||||
|
||||
```
|
||||
[ Vim plugins are awesome ! ]
|
||||
```
|
||||
|
||||
While it's a great help for text objects, this plugin really shines when working with HTML or XML tags. Consider the following HTML line:
|
||||
|
||||
```
|
||||
<p>Vim plugins are awesome !</p>
|
||||
```
|
||||
|
||||
You can emphasize the word "awesome" by pressing the combination **ysiw <em>** while the cursor is anywhere on that word:
|
||||
|
||||
```
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
```
|
||||
|
||||
Notice that the plugin is smart enough to use the proper closing tag **< /em>**.
|
||||
|
||||
Vim Surround can also indent text and add tags in their own lines using **ySS**. For example, if you have:
|
||||
|
||||
```
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
```
|
||||
|
||||
Add a **div** tag with this combination: **ySS <div class="normal">**, and notice that the paragraph line is indented automatically.
|
||||
|
||||
```
|
||||
<div class="normal">
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
Vim Surround has many other options. Give it a try—and consult [GitHub][7] for additional information.
|
||||
|
||||
### 4\. Vim Gitgutter
|
||||
|
||||
The [Vim Gitgutter][8] plugin is useful for anyone using Git for version control. It shows the output of **Git diff** as symbols in the "gutter"—the sign column where Vim presents additional information, such as line numbers. For example, consider the following as the committed version in 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 }
|
||||
```
|
||||
|
||||
After making some changes, Vim Gitgutter displays the following symbols in the 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 }
|
||||
```
|
||||
|
||||
The **-** symbol shows that a line was deleted between lines 5 and 6. The **~** symbol shows that line 8 was modified, and the symbol **+** shows that line 11 was added.
|
||||
|
||||
In addition, Vim Gitgutter allows you to navigate between "hunks"—individual changes made in the file—with **[c** and **]c** , or even stage individual hunks for commit by pressing **Leader+hs**.
|
||||
|
||||
This plugin gives you immediate visual feedback of changes, and it's a great addition to your toolbox if you use Git.
|
||||
|
||||
### 5\. VIM Fugitive
|
||||
|
||||
[Vim Fugitive][9] is another great plugin for anyone incorporating Git into the Vim workflow. It's a Git wrapper that allows you to execute Git commands directly from Vim and integrates with Vim's interface. This plugin has many features—check its [GitHub][10] page for more information.
|
||||
|
||||
Here's a basic Git workflow example using Vim Fugitive. Considering the changes we've made to the Go code block on section 4, you can use **git blame** by typing the command **:Gblame** :
|
||||
|
||||
```
|
||||
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 }
|
||||
```
|
||||
|
||||
You can see that lines 8 and 11 have not been committed. Check the repository status by typing **: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 opens a split window with the result of **git status**. You can stage a file for commit by pressing the **-** key on the line with the name of the file. You can reset the status by pressing **-** again. The message updates to reflect the new 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 }
|
||||
```
|
||||
|
||||
Now you can use the command **:Gcommit** to commit the changes. Vim Fugitive opens another split that allows you to enter a commit message:
|
||||
|
||||
```
|
||||
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 #
|
||||
```
|
||||
|
||||
Save the file with **:wq** to complete the commit:
|
||||
|
||||
```
|
||||
[master c3bf80f] vim-5plugins: Updated test1.go example file
|
||||
1 file changed, 2 insertions(+), 2 deletions(-)
|
||||
Press ENTER or type command to continue
|
||||
```
|
||||
|
||||
You can use **:Gstatus** again to see the result and **:Gpush** to update the remote repository with the new commit.
|
||||
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
If you like Vim Fugitive and want to learn more, the GitHub repository has links to screencasts showing additional functionality and workflows. Check it out!
|
||||
|
||||
### What's next?
|
||||
|
||||
These Vim plugins help developers write code in any programming language. There are two other categories of plugins to help developers: code-completion plugins and syntax-checker plugins. They are usually related to specific programming languages, so I will cover them in a follow-up article.
|
||||
|
||||
Do you have another Vim plugin you use when writing code? Please share it in the comments below.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/1/vim-plugins-developers
|
||||
|
||||
作者:[Ricardo Gerardi][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[pityonline](https://github.com/pityonline)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/rgerardi
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.vim.org/
|
||||
[2]: https://www.vim.org/scripts/script.php?script_id=3599
|
||||
[3]: https://github.com/jiangmiao/auto-pairs
|
||||
[4]: https://github.com/scrooloose/nerdcommenter
|
||||
[5]: http://vim.wikia.com/wiki/Filetype.vim
|
||||
[6]: https://www.vim.org/scripts/script.php?script_id=1697
|
||||
[7]: https://github.com/tpope/vim-surround
|
||||
[8]: https://github.com/airblade/vim-gitgutter
|
||||
[9]: https://www.vim.org/scripts/script.php?script_id=2975
|
||||
[10]: https://github.com/tpope/vim-fugitive
|
371
translated/tech/20190110 5 useful Vim plugins for developers.md
Normal file
371
translated/tech/20190110 5 useful Vim plugins for developers.md
Normal file
@ -0,0 +1,371 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (pityonline)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (5 useful Vim plugins for developers)
|
||||
[#]: via: (https://opensource.com/article/19/1/vim-plugins-developers)
|
||||
[#]: author: (Ricardo Gerardi https://opensource.com/users/rgerardi)
|
||||
|
||||
5 个好用的 Vim 插件
|
||||
======
|
||||
|
||||
通过这 5 个插件扩展 Vim 功能来提升你的编码效率。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/web_browser_desktop_devlopment_design_system_computer.jpg?itok=pfqRrJgh)
|
||||
|
||||
我用 Vim 已经超过 20 年了,两年前我决定把它作为我的首要文本编辑器。我用 Vim 来编写代码,配置文件,博客文章及其它任意可以用纯文本表达的东西。Vim 有很多超级棒的功能,一旦你适合了它,你的工作会变得非常高效。
|
||||
|
||||
在日常编辑工作中,我更倾向于使用 Vim 稳定的原生扩展,但开源社区对 Vim 开发了大量可以提升工作效率的插件。
|
||||
|
||||
以下列举 5 个非常好用的可以用于编写任意编程语言的插件。
|
||||
|
||||
### 1. Auto Pairs
|
||||
|
||||
[Auto Pairs][2] 插件可以帮助你插入和删除成对的文字,如花括号,圆括号或引用标记。这在编写代码时非常有用,因为很多编程语言都有成对标记的语法,就像圆括号用于函数调用,或引号用于字符串定义。
|
||||
|
||||
Auto Pairs 最基本的功能是在你输入一个左括号时会自动补全对应的另一半括号。比如,你输入了一个 `[`,它会自动帮你补充另一半 `]`。相反,如果你用退格键删除开头的一半括号,Auto Pairs 会删除另一半。
|
||||
|
||||
如果你设置了自动缩进,当你按下回车键时 Auto Pairs 会在恰当的缩进位置补全另一半括号,这比你找到放置另一半的位置并选择一个正确的括号要省劲多了。
|
||||
|
||||
例如下面这段代码:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := true
|
||||
items := []string{"tv", "pc", "tablet"}
|
||||
|
||||
if x {
|
||||
for _, i := range items
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
在 `items` 后面输入一个左花括号按下回车会产生下面的结果:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
x := true
|
||||
items := []string{"tv", "pc", "tablet"}
|
||||
|
||||
if x {
|
||||
for _, i := range items {
|
||||
| (cursor here)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Auto Pairs 提供了大量其它选项(你可以在 [GitHub][3] 上找到),但最基本的功能已经很让人省时间了。
|
||||
|
||||
### 2. NERD Commenter
|
||||
|
||||
[NERD Commenter][4] 插件增加了方便注释的功能,类似在 <ruby>IDE<rt>integrated development environment</rt></ruby> 中注释功能。有了这个插件,你可以一键注释单行或多行代码。
|
||||
|
||||
NERD Commenter 使用了标准的 Vim [filetype][5],所以它能理解一些编程语言并使用合适的方式来注释代码。
|
||||
|
||||
最易上手的方法是按 `Leader+Space` 组合键来开关当前行的注释。Vim 默认的 Leader 键是 `\`。
|
||||
|
||||
在<ruby>可视化模式<rt>Visual mode</rt></ruby>中,你可以选择多行一并注释。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)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
选择 `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` 组合键一次去掉这里所有的注释。
|
||||
|
||||
NERD Commenter 是任何使用 Vim 写代码的开发者都必装的插件。
|
||||
|
||||
### 3. VIM Surround
|
||||
|
||||
[Vim Surround][6] 插件可以帮你在现有文本中实现环绕插入成对的符号(如括号或双引号)或标签(如 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 文本中的<ruby>标签<rt>tag</rt></ruby>尤其在行。假如你有以下一行 HTML 代码:
|
||||
|
||||
```
|
||||
<p>Vim plugins are awesome !</p>
|
||||
```
|
||||
|
||||
当光标在 awesome 这个单词的任何位置时,你可以按 `ysiw <em>` 直接给它加上着重标签:
|
||||
|
||||
```
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
```
|
||||
|
||||
注意它聪明地加上了 `</em>` 闭合标签。
|
||||
|
||||
Vim Surround 也可以用 `ySS` 缩进文本。比如你有以下文本:
|
||||
|
||||
```
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
```
|
||||
|
||||
你可以用 `ySS <div class="normal">` 加上 `div` 标签,注意生成的段落是自动缩进的。
|
||||
|
||||
```
|
||||
<div class="normal">
|
||||
<p>Vim plugins are <em>awesome</em> !</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
Vim Surround 有很多其它选项,你可以参照 [GitHub][7] 上的说明尝试它们。
|
||||
|
||||
### 4. Vim Gitgutter
|
||||
|
||||
[Vim Gitgutter][8] 插件对使用 Git 作为版本控制工具的人来说非常有用。它会在 Vim 显示行号的列旁 `git diff` 的差异标记。假设你有如下已提交过的代码:
|
||||
|
||||
```
|
||||
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 会显示如下标记:
|
||||
|
||||
```
|
||||
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` 在多个有修改的块之间跳转,甚至可以用 `Leader+hs` 来暂存某个变更集。
|
||||
|
||||
这个插件提供了对变更的即时视觉反馈,如果你用 Git 的话,有了它简直是如虎添翼。
|
||||
|
||||
### 5. VIM Fugitive
|
||||
|
||||
[Vim Fugitive][9] 是另一个超棒的将 Git 工作流集成到 Vim 中的插件。它对 Git 做了一些封装,可以让你在 Vim 里直接执行 Git 命令并将结果集成在 Vim 界面里。这个插件有超多的特性,更多信息请访问它的 [GitHub][10] 项目页面。
|
||||
|
||||
这里有一个使用 Vim Fugitive 的基础 Git 工作流示例。设想我们已经对下面的 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 插件?请在评论区留言分享。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/1/vim-plugins-developers
|
||||
|
||||
作者:[Ricardo Gerardi][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[pityonline](https://github.com/pityonline)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/rgerardi
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.vim.org/
|
||||
[2]: https://www.vim.org/scripts/script.php?script_id=3599
|
||||
[3]: https://github.com/jiangmiao/auto-pairs
|
||||
[4]: https://github.com/scrooloose/nerdcommenter
|
||||
[5]: http://vim.wikia.com/wiki/Filetype.vim
|
||||
[6]: https://www.vim.org/scripts/script.php?script_id=1697
|
||||
[7]: https://github.com/tpope/vim-surround
|
||||
[8]: https://github.com/airblade/vim-gitgutter
|
||||
[9]: https://www.vim.org/scripts/script.php?script_id=2975
|
||||
[10]: https://github.com/tpope/vim-fugitive
|
Loading…
Reference in New Issue
Block a user