Vim Tips Wiki
m (Pad trailing blanks onto end of lines to ease visual blocks moved to Add trailing blanks to lines for easy visual blocks: Page moved by JohnBot to improve title)
(Remove html character entities)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=828
 
|id=828
  +
|previous=826
|title=pad trailing blanks onto end of lines to ease visual blocks
 
  +
|next=829
|created=December 3, 2004 7:14
+
|created=December 3, 2004
 
|complexity=basic
 
|complexity=basic
 
|author=Micha Shepher
 
|author=Micha Shepher
 
|version=5.7
 
|version=5.7
 
|rating=-3/3
 
|rating=-3/3
  +
|category1=
|text=
 
  +
|category2=
I love using the visual block feature to move columns around (<ctrl-v>, blockwise-visual).
 
 
}}
 
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
+
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:
when lines are not of equal length. &lt;ctrl-v&gt; marks then a block which
 
   
  +
<pre>
is equal in width to the shortest line.
 
 
" 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
  +
</pre>
   
 
You may alternatively use the oneliner:
   
  +
<pre>
In order to pad all lines to a given width with trailing blanks
 
 
:g/^/exe "norm! 100A" | call cursor(getline('.'), 79) | norm d$
  +
</pre>
   
 
==Comments==
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?
 
 
 
}}
 
 
== Comments ==
 
 
Try using 'virtualedit', as in :set ve+=block
 
Try using 'virtualedit', as in :set ve+=block
   
'''Anonymous'''
 
, December 3, 2004 8:32
 
 
----
 
----
 
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.
 
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.
   
vlmarek--AT--volny.cz
 
, December 4, 2004 0:42
 
 
----
 
----
I have had good luck with &lt;shift-v&gt;, --Visual Line-- as well... Sort of bypasses having to type V and then $.
+
I have had good luck with <shift-v>, --Visual Line-- as well... Sort of bypasses having to type V and then $.
   
 
thulsey--AT--yahoo.com
 
, December 5, 2004 23:53
 
 
----
 
----
All of your suggestions would work, but if you need to add
+
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,
a column, but precisely aligned, how would you achieve
 
that without blank padding? For example,
 
   
  +
<pre>
1. Test 1
+
1. Test 1
2. Test 2 has a longer description
 
3. Test 3
+
2. Test 2 has a longer description
4. Test 4
+
3. Test 3
  +
4. Test 4
  +
</pre>
   
 
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.
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.
 
   
'''Anonymous'''
 
, December 7, 2004 4:08
 
 
----
 
----
ctrl-v + virtual edit (set ve) will allow you to yank blocks and get the line padding automatically.
+
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.
+
As far as aligning columns goes, you can also use Align.vim.
You can align on tabs (\tab), change tabs to --AT--s and align on --AT--s, etc (see [/scripts/script.php?script_id=294 vimscript&#35;294]).
+
You can align on tabs (\tab), change tabs to @s and align on @s, etc (see [/scripts/script.php?script_id=294 vimscript#294]).
   
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
 
, December 7, 2004 9:29
 
 
----
 
----
As for adding text to the end of the bock (assuming you want the added text to line up)...
+
As for adding text to the end of the bock (assuming you want the added text to line up).
1. Type:
+
1. Type:
:set ve=all
+
:set ve=all
2. Hit ctr-v and highlight the block.
+
2. Hit ctr-v and highlight the block.
3. Type:
+
3. Type:
A - PASSED&lt;esc&gt;
+
A - PASSED<Esc>
   
 
You should see " - PASSED" added to the end of every line, all nice and neatly.
 
You should see " - PASSED" added to the end of every line, all nice and neatly.
   
'''Anonymous'''
 
, December 24, 2004 10:58
 
----
 
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
 
 
 
'''Anonymous'''
 
, July 6, 2005 19:57
 
 
----
 
----
<!-- parsed by vimtips.py in 0.549643 seconds-->
 

Latest revision as of 09:30, 29 September 2008

Tip 828 Printable Monobook Previous Next

created December 3, 2004 · complexity basic · author Micha Shepher · version 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$

Comments[]

Try using 'virtualedit', as in :set ve+=block


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.


I have had good luck with <shift-v>, --Visual Line-- as well... Sort of bypasses having to type V and then $.


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.


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 [/scripts/script.php?script_id=294 vimscript#294]).


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.