In vim, how to visual select previously pasted text - vim

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

Related

Getting highlight copy and paste in VIM

I'm trying to make the switch to VIM, but there's one feature I'm really lacking and that's the highlight copy - pasting. Additionally, I am really lost with the "p" and "y" commands, I can't seem to copy or paste anything properly. I've set "clipboard=unnamedplus" to use the OS clipboard. Can you give me some pointers, or possibly a solution to my problems. It'd be nice if I could Ctrl+C / Ctrl+V things and be able to copy highlighted text to my clipboard and paste from my clipboard.
Thanks in advance.
By default, Vim yanks text and stores it in the " register; after yanking, you can see it by typing the :reg command.
However, you can control which register you want to yank or paste. For example, "aY yanks/copies the current line into register a, "cp puts/pastes the content from register c.
In Vim you have more than one "clipboard", you can "copy" many things in different "clipboards" and choose the right one to paste from. It is Vim's great advantage, isn't it?
If you want to use ctrl-c to copy selected text to system clipboard, you could:
"easier copy/paste to/from clipboard
vnoremap <C-C> "+y
nnoremap <Leader>p "+P
But I wouldn't use C-V to paste, as it is already assigned to the blockwise selection feature.
To correctly paste from outside of terminal (possibly multiple indented lines of code) into Vim, add this to your ~/.vimrc:
set pastetoggle=<F2>
Then press F2, go to insert mode (you should see -- INSERT (paste) --) and Ctrl+shift+v to paste. Then again F2 to quit insert mode.

How do you select text in vim?

In most text editors, I can select text by clicking and dragging with my mouse, and then using Ctrl-C to copy that text, or Backspace to delete it.
However, since vim runs in the console, if I highlight some text with the mouse, my vim commands don't affect what I have selected.
What is the equivalent way to select text in vim?
In vim, text is selected by entering Visual mode. This can be done in multiple ways.
v (lower case v) begins regular Visual mode, and works similar to selecting text with a mouse. Use h and l to expand the selection left and right to include more words, and use j and k to expand the selection to the lines below and above.
V (upper case v) begins linewise visual mode. This selects entire lines of text at a time. Use j and k to expand the selection up and down.
Ctrl+v(lower case v) enters block visual mode. This selects text in a block format, allowing you to select parts of multiple lines without including the entire line. Use hjkl as usual.
As #FDinoff suggested, if your terminal emulator supports it, you can even specify visual selections with the mouse by enabling mouse input with :set mouse=a.
Once you have selected the text you want, you can use all sorts of commands on them. Some of the more useful ones are:
Escape visual mode
delete the text
yank (copy) the text
paste your clipboard onto the text, replacing it
change the text, which deletes it and sets your cursor for typing
replace the text with the next character you type
yq/p search for the text elsewhere in your document
You can learn more about Visual mode by typing :help v while inside vim.
Hightlight yanked text
First of all I would like to recommend highlight yanked text:
https://github.com/machakann/vim-highlightedyank (vim and neovim)
This is useful because it will give you a visual hint of what you have just copied.
For neovim:
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END
The vim philosophy goes way beond selecting, copying etc.
Start spending more time reading about vim/neovim and you will not going back to any other editor.
Nice to meet you dear "text-objects"
Read more about them here
Copy a whole paragraph to the clipboard:
"+yip
"+ .................... clipboard register
y ..................... copy
ip .................... inner paragraph
Copy the whole file to the clipboard
:%y+
Test some vim commands from the clipboard
:#+
The above command allows you to run functions and vim commands even if did not pasted them into your vimrc, there are some exceptions but in general it will work.
You can define your own text-objects
" vim line text-objects
xnoremap al :<C-u>norm! 0v$<cr>
xnoremap il :<C-u>norm! _vg_<cr>
onoremap al :norm! val<cr>
onoremap il :norm! vil<cr>
So you can use vil or dil
Sometimes you don't need to select to copy
If you wan to copy the second line to the end of the file you can do:
:2t$
If you want to move lines 4-7 to the beggining of the file you can do:
:4,7m0
copy from mark a to mark b:
ma .................. mark current line as mark a
Jump to a second place in your file and then
mb .................. mark current line as mark b
finally:
:'a,'by+
from mark a to mark b copy to the clipboard
Diving into advanced vim:
What is your most productive shortcut with Vim?

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