Wikia

Vim Tips Wiki

Simple text alignment

Talk0
1,610pages on
this wiki
Revision as of 16:30, December 20, 2012 by 82.33.218.50 (Talk)

Tip 319 Printable Monobook Previous Next

created August 23, 2002 · complexity basic · author Jahagirdar Vijayvithal S · version 5.7


Some times one would like to reformat text like:

a=1;
foo=2;
longstring=1;
c=2

to

a          =1;
foo        =2;
longstring =1;
c          =2;

This is how we achieve it

0f=20i<Space><Esc>020lvf=hx

This is what it does

0 goes to first column
f= finds next occurrence of = on current line
20i<Space><Esc> inserts 20 spaces before =
0 goes back to first column
20l forward 20 column
vf=hx deletes everything up to the = sign

Comments

Any lining up, alignment, etc - just leave it to Dr. Chip Campbell's Align.vim (search for it here)


I see a BIG drawback! What will it do in the following case?

int my_very_informative_variable_name = 0;

the result will be:

int my_very_informat= 0;

The correct way to solve this problem is to find the longest var in the list (selected lines) and align accordingly.


Some mappings and a python script for alignment. http://www.ophinity.com/code/wrangling/index.html#lineUp

pro:

  • humans can understand python

con:

  • you need to have a python interpreter on your box
  • it's not as sophisticated as dr. chips script

You can save a couple keystrokes without visual mode.

0f=20i<Space><Esc>020ldt=

And a couple more by using the goto-column movement :help bar:

0f=20i<Space><Esc>d20|

And finally, you can fire off something like

:11,32norm 0f=20i<Space><ctrl-v><Esc>d20|

to do this for a bunch of lines.


0f=gelcw<Tab><Esc>

works well of you have softtabs set to use spaces. (Or using :retab)

This will align based on next tab columns. Neat enough for me.


Personally I'd shell out and use column for this, but obviously requires having column installed!!

'<'>! column -ts=

Advertisement | Your ad here

Around Wikia's network

Random Wiki