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.
Related
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 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 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.
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
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)