5

Is there a way to edit comments so that they will all be aligned without having to align them manually with tab?

i.e., How can I get something like that?

some text          %comment
some more text     %another comment
&c.                %=etc.

without manually adjusting each comment?

1 Answer 1

5

You might be interested by the tabular plugin it allows to align text based on patterns.

In your case if you have

some text  %comment
some more text %another comment
&c.  %=etc.

with your cursor on the first line you can use 3V to select the lines you want to treat and then :Tabular /% and tabular will automatically indent the text like that:

some text          %comment
some more text     %another comment
&c.                %=etc.

The plugin is pretty powerful and can be used to do much more complex operations.

Edit As jjaderberg pointed it out in this case you don't need to select the text: Tabular is pretty clever and can "guess" how to format the text. For example you can use :Tabularize (without selection neither pattern) and the plugin will still align the comments.

Also an interesting mapping to add to your .vimrc is:

vnoremap T :Tabular /

This way you can visually select a part of text and use T to easily start tabular.

Edit 2 I was looking around to find fun stuff to do with tabular and I just found out a nice Tim Pope's gist which could be interesting here:

inoremap <silent> %   %<Esc>:call <SID>align('%')<CR>a

function! s:align(aa)
  let p = '^.*\s'.a:aa.'\s.*$'
  if exists(':Tabularize') && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
    let column = strlen(substitute(getline('.')[0:col('.')],'[^'.a:aa.']','','g'))
    let position = strlen(matchstr(getline('.')[0:col('.')],'.*'.a:aa.':\s*\zs.*'))
    exec 'Tabularize/'.a:aa.'/l1'
    normal! 0
    call search(repeat('[^'.a:aa.']*'.a:aa,column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
  endif
endfunction

It maps % so that when you write several consecutive lines containing a %, the comments will be automatically aligned on the fly. And when you write a single line with % it will only be added without more indent.

Note that a possible improvement would be to define the mapping according to the file time so that the correct comment character is mapped for each language.

3
  • 1
    +1 In this case you can probably omit the 3V as Tabular usually guesses correctly which selection of text you want to operate on.
    – jjaderberg
    Commented Sep 22, 2015 at 23:25
  • @jjaderberg You're right I edited my answer. Thanks :-)
    – statox
    Commented Sep 22, 2015 at 23:35
  • 1
    You might also be interested in vim-easy-align. I like particularly the LiveEasyAlign command, which allows real time customization of the align command. This is useful for more advanced cases. Commented Sep 23, 2015 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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