3

I've been messing about with file type specific commands this evening (which is going to be very useful) and I'm trying to write a markdown comment command at the moment. I'm not sure how to go about this though.

So If I have the following text :

This is a bit 
of text that 
I'm going 
to comment 

I can run

visual line
highlight text to comment
shift i to insert at beginning
enter "[//]: # (

That does the start, and I think that all of this could be put to a key command. But then I'm left with the end.

This is how it should look :

[//]: # (This is a bit 
of text that 
I'm going 
to comment)

Happy to correct any wonky terminology, thanks!

edit - possible 'tag' suggestion of ftplugin :)

  • 2
    This is a common issue. Have you given any thoughts to using a commenting plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc? – Peter Rincker Mar 20 '15 at 4:45
  • cheers @PeterRincker. I didn't really think to install a plugin for this as I felt that it's probably a fairly simple task that one should be able to do. Perhaps a plugin would be more efficient I'm not sure, the answer given below appears to address it without plugins though – baxx Mar 20 '15 at 12:14
4

Vim creates marks for the beginning and the end of the last visual selection in the current buffer. You can use '< to move to the beginning and '> to move to end. In this way you can conveniently add any text you want before and after the visual selection. For example, I have a map for inserting a LaTeX underline command that spans the current visual selection, like so

vnoremap <buffer> <localleader>mu <ESC>`>a}<ESC>`<i\uline{<ESC>
  1. <ESC>: leave visual mode
  2. `>: go to end of last visual selection
  3. a}: insert closing curly bracket after
  4. <ESC>: leave insert mode
  5. `<: go to beginning of last visual selection
  6. i\uline{: insert the characters \uline that make up the underline command and the opening curly bracket
  7. <ESC>: leave insert mode

I can use this as viwee,mu to visually select current word and the two following and 'surround' them with the underline command (, is my local leader). This turns "Ja|g ska bara", sa Alfons Åberg. into "\uline{|Jag ska bara}", sa Alfons Åberg.

You should be able to adapt this for your situation. Perhaps try something like

<ESC>`>a)<ESC>`<i[//]: # (<ESC>
  • ace thanks @jjaderberg. I'm curious - why is vnoremap used instead of xnoremap? From the docs the only difference that I can tell is that vnoremap works in both visual and select, whereas xnoremap only works in visual. If I change the command to xnoremap instead it still seems to work, so I'm a little confused. Thanks :) – baxx Mar 20 '15 at 12:29
  • Just to add - vnoremap <buffer> <Leader>ma <S-V>>a$$<ESC><i$$<ESC> I expected that to let me convert a line in LaTeX into a math line, but It doesn't work. I tried using vnoremap <Leader>ma <S-V>>a$$<ESC><i$$<ESC> instead, but that didn't work either (I thought that perhaps the information was selected yet so it wasn't in the buffer or something). I'm not sure why it won't carry out the shift+V then work as before... ho hum – baxx Mar 20 '15 at 12:45
  • After <S-V> you are in visual mode, but '> is a motion in normal mode. Return to normal mode first (<ESC> before '>). You can use o and O (mnemonic: "other") to jump between the ends in visual mode. But in this case visual mode is unnecessary, just use insert-at-beginning-of-line and -at-end-of-line: I$<ESC>A$<ESC>. Also, look at Tim Pope's Surround plugin, it is good. – jjaderberg Mar 20 '15 at 13:27
  • 1
    I use this technique too. It's important to jump to the end of the range first: adding text at the beginning first will move the end of the range, but it will not move the `` `>`` marker. – glenn jackman Mar 20 '15 at 15:24
3

Adding/removing/toggling comments is a common request. As such there are a few approaches

Commenting Plugins

Using a plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc make managing comments for all sorts of filetypes much easier as well has giving a consistent interface.

For example commentary use gcc to toggle an entire line or use gc followed by a motion to toggle out an entire motion. e.g. gcip to toggle a paragraph.

Surround.vim

Using Tim Pope's surround.vim plugin can make surrounding with quotes, parens, braces, and tags pairs very simple. It can also be customized so you can add your own "surroundings".

Add the following to ~/.vim/after/ftplugin/markdown.vim:

let b:surround_{char2nr('#')} = "[//]: # (\r)"

Now you can select your text via v and then surround via S#.

For more help with surround see:

:h surround
:h surround-customizing

Change and paste

Example assuming you have text selected via v:

c[//]: # (<c-r>")<esc>

This works because c by default will "cut" the text to the default/unnamed register. Using <c-r> we can recall a register while in insert mode. The register in the example above is " aka unnamed register.

If you do this often enough it might be worth creating a mapping:

xnoremap gc c[//]: # (<c-r>")<esc>

Thoughts

Personally I would install a commenting plugin because it can be such a common operation and creating new mappings for all the filetypes you come across can get annoying quickly.

I would also recommend looking into surround.vim which feels like a very natural extension of Vim. Install repeat.vim to bring back the glory of . when using certain plugins.

  • thanks @Peter Rincker, I haven't used plugins afaik so I'll have to try and get my head around them at some point (by the looks of things!). The comments are working nicely so far, looking forward to adding more. – baxx Mar 20 '15 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.