注释多行Vimrc的功能 [英] Function to Comment Multiple Lines Vimrc

查看:73
本文介绍了注释多行Vimrc的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前喜欢这种在vim中注释多行的解决方案:

So I currently like this solution to commenting multiple lines in vim:


  1. CTRL-v (进入可视块模式)

  2. 选择要注释的行

  3. Shift -i (进入插入模式)

  4. 键入您的语言使用的注释字符

  5. ESC ESC (按两次退出键可使结果显示更快)

  1. Press CTRL-v (to go into Visual Block mode)
  2. Select the lines you want to comment
  3. Press Shift-i (to go into Insert mode)
  4. Type whatever comment characters your language uses
  5. Press ESC ESC (pressing the escape key twice makes the results appear faster)

但是我想一些帮助将这些步骤映射到我的vimrc文件中。
我目前使用以下注释行:

But I would like some help mapping these steps into my vimrc file. I currently use the following to comment lines out:

vnoremap ;/ <C-v>0I// <ESC>

对于那些想要解释命令功能的人:

For those who want an explanation of what the command does:

您基本上在Visual模式下键入; / 即可使用此功能(Visual,Visual Line和Visual Block模式自< C-v> 部分将您强制进入Visual Block模式,这是正确的)。

You basically type ;/ when you're in Visual mode to use this (Visual, Visual Line, and Visual Block mode all work since the <C-v> part forces you into Visual Block mode, which is correct).

0I 部分将使您在行首进入插入模式。

The 0I part will put you in Insert mode at the beginning of the line.

//< ESC> 部分将插入注释字符 // ,然后让您回到普通模式。

The // <ESC> part will insert the comment characters // and put you back into Normal mode.

我需要帮助的部分是取消注释。如何在vimrc中编写一个基本上可以切换 // 字符的函数?

The part I need help with is uncommenting the lines. How do I write a function in my vimrc that will basically let me toggle the // characters?

理想情况下,解决方案将涉及以下内容:

Ideally, the solution would involve the following:


  1. 选择行

  2. ; /

  3. 如果没有 // 字符,然后将其插入

  4. 如果有 // 个字符,则将其删除
  5. >
  1. Selecting the lines
  2. Pressing ;/
  3. If there are NO // characters then it will insert them
  4. If there ARE // characters then it will remove them


推荐答案

将此内容放入您的 .vimrc 文件:

Put this in your .vimrc file:

vnoremap <silent> ;/ :call ToggleComment()<cr>

function! ToggleComment()
        if matchstr(getline(line(".")),'^\s*\/\/.*$') == ''
                :execute "s:^://:"
        else
                :execute "s:^\s*//::"
        endif
endfunction

这篇关于注释多行Vimrc的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆