By default, the paste commands use the "
(“unnamed”) register. Effectively, any command that writes to a register also writes to the unnamed register, so yanks, deletes, and changes all affect it. This is why your yank-delete-paste sequence pastes the deleted text instead of the yanked text.
The 0
register can help here. Any yank commands that do not specify a register put the yanked text in register 0
(in addition to "
). It is not affected by delete or change operations, so you can use it to paste a yanked line multiple times even if you do intermediate deletes or changes.
yy
: Registers 0
and "
both now have the yanked line.
- Move to a line to replace.
dd
: Register "
now has the deleted line, but register 0
still has the yanked line.
"0P
: Paste the originally yanked line from register 0
.
- Move to the next line to replace.
dd"0P
(same as above)
(Due to the way cursor positioning works when replacing the last line of a buffer, you will want to use "0p
instead of "0P
.)
This is very close to Bruce Ediger’s answer, except that you do not have to specify a register when initially yanking. Using one or more named registers can be very handy though if you need to (for example) replace some lines with AAA
, but other lines with BBB
(put AAA
in register a
, and BBB
in register b
(or leave one of them in register 0
), then paste them accordingly).
You can also paste from 0
in line-wise visual mode (V
) to save a keystroke: V"0p
.
If you do not like having to type "0
, you might find a mapping more convenient:
noremap <Leader>p "0p
noremap <Leader>P "0P
vnoremap <Leader>p "0p
An alternate approach is to delete to the _
(“blackhole”) register. When you delete to it, the "
register is not affected, so your yank-delete-paste sequence can still paste the yanked text from the unnamed register.
yy
: Register 0
and "
both now have the yanked line.
"_dd
: No change to the registers.
P
: Paste the originally yanked text from register "
.
Again, you might find a mapping more convenient:
noremap <Leader>d "_d
Alternatives
Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed
, awk
, or Perl / Python / Ruby / your favorite scripting language here.
That said, you can use Vim non-interactively:
Silent Batch Mode
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute
command), use Ex-mode.
REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"
Note: silent batch mode (:help -s-ex
) messes up the Windows console, so you may have to do a cls
to clean up after the Vim run.
# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"
Attention: Vim will hang waiting for input if the "commands.ex"
file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the -
argument.
Full Automation
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
Here's a summary of the used arguments:
-T dumb Avoids errors in case the terminal detection goes wrong.
-N -u NONE Do not load vimrc and plugins, alternatively:
--noplugin Do not load plugins.
-n No swapfile.
-i NONE Ignore the |viminfo| file (to avoid disturbing the
user's settings).
-es Ex mode + silent batch mode -s-ex
Attention: Must be given in that order!
-S ... Source script.
-c 'set nomore' Suppress the more-prompt when the screen is filled
with messages or output to avoid blocking.
Best Answer
From
:h :w
:So, if you have selected the text using visual mode, just do
:w >> /foo/samples
(:'<,'>
will be automatically prepended). If you miss out on a>
, Vim will complain: