After pasting how to jump to other end of inserted text - vim

After pasting some text in vim, how can I jump to the other end of the inserted text?

You can use `[ to jump to the first character of the previously changed (or yanked) text.
You can use `] to jump to the last character of the previously changed (or yanked) text.
This works for me for whether pasting ("putting") from the unnamed register or the system clipboard.

You may like these commands as well gp and gP. They are like p and P but leave the cursor just after the new text.

Related

Paste before multiline selection

I have some prefix in yank buffer that I want to paste in multiple lines before the area selected by visual-block selection.
When I press I I can start typing text before selection in each line but when I try to paste (using either p or P) it changes selection with pasted text. How to avoid removing selected text?
The easiest way to do this is probably to expand your yanked text during insertion. If you have just yanked "word" (so that it is in the register "0), then you could do i<ctrl-r>0<esc>. This is exactly like typing i or I, except that you place the content of your yanked text (in this case, the contents of register 0) in your inserted text.

In vim, how to visual select previously pasted text

I usually visual select a block and copy/cut it somewhere else, then I found myself always formatting the pasted text, so is there a way to quickly visually select the text again.
Every command that modified the buffer (and yanks) will set the '[ and '] marks around the changed area. So you can reformat your pasted block via:
`[v`]=
Some people go so far as to use the following mapping to reselect last modified chunk of text:
nnoremap <expr> gV '`[' . getregtype()[0] . '`]'
With this mapping you can just do gV= and format your just pasted text.
However if you have the unimpaired.vim plugin and you are pasting linewise you can use the =p command it provides. This will paste and then reformat the text in one go. It also provides some other alternative paste commands >p for example will paste one indent level deeper.
For visual mode, gv is the standard way to reselect the previous area.
If you want to toggle between start and end positions of the area, just press o.
As other answers have mentionned it, you can apply standard = command on this reselected area.
It works well after a p or P paste.
The advantage is that you do not need any custom mapping.
The way I use is straightforward. The cursor is at the beginning of the pasted text after pasting. The press V to switch to visual selection, the press '] to go the end of the pasted.
They are 3 key presses. If it is too long then you can do mapping for p
map p pV'[
map P PV'[

Paste in vim stripping newlines from the pasted text

I am copying large amounts of text from PDF documents into vim, but when you do a copy-paste from PDFs as opposed to say web browsers, everytime the PDF line breaks within a paragraph the copied text includes a newline. So when I paste into vim using 'p', I then have to press 'J' many times to collapse the newlines.
I'd much rather have a command that I can map to another letter on the keyboard that takes the contents of the copied text and pastes it removing the newlines. Any idea how I can do this?
This should do it:
map <leader>xx :let #* = substitute(#*, "\n", "", "g")<CR>"*p
Change the <leader>xx with the mapping of your choice.
What this command does, it substitues the end of line charater (\n) with nothing (""), inside the clipboard register (which is the star register). Then it pastes the text from the * register.
You can use my UnconditionalPaste plugin for that. It provides gcp / gcP mappings that force the paste to be characterwise, i.e. all newlines and indent are flattened to spaces. It also has other, similar mappings to force linewise mode, or paste with a custom separator, etc.

How to copy a word and paste it over another word in Vim?

I know how to copy a word, but I seem to overwrite what is in my clipboard because, when I try to copy over a word, it does not seem to be working.
To copy a word, I can use
bye
How to copy over a word?
Perhaps you just want to do this:
viwp
which will visually select a new word, and paste over it.
Now, if you don't want to lose your register when doing this, you can also put in your vimrc:
xnoremap p pgvy
Copy the source word as usual (e.g., with yiw) and use
viw"0p
to paste over the destination word.
Since the p command in Visual mode (see :help v_p) does not alter
the numbered register 0 containing the text from the most recent
yank command, the same copied word can be pasted over and over again.
Do this once:
ciw<C-r>0
Then to replace words always using the text you yanked do:
.
You can use search with it like this:
/foo<Cr>
.n.n.n
It works because:
ciw replaces inner word and
<C-r>0 uses the most recently yanked register to replace it, which . then also uses.
Sadly, this does not work if you visually select text you wish to replace and use ..
Note that if you originally used visual selection to select the text to replace and did c<C-r>0 then after that . will replace the same length of characters as was included in the visual selection.
When you delete a word it is put in the " register which is the default register for pasting, so when you delete the word you want to replace, it will take the previous word's place in the " register. However, the previous word will be in register 0, the one before in 1 and so on – you can at any time see this by running :reg and see the registers' contents. So, to replace a word you can first copy the first word (yiw), then “change” the word you want to replace (ciw) and then insert from register 0 by hitting ctrl-r, 0. (You could also first delete the word (diw) and then paste from register 0: "0p.
The easy solution is to do it the other way around: first paste the new word, then delete the old one.
You could use register 0, which contains the just-overwritten "clipboard" text. For your example, you could yank the text to paste, put the cursor somewhere in the word "bye", and type
ciw [cut in word; deletes the word under the cursor and goes to insert mode there]
ctrl-r 0 [register 0; paste text from register 0]
You can see what's in all the registers with :disp. As Daenyth says, you can yank into a particular register with "x[del/cut/yank command] and paste with "xp from command mode, or ctrl-r x from insert / replace.
You can yank into a register by prepending it with "a (where a is the name of the register). See How to use vim registers
b"aye

Cut to beginning/end of line, paste over to end/beginning of line

If I am in the middle of a line, how to I cut to the beginning/end of a line? (is copy different)
If I am in the middle of a line, how do I paste over to the beg/end of the line? (is pasting w/o copying over the text much different?)
thanks allot!
As Yoda said:
d$ cuts to the end of the line
d0 cuts to the beginning
d^ cuts to first non-whitespace character
To paste over the 'head' of the line:
v^p
or
v0p
To make it remember the default register, you could use the _ unnamed register:
v$"_p
Note that in that case, the overwritten text is 'forgotten' instead of yanked. (By default replacing a visual selection effectively yanks the overwritten text, so you can put it somewhere else)

Resources