2009-03-03

macro in vim question...

I can create a simple macro in vi(m) to add the letter b at the end of every line:

q b $ a b Esc j q

q start recording macro
b name the macro
$ jump to end of current line
a append
b the letter b
Esc back to command mode
j one line down
q end recording

When I have a text file, and I execute 20@b, then the letter b is added to 20 lines.

When there are less than 20 lines, then I would expect this macro to add multiple b's to the end of the last line. But it does not. When you type the commands in the macro yourself, then it does add multiple b's.

How can you explain this behaviour ?
Is there an easier way to add a b to every line (in vim) ?

2 comments:

Anonymous said...

ik denk dat je beter de tools gebruikt voor wat ze dienen, vim dient niet voor batch line manipulation.

Gebruik daar beter bv sed voor:

cat file | sed 's/$/b/g'

Vim doet nu en dan raar als je over het einde van de file gaat.

Anonymous said...

use global search and replace:

:0,$s/$/b/

"add a b before each and every EOL-marker in this file". Note that "$" does not match the EOL-marker itself, but a zero-byte position right before it.