vim 删除多行_让Vim变成你的神器,你还缺这几大插件

开源最前线(ID:OpenSourceTop) 猿妹整编

链接:https://opensource.com/article/19/1/vim-plugins-developers

我用Vim作为文本编辑器已有20多年了,但大约两年前我决定将它作为我的主要文本编辑器。我使用Vim编写代码,配置文件,博客文章等。Vim有很多很棒的功能,一旦你习惯了它,你就会变得非常高效。

一直以来,我都更倾向于使用Vim强大的原生功能,但是有许多开源的Vim插件,可以改善你的工作流程,大大提高你的工作效率,以下是我个人认为最好用的5个Vim插件

1. Auto Pairs

Auto pair插件可以帮助插入和删除对字符,如方括号,圆括号或引号。这对于编写代码帮助很大,因为大多数编程语言在其语法中都要用到字符对 - 例如函数调用的括号或字符串定义的引号。

在其最基本的功能中,会自动对输入的起始字符插入相应的结束字符。例如,如果输入左括号[,Auto-Pairs会自动插入右括号]。相反,如果使用Backspace键删除左括号,Auto Pairs将删除相应的右括号。

如果您启用了自动缩进,则当你按Return / Enter键时,自动对将成对的字符插入正确的缩进位置,从而大大节省你的时间。

以如下这个Go代码块为例:

package main

import "fmt"

func main() {
    x := true
    items := []string{"tv", "pc", "tablet"}

    if x { 
        for _, i := range items
    } 
}

在项目后插入左大括号{,并按回车/回车会产生如下结果:

package main

import "fmt"

func main() {
    x := true
    items := []string{"tv", "pc", "tablet"}

    if x {
        for _, i := range items  {
            | (cursor here)
        }
    }
}

更多帮助你节约时间的功能,你可以通过查看GitHub项目页(GitHub地址:https://github.com/jiangmiao/auto-pairs)

2. NERD Commenter

NERD Commenter插件向Vim添加了代码注释功能,类似于集成开发环境(IDE)中的代码注释功能。安装此插件后,你可以选择一行或几行代码,并快速将其更改为注释。

NERD Commenter集成了标准的Vim filetype插件,因此它可以理解多种编程语言,最简单的入门方法是按Leader + Space切换当前已注释和未注释的行。标准的Vim Leader键是\字符。

在视图模式下,可以选择多行同时切换它们的状态,其他一些有用的功能就是由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编写代码的开发人员必备的。(GitHub地址:https://github.com/scrooloose/nerdcommenter)

3. VIM Surround

vim-surround一款强大的更改成对符号的Vim插件,它类似于Auto pair,只不过它在插入文本时不工作,在编辑文本时更有用。例如:

"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>

当光标位于该单词的任何位置时,您可以通过按ysiw 组合来强调“awesome”这个单词 :

<p>Vim plugins are <em>awesomeem> !p>

Vim Surround还有许多强大的功能,你依然可以从GitHub获得。(GitHub地址:https://github.com/tpope/vim-surround)

4. Vim Gitgutter

如果你使用Git版本控制,那么Vim Gitgutter对你的帮助一定很大,它可以显示git diff状态,例如,将下面的代码作为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行。

5. VIM Fugitive

对于将Git纳入Vim工作流程的任何人来说,Vim Fugitive是另一个很棒的插件。它是一个Git包装器,允许你直接在Vim执行Git命令并与Vim的界面集成。

下面是一个使用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 ..." to update what will be committed)
  6 #   (use "git checkout -- ..." 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 ..." 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提交更改。

  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

如果你想了解更多,可查看GitHub详情页(GitHub地址:https://github.com/tpope/vim-fugitive)


●编号513,输入编号直达本文

●输入m获取文章目录

e817b8ea0bd8fde59f3e537668a20c27.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值