3

I would like to be able to edit and format blocks of comments (in different markup and programming languages) as if the commenting characters (generally in the beginning of the lines) are ignored.

For example, when i indent or unindent a block of "commented out" text, i would like only the "commented out" text to be moved right or left, not the whole comment:

" NOTE:
" this is a bug

~>

" NOTE:
"   this is a bug

not like this:

" NOTE:
  " this is a bug

Is there some plugin for making Vim behave like this? If not, what would be a simple solution to indent and unindent text in comments?

Other examples of operations i would like to have:

  • go to first nonblank in the line (like ^), but ignoring the commenting character,

  • delete to the beginning of line in Insert mode (like <C-u>), but ignoring the commenting character.

  • 1
    You could always get one of the commenter plugins (or define the commands yourself, easy for a specific language) and then define your keemapping as "uncomment, then move, then comment". Or, better yet "toggle comment, move, toggle comment" so the same binding works for non-commented lines as well. – PhilippFrank Oct 30 '15 at 11:19
  • I think, when commenting out text, the initial indent is taken into account (which looks like the correct way to comment out). So uncomment-indent-comment will probably not work. – Alexey Oct 30 '15 at 11:23
  • That would certainly depend on the plugin/mapping you're starting with. I mean, if you define nnoremap <leader>> 0x>>0i"<Esc>, you can indent your commented line the way you want. – PhilippFrank Oct 30 '15 at 11:56
  • 1
    @PhilippFrank, why not just use 0x>>0P? Then it will work for any one-character comment indicator (#, ", etc.) – Wildcard Feb 15 '16 at 20:27
  • @Alexey, you could use a mapping to something like ^a<Tab><Esc> and depend on your expandtab and tabwidth settings to be sensible. – Wildcard Feb 15 '16 at 20:28

Your Answer

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

Browse other questions tagged or ask your own question.