Vim Tips Wiki
No edit summary
 
No edit summary
Tag: sourceedit
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=319
 
|id=319
  +
|previous=318
|title=text formatting (lining up ='s,('s etc))
 
  +
|next=320
|created=August 23, 2002 7:01
 
  +
|created=August 23, 2002
 
|complexity=basic
 
|complexity=basic
 
|author=Jahagirdar Vijayvithal S
 
|author=Jahagirdar Vijayvithal S
 
|version=5.7
 
|version=5.7
 
|rating=-6/20
 
|rating=-6/20
  +
|category1=
|text=
 
  +
|category2=
some time onw would like to reformat text like
 
  +
}}
  +
Some times one would like to reformat text like:
   
  +
<pre>
a=1;
 
  +
a=1;
  +
foo=2;
  +
longstring=1;
  +
c=2
  +
</pre>
   
  +
to
foo=2;
 
   
  +
<pre>
longstring=1;
 
  +
a =1;
  +
foo =2;
  +
longstring =1;
  +
c =2;
  +
</pre>
   
  +
This is how we achieve it
c=2
 
   
  +
0f=20i<Space><Esc>020lvf=hx
   
  +
This is what it does
   
  +
<pre>
to
 
  +
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
  +
</pre>
   
  +
==Comments==
  +
Any lining up, alignment, etc - just leave it to Dr. Chip Campbell's Align.vim (search for it here)
   
 
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&lt;space&gt;&lt;esc&gt;020lvf=hx
 
 
and this is what it does
 
 
0 goes to first column
 
 
f= finds next occurance of = on current line
 
 
20i&lt;space&gt;&lt;esc&gt; inserts 20 spaces before =
 
 
0 goesback 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)
 
 
'''Anonymous'''
 
, August 23, 2002 7:31
 
 
----
 
----
  +
I see a BIG drawback! What will it do in the following case?
This is a more lightweight solution, though. There's something to be said for that...
 
   
  +
int my_very_informative_variable_name = 0;
'''Anonymous'''
 
, August 23, 2002 12:41
 
----
 
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 result will be:
the correct way to solve this problem is to find the longest var in the
 
list (selected lines) and align accordingly....
 
   
  +
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.
   
Alex A. Naanou &lt;alex_nanou--AT--yahoo.com&gt;
 
, August 23, 2002 16:35
 
 
----
 
----
some mappings and a python script for alignment.
+
Some mappings and a python script for alignment.
http://www.ophinity.com/code/wrangling/index.html&#35;lineUp
+
http://www.ophinity.com/code/wrangling/index.html#lineUp
   
pro:
+
pro:
humans can understand python
+
*humans can understand python
   
con:
+
con:
you need to have a python interpreter on your box
+
*you need to have a python interpreter on your box
it's not as sophisticated as dr. chips script
+
*it's not as sophisticated as dr. chips script
   
 
'''Anonymous'''
 
, August 23, 2002 22:14
 
 
----
 
----
  +
You can save a couple keystrokes without visual mode.
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.
 
   
  +
0f=20i<Space><Esc>020ldt=
   
  +
And a couple more by using the goto-column movement {{help|bar}}:
jahagirdar_vs--AT--yahoo.Ihatespam.com
 
, August 24, 2002 3:51
 
----
 
You can save a couple keystrokes without visual mode.
 
   
  +
0f=20i<Space><Esc>d20|
0f=20i&lt;space&gt;&lt;esc&gt;020ldt=
 
   
  +
And finally, you can fire off something like
And a couple more by using the goto-column movement ( [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:bar}} :help bar] )
 
   
0f=20i&lt;space&gt;&lt;esc&gt;d20|
+
:11,32norm 0f=20i<Space><ctrl-v><Esc>d20|
 
And finally, you can fire off something like
 
 
:11,32norm 0f=20i&lt;space&gt;&lt;ctrl-v&gt;&lt;esc&gt;d20|
 
   
 
to do this for a bunch of lines.
 
to do this for a bunch of lines.
   
'''Anonymous'''
 
, August 25, 2002 17:53
 
 
----
 
----
  +
0f=gelcw<Tab><Esc>
 
0f=gelcw&lt;TAB&gt;&lt;ESC&gt;
 
   
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)
   
This will align based on next tab collumns. Neat enough for me.
+
This will align based on next tab columns. Neat enough for me.
   
Marius.
 
 
agentwho--AT--hotmail.com
 
, August 26, 2002 8:00
 
 
----
 
----
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.
 
   
  +
Personally I'd shell out and use column for this, but obviously requires having column installed!!
That way, the odds of things lining up are much better, since the tips display in a monospaced font.
 
  +
  +
:'<'>! column -ts=
   
jmcpherson--AT--bigfoot.com
 
, August 28, 2002 15:07
 
 
----
 
----
Tip &#35;319: text formatting (lining up ='s,('s etc))
 
   
tip karma
 
Rating -3/3, Viewed by 491
 
   
  +
Allow me to make a small but important modification to the last command:
   
  +
:'<'>! column -ts= -o=
created:
 
   
  +
Otherwise, the "=" sign would be removed from the output.
August 23, 2002 7:01
 
  +
It's a very good replacement for the Tabularize plugin! At least for simple stuff like that.
   
  +
----
complexity:
 
   
  +
If you don't mind using up a bookmark, you can align the equals sign on the current line at the current cursor position using the following magic:
basic
 
author:
 
   
  +
:nnoremap ,= mc0f=80i<Space><Esc>`cdt=j
Jahagirdar Vijayvithal S
 
   
  +
Here's how it works.
as of Vim:
 
   
  +
<pre>
5.7
 
  +
mc Stores the cursor position in bookmark "c" (you can choose a different slot)
 
  +
0 Goes to start of line
some time onw would like to reformat text like
 
  +
f= finds next (first) occurrence of = on current line
a=1;
 
  +
80i<Space><Esc> inserts 80 spaces before =
foo=2;
 
  +
`c Returns to bookmark (` positions by both line and column)
longstring=1;
 
  +
dt= Deletes up to the character before the =
c=2
 
  +
j Goes down 1 line (optional)
 
  +
</pre>
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&lt;space&gt;&lt;esc&gt;020lvf=hx
 
and this is what it does
 
0 goes to first column
 
f= finds next occurance of = on current line
 
20i&lt;space&gt;&lt;esc&gt; 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
 
 
&lt;&lt;Extended Bracket and Parenthesis + extras for perl | Borland pageup/down behavier &gt;&gt;
 
 
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 &lt;alex_nanou--AT--yahoo.com&gt;, 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&#35;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
 
 
jahagirdar_vs--AT--yahoo.Ihatespam.com, 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&lt;space&gt;&lt;esc&gt;020ldt=
 
 
And a couple more by using the goto-column movement ( [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:bar}} :help bar] )
 
 
0f=20i&lt;space&gt;&lt;esc&gt;d20|
 
 
And finally, you can fire off something like
 
 
:11,32norm 0f=20i&lt;space&gt;&lt;ctrl-v&gt;&lt;esc&gt;d20|
 
 
to do this for a bunch of lines.
 
 
agentwho--AT--hotmail.com, August 26, 2002 8:00
 
 
 
0f=gelcw&lt;TAB&gt;&lt;ESC&gt;
 
 
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.
 
 
jmcpherson--AT--bigfoot.com, 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.
 
 
argosoco--AT--yahoo.com
 
, August 29, 2002 6:42
 
----
 
agentwho--AT--hotmail.com:
 
 
0f=gelcw&lt;TAB&gt;&lt;ESC&gt;
 
will fail if there's no space between the previous word and the equals sign.
 
 
'''Anonymous'''
 
, August 30, 2002 13:06
 
----
 
<!-- parsed by vimtips.py in 0.721355 seconds-->
 

Latest revision as of 15:50, 20 November 2015

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=


Allow me to make a small but important modification to the last command:

:'<'>! column -ts= -o=

Otherwise, the "=" sign would be removed from the output. It's a very good replacement for the Tabularize plugin! At least for simple stuff like that.


If you don't mind using up a bookmark, you can align the equals sign on the current line at the current cursor position using the following magic:

:nnoremap ,= mc0f=80i<Space><Esc>`cdt=j

Here's how it works.

mc Stores the cursor position in bookmark "c" (you can choose a different slot)
0  Goes to start of line
f= finds next (first) occurrence of = on current line
80i<Space><Esc> inserts 80 spaces before =
`c Returns to bookmark (` positions by both line and column)
dt= Deletes up to the character before the =
j Goes down 1 line (optional)