Wikia

Vim Tips Wiki

Specify a column with bar

Talk0
1,612pages on
this wiki
Revision as of 02:40, July 20, 2009 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 253 Printable Monobook Previous Next

created May 26, 2002 · complexity basic · author RobertKellyIV · version 6.0


You may well be aware that '|' is the goto column motion, and "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.

v75|r-

will replace from the cursor to the end of line with '-'.

Explanation:

v   start visual mode
75  for the count
|   goto column
r   replace
-   the character for the replace

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

A handy way to use this (formatted 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 "

==Comments==
How can I move the "bar" in
 foo bar
to column 30?

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

Explanation:

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

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


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


That is because you need:

:set virtualedit=all

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) )


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...


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

Explanation:

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)

See feralalign for a simple script to save you from "ciw<tab><tab><tab>" to align text.

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


Around Wikia's network

Random Wiki