Vim logo vim online Vim Book Ad

basic Tip #319: text formatting (lining up ='s,('s etc))

 tip karma   Rating -3/13, Viewed by 1419 

created:   August 23, 2002 7:01      complexity:   basic
author:   Jahagirdar Vijayvithal S      as of Vim:   5.7

some time onw would like to reformat text like
a=1;
foo=2;
longstring=1;
c=2

to

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

Note I am not sure wether the code above is displayed properly in your browsers what  is basically shows is all the ='s are lined up in a single column
and this  is how we achive it
0f=20i<space><esc>020lvf=hx
and this is what it does
0                             goes to first column
f=                             finds next occurance of = on current line
20i<space><esc>         inserts 20 spaces before =
0                               goesback to first column
20l                             forward 20 column
vf=hx                         deletes everything up to the = sign

 rate this tip  Life Changing Helpful Unfulfilling 

<<Extended Bracket and Parenthesis + extras for perl | Borland pageup/down behavier >>

Additional Notes

Anonymous, August 23, 2002 7:31
Any lining up, alignment, etc..just leave it to Dr. Chip Campbell's Align.vim (search for it here)
Anonymous, August 23, 2002 12:41
This is a more lightweight solution, though. There's something to be said for that...
Alex A. Naanou <[email protected]>, August 23, 2002 16:35
Nice, but I see a BIG drawback!!

what will it do in the following case??  :)
Ex:
---cut---
int my_very_informative_variable_name = 0;

---uncut---
the result will be:

int my_very_informat= 0;

yes the simplest way out of this is to extend the thing to say... 40 spaces....
but this will make all the short variable defs look ugly!

Ex:

int i                                   = 0;
char * my_foo_bar                       = "moo";

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

Anonymous, August 23, 2002 22:14
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
[email protected], August 24, 2002 3:51
I know about the drawback alex mentioned( having a variable name more than 20 characters long) this was just a sample example the number of space inserted and the column at which alignment has to be done can be changed as per requirement.
Anonymous, August 25, 2002 17:53
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.
[email protected], August 26, 2002 8:00

0f=gelcw<TAB><ESC>

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

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

Marius.
[email protected], August 28, 2002 15:07
Metatip: Edit your tips with Vim itself (or Notepad, or another editor that edits raw text in a monospaced font). Then paste them into the textbox when you are posting your tip.

That way, the odds of things lining up are much better, since the tips display in a monospaced font.
[email protected], August 29, 2002 6:42
Tip #319: text formatting (lining up ='s,('s etc))

tip karma
Rating -3/3, Viewed by 491


created:

August 23, 2002 7:01
    
complexity:

basic
author:

Jahagirdar Vijayvithal S
    
as of Vim:

5.7

some time onw would like to reformat text like
a=1;
foo=2;
longstring=1;
c=2

to

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

Note I am not sure wether the code above is displayed properly in your browsers what  is basically shows is all the ='s are lined up in a single column
and this  is how we achive it
0f=20i<space><esc>020lvf=hx
and this is what it does
0                            goes to first column
f=                            finds next occurance of = on current line
20i<space><esc>        inserts 20 spaces before =
0                              goesback to first column
20l                            forward 20 column
vf=hx                        deletes everything up to the = sign

rate this tip
Life Changing Helpful Unfulfilling

<<Extended Bracket and Parenthesis + extras for perl | Borland pageup/down behavier >>

Additional Notes

Anonymous, August 23, 2002 7:31

Any lining up, alignment, etc..just leave it to Dr. Chip Campbell's Align.vim



(search for it here)

Anonymous, August 23, 2002 12:41

This is a more lightweight solution, though. There's something to be said for that...

Alex A. Naanou <[email protected]>, August 23, 2002 16:35

Nice, but I see a BIG drawback!!

what will it do in the following case??  :)
Ex:
---cut---
int my_very_informative_variable_name = 0;

---uncut---
the result will be:

int my_very_informat= 0;

yes the simplest way out of this is to extend the thing to say... 40 spaces....
but this will make all the short variable defs look ugly!

Ex:

int i                                  = 0;
char * my_foo_bar                      = "moo";

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

Anonymous, August 23, 2002 22:14

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

[email protected], August 24, 2002 3:51

I know about the drawback alex mentioned( having a variable name more than 20
characters long) this was just a sample example the number of space inserted and the column at which alignment has to be done can be changed as per requirement.

Anonymous, August 25, 2002 17:53

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.

[email protected], August 26, 2002 8:00


0f=gelcw<TAB><ESC>

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

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

Marius.

[email protected], August 28, 2002 15:07

Metatip: Edit your tips with Vim itself (or Notepad, or another editor that edits raw text in a monospaced font). Then paste them into the textbox when you are posting your tip.

That way, the odds of things lining up are much better, since the tips display in a monospaced font.
Anonymous, August 30, 2002 13:06
[email protected]:

0f=gelcw<TAB><ESC>
will fail if there's no space between the previous word and the equals sign.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo