Take any long comment in paragraph form:

  1 // This is a comment that is really long because it is describing something
  2 // worth being verbose about, possibly at the beginning of the program (like a
  3 // preamble).  It is desirable to keep the width limited to 80-characters,
  4 // conveniently done by typing the whole comment out on one line, selecting
  5 // with SHIFT+V and executing gq.

As mentioned, the paragraph is made to span several lines with Vim's gq so the width doesn't run off the edge of certain UI's (such as the StackExchange code block).

My question is: how can the paragraph-comment be edited in Vim? I have been:

  1. Deleting the comment slashes with Ctrlv,

  2. selecting the text with Shiftv,

  3. joining with Shiftj,

  4. editing and using gq to join them again.

Is there a better (i.e., faster, built-in) way to do this?

  • May be, you can use a mapping or a command for that. (Already you are using just 6 keys. If we dont consider shift as a key, its just four keys. I don't think any short cut will exist with three or two keys :D) shall I add a function or command for that? – SibiCoder Jul 5 '16 at 14:32
  • 1
    set fo+=cq, and / or use a smart text formatter such as Par. See :h fo-table for details. – Sato Katsura Jul 5 '16 at 14:49
  • 1
    @SibiCoder, I know Vim is very flexible (and its user base extremely talented) such that a home-brewed macro or function can be put together. However, I am asking if anyone is familiar with a built-in feature, which I have no way of searching for. – user1717828 Jul 5 '16 at 15:56
  • I'm curious, why you are looking for a built-in solution? This could be easily solved by custom macros/functions, with or without the aid of Vim plugins. – mMontu Jul 5 '16 at 16:10
  • @mMontu, I used to subscribe to all sorts of custom functionality: Web browser behavior, text editor shortcuts, window manager behavior, you name it. Every time I switched environments I would reimplement these home brewed solutions by doing a dance with configuration files, Bash scripts and other customization tools. After many years of this, I realized it is far better to become an expert with stock tools available in every environment than custom shims I put in myself to handle unique issues. – user1717828 Jul 5 '16 at 19:51

Why not just use a macro or a mapping? Here's what I whipped up:

nnoremap <leader>u {j<C-v>}k^ldgvVJ0i//<esc>

Think of leader+u as "(U)format". Of course, you can choose different keys if you like.

Here's how it works:

{j<C-v>                    "Start a visual block at the top of the paragraph
       }k^l                "Move to the end of the paragraph, to select all the '//'
           d               "Delete it
            gvV            "Reselect the whole paragraph
               J           "And join it
                0i//<esc>  "Add a comment back in.

This is essentially the same process as you described, but easier and faster. Best part of this method? It's faster and builtin. =)

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.