sponsor Vim development Vim logo Vim Book Ad

basic Tip #828: pad trailing blanks onto end of lines to ease visual blocks

 tip karma   Rating -3/3, Viewed by 729 

created:   December 3, 2004 7:14      complexity:   basic
author:   Micha Shepher      as of Vim:   5.7

I love using the visual block feature to move columns around (<ctrl-v>, blockwise-visual).
However, this does not work conveniently on the last column
when  lines are not of equal length. <ctrl-v> marks then a block which
is equal in width to the shortest line.

In order to pad all lines to a given width with trailing blanks
you can use the following functions:

" truncate line 'line' to no more than 'limit' width
function! Truncate( line, limit )
    call cursor(a:line,a:limit)
    norm d$
endfunc

" Pad all lines with trailing blanks to 'limit' length.
function! AtOnce( limit )
    norm mm
    g/^/norm 100A
    g/^/call Truncate( getline('.'), a:limit )
    let @/=""
    norm 'm
endfunc


You may alternatively use the oneliner:
:g/^/exe "norm! 100A" | call cursor(getline('.'), 79) | norm d$


I even saw someone use a standard vi (non vim) oneliner to achieve the
same, but I forgot how. Any ideas?

 rate this tip  Life Changing Helpful Unfulfilling 

<<XTerm and 256 Colors | copy & paste between vim session >>

Additional Notes

Anonymous, December 3, 2004 8:32
Try using 'virtualedit', as in :set ve+=block
[email protected], December 4, 2004 0:42
Also if you want to block-select to the end of the line, press Ctrl-v for virtual edit and $ for end of line. Then move the cursor up or down and the block will match everything till the end.
[email protected], December 5, 2004 23:53
I have had good luck with <shift-v>, --Visual Line-- as well... Sort of bypasses having to type V and then $.
Anonymous, December 7, 2004 4:08
All of your suggestions would work, but if you need to add
a column, but precisely aligned, how would you achieve
that without blank padding? For example,

1. Test 1
2. Test 2 has a longer description
3. Test 3
4. Test 4


And you need to add the string "- PASSED" to the end
of each line with g/^/exe "norm! A - PASSED".
Without the padding of blanks you can't have the
required result.
[email protected] - NOSPAM, December 7, 2004 9:29
ctrl-v + virtual edit (set ve) will allow you to yank blocks and get the line padding automatically.

As far as aligning columns goes, you can also use Align.vim.
You can align on tabs (\tab), change tabs to @s and align on @s, etc (see vimscript#294).
Anonymous, December 24, 2004 10:58
As for adding text to the end of the bock (assuming you want the added text to line up)...
1. Type:
       :set ve=all
2. Hit ctr-v and highlight the block.
3. Type:
       A - PASSED<esc>

You should see " - PASSED" added to the end of every line, all nice and neatly.
Anonymous, July 6, 2005 19:57
how to append a block at the end the several line?
update a where id=1000 set text='333'
update a where id=1001 set text='4444'
update a where id=1002 set text='55555'
update a where id=1003 set text='666666'

so I want to cut the "where id=1xxx" and append it the end of every line , such as
update a  set text='333' where id=1000
update a set text='4444'  where id=1001
update a set text='55555'  where id=1002
update a set text='666666'  where id=1003
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.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo