paste data at the start of line in visual mode - vim

I can select lines using SHIFT + V, then selecting lines using up down left right keys, then copy them using y (yank them) and paste them using p (put).
I can similarly select data block using CTRL + V, then selecting lines using up down left right keys, then then copy them using y (yank them) and paste them using p (put).
But when I am pasting data blocks using p, it always pastes data after the current cursor location. Which means, if I want to paste to the beginning of lines, it won't work - it copies the data after the first character. So how can I paste data block at the beginning of a line in vim in visual mode?
Curently I do this by pasting at the second cursor location, deleting the characters at the beginning of line, and then pasting them after the previously pasted block.

select and yank as what you are doing, when you paste press P instead of p. The content in " register will be put before your cursor.
also note that: with p or P, after your pasting, the cursor will stay at the beginning of the just pasted content. If you want your cursor to be at the end of pasted text, use gp or gP.

I think IC-r" could be close to what you want

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'[

After pasting how to jump to other end of inserted text

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.

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

How to duplicate a selection and place it above or below the selection

If I have something selected in Vim in visual mode, how can I duplicate that selection and place it below or above the selection?
Press y to yank what you've got selected visually, then p to paste below the cursor or P to paste above it.
And since you asked about pasting below the selection block, I'll copy what michael said below: After you y to yank, use '> to move it to after the selection block, and then p to paste.
Since I do this a lot (select a block, yank, go to end of last visual selection, paste) I set up a visual block shortcut under CTRL+P (prior to this, CTRL+p seems to be the same as j in a visual block).
vmap <C-p> y'>p
Now it's just making a visual selection and pressing CTRL+p.
In addition to the V...yp combo you might want to know about some jumps '< and '> to get to the last character of the previous visual mode text. Specifically, if you want to paste below you'd go V...y'>p. If it's a long multiline it may be handy.
It's one of those jumps you may find handy if you're doing this a lot.
Use y to yank (copy) the selection into a buffer.
Use p to paste the selection where you want it to be.
You have two options:
yy which copies the current line, then p to paste.
Make a selection, with v for example, then copy with y and paste with p.
Do you want to copy/paste the whole line? If so, get out of visual mode, then use yy to yank the whole line, then p to paste.

Resources