How to map to toggle comments in vim?

后端 未结 4 1908
天涯浪人
天涯浪人 2020-12-25 10:43

I use vim under iterm2. I\'m using the NERDCommenter plugin, and I\'d like to use Ctrl+/ to toggle comments (Trying to switch from Idea/Eclipse to vim). This is my mapping i

相关标签:
4条回答
  • 2020-12-25 10:53

    Just to sum up the information from other answers. For me (there might be a difference due to the fact that I'm using neovim) <C-/> works fine on Windows, but I need to use <C-_> on Linux:

    if has('win32')
      nmap <C-/> <leader>c<Space>
      vmap <C-/> <leader>c<Space>
    else
      nmap <C-_> <leader>c<Space>
      vmap <C-_> <leader>c<Space>
    endif
    
    0 讨论(0)
  • 2020-12-25 10:57

    For some reason, vim registers <C-/> as <C-_> (you can see it in insert mode using <C-v><C-/>). It can be the terminal or a historical design thing that terminal apps have to suffer.

    And Gvim doesn't even try to recognize <C-/>. Sees it as single /.

    0 讨论(0)
  • 2020-12-25 11:05

    If you're using iTerm2 + vim, maybe the following steps can help you:

    1. Add following code to your .vimrc file.

      map ,cc <plug>NERDCommenterToggle

      or if you have defined your <leader> as ,

      map <leader>cc <plug>NERDCommenterToggle

    2. Check if you can use ,cc to toggle comments in vim

    3. Open iTerm2 -> Preferences -> Keys, click the + button

    4. Select Send Text with "vim" Special Chars, enter ,cc, like this.

    5. Now you can use C-/ to toggle comments in vim.

    0 讨论(0)
  • 2020-12-25 11:14

    Here is how you can do it regaining the selection if you are in visual mode:

    nmap <C-_>   <Plug>NERDCommenterToggle
    vmap <C-_>   <Plug>NERDCommenterToggle<CR>gv
    
    0 讨论(0)
提交回复
热议问题