How to cut and then paste previous item in clipboard in vim? - vim

In vim I copied the string ABCD to my clipboard. Now I want to replace certain words in a paragraph of text by doing c-e (this deletes and immediately puts me in insert mode). But when I paste it will paste the thing I just cut overriding my ABCD.
One solution I came up with is:
c-e
ctrl-o
"0P
But that just seems way too long. Is there a faster alternative to that?

While copying a string, place the cursor at the start of the string, then type
"ayw
where "a means the name of the register(its like a storage) and yw means copy(yank) the word into that register.
There are 26 registers, one for each letter of the alphabet.
When you want to paste the content, type
"ap
where p means to paste. Then use the vim repeat command . to repeat last action
You can store strings in different registers and paste them whenever u want.
to set contents of all registers type
:reg
For more information go to
Using Vim's named registers
Vim registers: The basics and beyond
Vim tips and tricks

You can use yankstack, which simplifies your use case.
This make it easy to cycle against your yank buffer when you paste.
I've personally remapped the keys to space (my leader key).
Here is an extract of my .vimrc:
if &runtimepath =~ 'vim-yankstack'
let g:yankstack_map_keys = 0
nmap <leader>p <Plug>yankstack_substitute_older_paste
nmap <leader>P <Plug>yankstack_substitute_newer_paste
endif
In your case, you just press space p right after having pasted your text. This will replace the word pasted with the last entry in your buffer. You can repeat pressingspace p if you need to go deeper in the history.

Use the "stamping" approach; this allows you to stay in normal mode and is just one keystroke. Add this mapping:
nnoremap S diw"0P
Then press S to "stamp" over any word your cursor is on with what you've yanked.
So, an example of the full routine:
1. ye
2. <move cursor over the word you wish to replace with the yank>
3. S
And, of course, if S is used, chose something else, e.g. ww.
This is a well known approach; see this documentation for variations and details: http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text.

The last thing you yanked is always available in register 0 so, in your case, you only have to do:
<C-e><C-r>0<Esc>
See :help i_ctrl-r.
But yeah, there's a much simpler way for one-off puts:
vep
or, if you need to do those changes several times:
ve"0p

Related

How can I stop a c)hange from copying to my paste buffer?

A common thing I want to do is yank "some text" and then use it to change "some other text".
So I cursor to some text and then yi" to grab the some text. How do I now replace some other text? If I do di" then my copy paste register gets overwritten with some other text. I know I can use named registers, but my problem is my muscle memory has already done yi". Is there any way I can override the default behaviour of either y or d?
"Unnamed" register in Vim is not a real register, but a pointer to a register last used. It's even implemented in Vim's source code as a pointer (or to be more precise, as an index into register array).
So the yanked text does not get really overwritten by "delete" command, as "yank" by default uses register "zero", while "delete" uses either "one" or "minus".
Hence you can always put the last yanked text by pressing "0p.
You could remap the d key to perform a deletion into the blackhole register "_:
nnoremap d "_d
You need to use one of the noremap versions such that it doesn't go into an infinite loop.

Pasting text in vim. A tedious operation?

I've been using vim for somewhat longer than a year now and during this time I have never feel really comfortable with the way vim works with yanking and pasting text (or maybe it is just me not using it in the most efficient way)
For example, I have the word "World" yanked onto a register, and I want to paste it after "Hello". (Note that there are no spaces on either of the words). So, what I would do is
Hello
|
Place cursor here, and press "p". Then, what I will end up with is
HelloWorld
So, in order to avoid this, I have always to swith into insert mode, insert a espace, and go back into normal mode (or either make sure that the yanked word has a space before it). Be as it may, this is quite annoying behaviour I can't think of a solution for... Am I missing something here?
Suggestions will be appreciated.
Thanks
option zero
just live with what you have now.
option one
create a mapping for your workflow. for example
nnoremap <leader>p i<space><esc>p
option two
:set ve=all
then you could move your cursor to anywhere and paste
option three
you could in insert mode use <c-o> do normal mode stuff or <c-r> to get register values
I recommend option zero
You can use the Smartput : Adjust spaces and commas when putting text plugin for that. It modifies the p / P commands (this can be toggled on / off).

macvim: how to paste several times the same yanked word?

Each time i copy a word and want to replace it for several words, i do:
yank the word
enter visual mode, select the word to be replaced and paste the yanked word.
After this process, the replaced word will be yanked and cannot continue replacing new words bceause i lost the first yanked word. So, i must copy again the first yanked word.
Could anybody guide to me on how to achieve my goal in an efficient way? It could be enough if my yanked word would not get changed.
I would suggest explicitly using a register for your yank and paste.
"ayw or however you chose to yank your word.
"ap to paste.
In this case I've used the a register but you could use whichever suits you.
It has been answered before: Vim: how to paste over without overwriting register.
Overall, crude vnoremap p "_dP mapping will almost get you there, but it won't work well in a few edge cases (e.g. if a word you're replacing is at the end of the line).
The superior approach is to use this crazy-looking snippet (I wish I knew Vimscript at least half as good as the author of this):
" replace visual selection without overwriting default register
function! RestoreRegister()
let #" = s:restore_reg
return ''
endfunction
function! s:Repl()
let s:restore_reg = #"
return "p#=RestoreRegister()\<cr>"
endfunction
vnoremap <silent> <expr> p <sid>Repl()
Personally, I'd favour doing :s/word/replacement words/gc.
Alternatively, you could use "_de to delete the word to be replaced. "_ says use the "black hole" buffer to prevent losing the existing default buffer contents.
Perhaps a bit better than this is to yank the replacement words into an alternative named buffer (e.g. "a3ye), then you can delete the work to be replaced (de) and paste the named buffer "ap.
One addition to #Randy Morris answer: instead of specifying register explicitly in both cases, you can specify it only in the second one, see :h quote0 («Numbered register 0 contains the text from the most recent yank command...»). In this case using a register is better (as it is much easier to type), but if you say you are replacing words, you may want to use ciw<C-r>0 and then one . for each other word you want to replace.
I use this mapping to replace the currently selected text with default register without yanking it:
vnoremap <leader>p "_dP
I dont use yank, but ciw and then repeat with .
For instance:
Go to somewhere inside the word you want to replace.
Do ciw <type new word> Esc
Go to somewhere inside the next word you want to replace.
Press . to repeat the last replace.
Advanced:
You can also first find the word with /<word> and then use ciw <new word>.
Then you dont have to move to the word yourself before pressing . but you can just use n to go to the next (or N to go to the previous).

How to move yanked text into particular register in vim?

When editing in vim, often enough I find myself stopping when I'm going to delete text and I notice that previously yanked text would become handy for next operations.
So - how to move already yanked text into particular register (e.g. under a)?
To move register 0 to register a:
:let #a=#0
You can use something like:
noremap <leader>ma :let #a=#<CR>
Now, when you press \ma in normal mode, your last yanked text will go to register 'a'.
Note : Assuming leader is '\'
You could also use the 0 register, if you only need it for a short amount of time. From :help quote_number (just below :help registers):
2. Numbered registers "0 to "9
Vim fills these registers with text from yank and delete commands.
Numbered register 0 contains the text from the most recent yank command,
unless the command specified another register with ["x].
(link to the rest)
Alternatively, you can delete text to the devnull registry with "_d (actually it is called the blackhole registry). See :help quote_. No need then to move registers, your previously yanked text will still be available with p.

Replace word with contents of paste buffer?

I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g.
I know that this is the typical way one replaces the word at the current cursor position: cw<text><esc> but is there a way to do this with the contents of the unnamed register as the replacement text and without overwriting the register?
I'm thinking by "paste" you mean the unnamed (yank/put/change/delete/substitute) register, right? (Since that's the one that'd get overwritten by the change command.)
Registers are generally specified by typing " then the name (single character) of the register, like "ay then "ap to yank into register a, then put the contents of register a. Same goes for a change command. In this case, if you don't want the text you remove with the change command to go anywhere, you can use the black hole register "_: "_cw. Then once in insert mode, you can hit ctrl-R followed by the register you want (probably ") to put in the contents of that register.
"* - selection register (middle-button paste)
"+ - clipboard register (probably also accessible with ctrl-shift-v via the terminal)
"" - vim's default (unnamed) yank/put/change/delete/substitute register.
Short answer: "_cw^R"
Edit: as others are suggesting, you can of course use a different register for the yank (or whatever) that got your text into the default register. You don't always think of that first, though, so it's nice to do a single change command without blowing it away. Though it's not totally blown away. There are the numbered registers "0 through "9:
Vim fills these registers with text from yank and delete commands.
Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with ["x].
Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: %, (, ), `, /, ?, n, N, { and }. Register "1 is always used then (this is Vi compatible). The "- register is used as well if the delete is within a line.
With each successive deletion or change, Vim shifts the previous contents of register 1 into register 2, 2 into 3, and so forth, losing the previous
contents of register 9.
Using the information in this post, I have formed this useful mapping. I chose 'cp' because it signifies "change paste"
nmap <silent> cp "_cw<C-R>"<Esc>
EDIT:
Also I took this a step further and supported any motion.
To get the equivalent of command above it would be cpw for "change paste word"
"This allows for change paste motion cp{motion}
nmap <silent> cp :set opfunc=ChangePaste<CR>g#
function! ChangePaste(type, ...)
silent exe "normal! `[v`]\"_c"
silent exe "normal! p"
endfunction
You can use the visual mode of vim for this. e.g. copy a word: ye and then overwrite another one with the copied word: vep
If your cursor is on the word you want to replace with the contents of the unnamed register, you can use viwp. v switches to visual mode, iw selects the inner word, and p puts the contents of the register in its place.
In practice, when I need to replace one word (function name, etc.) with another, I'll move to the one to use as a replacement, yiw to yank the inner word to the unnamed register, then move to the word I'm replacing, and viwp to replace it. Pretty quick way of substituting one word for another. If you searched (/) for the word you're replacing to get to it, you can then just hit n to get to the next occurrence you need to replace. Obviously no substitute for using :%s/find/replace/g, but for a couple of quick substitutions it can be handy, especially if you already have the new word in a register.
If you make use of a named register (ie. use "ay or "ad, etc., to fill your paste register), you can do something like
cw<CTRL-R>a<esc>
Which will replace the word with the contents of register a. As far as I can tell, you can't use the default register because when you cw it'll be filled with the word that was cut by that command.
Do you mean the system paste buffer or the vi register?
If you want to use the system paste buffer then you are fine and could do dw"+P - " chooses a register, and "+ is the system paste buffer.
Otherwise copy into the non-default register with say "ay to copy into register a and then to replace something do dw"aP
You can use yw to yank the word, then you can change the word with yanked word by vipw to yank word and paste previously yanked word.
You can use registers for that:
first place replacement text in register
<mark some text>"ay
where a is register name
then you can use that register in replacement
ve"ap
Or you could do Shift+v-p (select the whole line and paste in its' place)

Resources