Vim logo vim online Vim Book Ad

basic Tip #253: The power of | (v75|r- actually...)

 tip karma   Rating 13/7, Viewed by 1098 

created:   May 26, 2002 8:20      complexity:   basic
author:   RobertKellyIV      as of Vim:   6.0

'|' as you may well be aware is the goto column motion, and that "75|" will place your cursor on column 75 of the current line.

That in itself is pretty handy at times, but some true power arises when used in conjuction with visual mode and replace. Or you could just say a sneaky trick :)

v75|r-
will repace from the cursor to the end of line with '-'
*breakdown*
v to turn on visual mode
75 for the count
| *bar* to goto column
r to enter repace
- to specify the char to replace.

A handy and quick way to make a noticable section of your code (or whatever).

A handy way to use this (formated to just drop into DrChip's CStubs):
"// -[Feral]---------------------------------------------------------------
"// <cursor>
elseif wrd == "//"
exe "norm! a -[AuthorId]\<esc>$lv75|r-$a\<cr>\<esc>$a "

"// -[Feral:146/02@08:31]--------------------------------------------------
"// <cursor>
elseif wrd == "///"
exe "norm! s -[AuthorId:\<C-R>=strftime('%j/%y@%H:%M')\<CR>]\<esc>$lv75|r-$a\<cr>\<esc>$a "

"/* -[Feral:146/02@08:31]--------------------------------------------------
" * <cursor>
" * -------------------------------------------------------------------- */
elseif wrd == "/*"
exe "norm! a -[AuthorId:\<C-R>=strftime('%j/%y@%H:%M')\<CR>]\<esc>$lv75|r-$a\<cr>\<cr>\<esc>2lv72|r-$a */\<esc>k$a "


Have to love VIM!

 rate this tip  Life Changing Helpful Unfulfilling 

<<python script to align statements | Using \%[] to easily match parts of a word. >>

Additional Notes

Anonymous, May 26, 2002 17:03
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]".

RobertKellyIV <[email protected]>, May 26, 2002 20:18

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 :)
Anonymous, May 26, 2002 21:33
This tip made no sense to me until I ":set virtualedit=all", so if it's not working for you, try that.
Anonymous, May 27, 2002 1:02
Thanks Robert, it works great.
Anonymous, May 27, 2002 2:37
for some reason | wont go beyond the newline character and is completely meaningless on an empty line.
so I just do "75i-<esc>"
RobertKellyIV <[email protected]>, May 27, 2002 10:47
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! (=
Anonymous, May 27, 2002 20:53
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 29, 2002 10:30
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...
RobertKellyIV <[email protected]>, May 30, 2002 2:35
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 <[email protected]>, May 30, 2002 2:40
*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.
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