Wikia

Vim Tips Wiki

Changes: Simple text alignment

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
Line 32: Line 32:
 
This is how we achieve it
 
This is how we achieve it
   
0f=20i<space><esc>020lvf=hx
+
0f=20i<Space><Esc>020lvf=hx
   
 
This is what it does
 
This is what it does
Line 39: Line 39:
 
0 goes to first column
 
0 goes to first column
 
f= finds next occurrence of = on current line
 
f= finds next occurrence of = on current line
20i&lt;space&gt;&lt;esc&gt; inserts 20 spaces before =
+
20i<Space><Esc> inserts 20 spaces before =
 
0 goes back to first column
 
0 goes back to first column
 
20l forward 20 column
 
20l forward 20 column
Line 73: Line 73:
 
You can save a couple keystrokes without visual mode.
 
You can save a couple keystrokes without visual mode.
   
0f=20i&lt;space&gt;&lt;esc&gt;020ldt=
+
0f=20i<Space><Esc>020ldt=
   
 
And a couple more by using the goto-column movement {{help|bar}}:
 
And a couple more by using the goto-column movement {{help|bar}}:
   
0f=20i&lt;space&gt;&lt;esc&gt;d20|
+
0f=20i<Space><Esc>d20|
   
 
And finally, you can fire off something like
 
And finally, you can fire off something like
   
:11,32norm 0f=20i&lt;space&gt;&lt;ctrl-v&gt;&lt;esc&gt;d20|
+
:11,32norm 0f=20i<Space><ctrl-v><Esc>d20|
   
 
to do this for a bunch of lines.
 
to do this for a bunch of lines.
   
 
----
 
----
0f=gelcw&lt;TAB&gt;&lt;ESC&gt;
+
0f=gelcw<Tab><Esc>
   
 
works well of you have softtabs set to use spaces. (Or using :retab)
 
works well of you have softtabs set to use spaces. (Or using :retab)

Revision as of 05:35, September 29, 2008

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.


Around Wikia's network

Random Wiki