0

I'm new(bie) in vim. I've got the following mapping to comment my python code :

nmap cc 0i#<ESC>

I would like to have the same mapping to uncomment a line. I think I need an function to check the first character of the line. Do you know how I could do the tricks ?

thanks.

edit : It's not the same as that question , I wonder how to do that without plugin.

Community
  • 1
  • 1
Gaut
  • 1,255
  • 2
  • 15
  • 33
  • 1
    Please [find a good comment plugin](http://vimawesome.com/?q=comment). Also be aware that `cc` is already used by Vim to change an entire line (Same as `S`). See `:h cc`. – Peter Rincker Jun 03 '15 at 16:30

2 Answers2

2

You shouldn't attempt to implement this (poorly) yourself; this is a solved problem, and you can choose from several good plugins. See Comment Lines according to a given filetype for a list of plugins.


As a learning experience, attempting a mapping is fine, though. Here's one approach that uses :help map-expr to check the line for the existence of a comment first:

nnoremap <expr> cc getline('.') =~# '^#' ? '0x' : '0i#<ESC>'

PS: You should use :noremap; it makes the mapping immune to remapping and recursion.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

There is this plugin. It's very good and he works for many languages.

Jean
  • 71
  • 9