Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #253 - Specify a column with bar

Created: May 26, 2002 8:20 Complexity: basic Author: RobertKellyIV <feral--AT--firetop.com> Version: 6.0 Karma: 17/8 Imported from: Tip#253

'

Comments

Very nice feature.

Additional question:

How can I move the "bar" in

foo bar

to column 30?

O.K., I can think of several ways to do that, but I'd like to do that without thinking about how many spaces or shiftwidths I might need. I'd love to know a command "move everything after the cursor to column [count]".


Anonymous , May 26, 2002 17:03


That is also quite easy to do as it turns out. "move everything after the cursor to column [count]". equates to: d$30|p

  • breakdown*

d$ to del to end of line (into unnamed register) 30| to place cursor on column 30 p to paste unnamed register.


I.e. Before: int foo = bar;

<place cursor where desired (on b of bar)> d$30|p


Becomes: int foo = bar;

VIM is neat :)


RobertKellyIV <feral--AT--firetop.com> , May 26, 2002 20:18


This tip made no sense to me until I ":set virtualedit=all", so if it's not working for you, try that.

Anonymous , May 26, 2002 21:33


Thanks Robert, it works great.

Anonymous , May 27, 2002 1:02


for some reason | wont go beyond the newline character and is completely meaningless on an empty line. so I just do "75i-<esc>"

Anonymous , May 27, 2002 2:37


That should be because (as Anonymous, May 26, 2002 21:33) mentioned,

set virtualedit=all

Is needed (opoligies for forgetting to mention that..)

The `problem` with just 75i-<esc> is that it will inset 75 chars(or strings as case may be) regardless of what column you are in. So, to trim the line at column 75 you would need to add 75|D (goto column 75 and delete to end of line).

There is an advantage to doing things that way however, one it works regardless of how you have virtualedit set (I have a hard time fathoming it being anything but all personally ;) ) but you can also specify a string to repeate, thus "=-=-=-=-=-=-=-=-=-=-=-" or some such is possible (75i=-<esc>75|D)

The same idea holds true for positioning the rest of a line at a specific column: //place cursor on start of word you want at column 30i <esc>30|dw

(goto column 30 and delete word (i.e. the extra spaces) )

Happy VIMing! (=

RobertKellyIV <feral--AT--firetop.com> , May 27, 2002 10:47


A nice tip. Now, it would be better if I could set a default count for the '|' command; something like

set defaultbarcolumn=75


Anonymous , May 27, 2002 20:53


Is there a way to use this to align variable declarations? Say I have three spaces then the variable name then some unknown number of spaces and the type information. If I want to align all the type information at column 20, how would I do that? I essentially need to eliminate the leading whitespace before pasting I think...

Anonymous , May 29, 2002 10:30


Sort of messy but given:

varname typeinfo 
varname typeinfo 
varname typeinfo 

0weldw20i <esc>20|dw

on each line will result in:

varname typeinfo 
varname typeinfo 
varname typeinfo 
  • breakdown*

0 = start of line, column 0 wel = place cursor one char past first word (var name) dw = delete spaces to next word (type definition) 20i <esc> = instert 20 spaces. 20|dw = goto column 20 and delete the spaces from here to next word (type definition)

RobertKellyIV <feral--AT--firetop.com> , May 30, 2002 2:35


  • note to self* this forum no like tabs.

Imagine the first two entries under given to look like:

varname typeinfo 
varname typeinfo 
varname typeinfo 

Or some such, as long as there is one space between varname and typeinfo the above method should work.

RobertKellyIV <feral--AT--firetop.com> , May 30, 2002 2:40


See also, [/scripts/script.php?script_id=686 vimscript #686] , which is: feralalign.vim : Simple script to save you from "ciw<tab><tab><tab>" to align text.

Which is relevant to the alignment of variables question/answer above.


Sreny--AT--SverGbc.Pbz (Rot13ed) , July 6, 2003 21:57


Advertisement