vim: copy into system clipboard not working - vim

In my .vimrc I have those two lines for copy and paste into the system clipboard:
nnoremap <Leader>p "+p
nnoremap <Leader>y "+y
Weirdly the remapping of pasting works fine, but I can't copy any lines. If I press the buttons "+y it works and the highlighted text is in my system clipboard, but if I use < Leader> y it is only copied into a register inside vim, but not into the system clipboard.
Some help would be great!

If you use your mapping in Visual mode, you have to use vnoremap or noremap, instead of nnoremap (which is for Normal mode only).
See :h map-overview for details.

Your yanking mapping is incorrect. "+y lacks a motion. If you want to yank the whole line use upper Y ("+Y) or or yy ("+yy).
:help y
*y* *yank*
["x]y{motion} Yank {motion} text [into register x]. When no
characters are to be yanked (e.g., "y0" in column 1),
this is an error when 'cpoptions' includes the 'E'
flag.
Note that, if you type "+y, vim hangs in "operation-penging mode" (:help Operator-pending-mode).

Related

Mapping select-all, copy, paste in vim

I have the following map in my vimrc:
nnoremap <C-a> ggVG
nnoremap <C-c> "*yy (might be because I'm in visual mode here?)
nnoremap <C-v> "*p
The select-all (ctrl-a) and the paste (ctrl-p) works, but the (ctrl-c) does not work with that shortcut, though it works if I manually type in the command after doing ctrl-c.
What needs to be fixed here?
The first issue I would like to address is that your mapping for copying the text, nnoremap <C-c> "*yy, will only work in normal mode. When you select text in Vim, you enter visual mode, and the first n of nnoremap makes the mapping work in normal mode only.
You can make your mapping work by using noremap (all modes), vnoremap (visual and select mode), or xnoremap (visual mode only), like this:
vnoremap <C-c> "*y
You can find more information about mappings in the documentation.
Another thing to note is that the default function of Ctrl-c is to cancel/interrupt the current command. For example, if you enter insert mode and press Ctrl-c, you will exit insert mode and go back to normal mode. With your original mappings, it will cancel the selection (exits visual mode) without copying anything.
This works for me in Neovim but I believe it should also work in Vim as well. To yank all the content I have the following mapping in my configuration:
nnoremap <leader>ya ggVGy<C-O>
Details:
gg: go to the first line
V: select the first line
G: go to the last line
y: yank selection
<C-O>: go to the previous cursor position

Map <S-Insert> to paste content of clipboard in a filetype

I want to use <S-Insert> to paste the content of clipboard in vim irrespective me being in normal or insert mode
I am using [Fedora 23, Gnome terminal 3.18.3, vim 7.4 +xterm_clipboard]
The usual problem I have is:
I go on the web and Ctrl-C on multiple lines
Back to vim in Insert or Normal mode I want to paste WITHOUT indentation modification
And I get a paste with a messed up indentation but only when the file being copied to as a filetype like r/python (I guess because an indent is defined)
What I have found
I found this post (amongst others) but that's not working.
How I can make it work
When I use the paste mode it works
For example if I do
I go on the web and Ctrl-C on multiple lines
In vim :set paste
I do <S-Insert>
Then it works irrespective of mode OR filetype and I can :set nopaste
Another way is to paste from clipboard "+p which works (without messing with paste mode)
Leads ??
Thing is I am not even sure this is vim related, I think I cannot map <S-Insert> as if I do
nnoremap <S-Insert> <NOP>
inoremap <S-Insert> <NOP>
vnoremap <S-Insert> <NOP>
And start new terminal then vim and try <S-Insert> it still copies
I get a paste with a messed up indentation
This is because vim assumes the copied text as a stream of characters typed in to it.
If you can use system clipboard in vim, use these mappings
:map <silent> <S-Insert> "+p
:imap <silent> <S-Insert> <Esc>"+pa
You can find if vim has clipboard support using
vim --version
If you see "+xterm_clipboard", you are good to go. Use it with
set clipboard=unnamed " or
set clipboard=unnamedplus
If using system keyboard is out of the picture, my suggestion would be to write a function that takes you to paste mode with set paste, pastes the copied text, takes you back to normal mode with set nopaste

How to use system clipboard in VIM

Here is scenario, In VIM, I copy the string(e.g. /home/redhat) from the file, and then use this command :! cd XXX(the path I have copied from the buffer, in this case it should be /home/redhat).
My question is, what command should I use to paste the string?
"+y is copy from system clipboard in vim, and "+p is paste from system clipboard, you can map them to normal key bindings.
noremap <C-C> "+y
noremap <C-V> "+p
" Note that mapping <C-V> to paste from system clipboard conflicts with vertical mode I suggest you remap vertical mode to <C-Q>
noremap <C-Q> <C-V>
for other mappings, you can check mswin.vim
If you do not care about system clipboards, y is copy and p is paste in normal mode.
You don't need the system clipboard at all for your use case:
:!cd <C-r>"
In insert mode and in Vim's command-line, <C-r>{register} inserts the content of that register at the cursor. Since you yanked /home/redhat, the content of the unnamed register, " (see :help registers), is /home/redhat and it's inserted right where you typed <C-r>":
:!cd /home/redhat
FWIW, you can also insert the file path under the cursor without yanking and pasting:
:!cd <C-r><C-f>
After :, you're in command-line mode. Like in insert mode, you can insert register contents at the cursor position by pressing Ctrl + R, followed by the register name. So, if you simply yanked the /home/redhat, that would be " for the default register; for the system clipboard, use + instead.
In the command-line, you can also press Ctrl + F to switch to the command-line window, in which you can use all Vim commands, like in any other buffer, so you could also paste via the usual p / P from normal mode.
Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor. The help topics for the commands mentioned here are :help c_CTRL-R and :help c_CTRL-F.
With :new /path/to/file you go to new file. Then paste the information with P (always out of mode Insert).

vim: how to select pasted block

Is there a vim command to directly select a block of text which has just been pasted?
ps. I know about gv to reselect a block after exiting visual mode. It doesn't apply to this case.
If you want to select it just after paste (before you change anything else), use
nnoremap <expr> gV "`[".getregtype(v:register)[0]."`]"
. [ and ] marks point to start and end of the last change, v:register is set to the last register used (which is register used for the paste command unless you, for example, yank something), [0] selects only first byte of register type (it is required because for blockwise register it returns <C-v>{width}) and register type is one byte which is just the same as the keystroke you should use in normal mode to invoke visual mode.
I saw this solution somewhere on SO, you may want to search for it in order to get some alternatives.
In my case I have this map:
:nnoremap gp `[v`]
After more research I think the better solution is:
" https://vim.fandom.com/wiki/Selecting_your_pasted_text
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
I have had the following maps in my vimrc forever:
nnoremap <leader>p `[V`]
nnoremap <leader>[ `[V`]<
nnoremap <leader>] `[V`]>
They do the following:
visually select the recently pasted block
de-indent the recently pasted block
indent the recently pasted block
I probably use the indent ones even more than the selection one.

Paste in insert mode?

Is it possible to paste in insert mode in Vim?
While in insert mode hit CTRL-R {register}
Examples:
CTRL-R * will insert in the contents of the clipboard
CTRL-R " (the unnamed register) inserts the last delete or yank.
To find this in vim's help type :h i_ctrl-r
If you don't want Vim to mangle formatting in incoming pasted text, you might also want to consider using: :set paste. This will prevent Vim from re-tabbing your code. When done pasting, :set nopaste will return to the normal behavior.
It's also possible to toggle the mode with a single key, by adding something like set pastetoggle=<F2> to your .vimrc. More details on toggling auto-indent are here.
No not directly. What you can do though is quickly exit insert mode for a single normal mode operation with Ctrl-O and then paste from there which will end by putting you back in insert mode.
Key Combo: Ctrl-O p
EDIT: Interesting. It does appear that there is a way as several other people have listed.
While in insert mode, you can use Ctrl-R {register}, where register can be:
+ for the clipboard,
* for the X clipboard (last selected text in X),
" for the unnamed register (last delete or yank in Vim),
or a number of others (see :h registers).
Ctrl-R {register} inserts the text as if it were typed.
Ctrl-R Ctrl-O {register} inserts the text with the original indentation.
Ctrl-R Ctrl-P {register} inserts the text and auto-indents it.
Ctrl-O can be used to run any normal mode command before returning to insert mode, so Ctrl-O "+p can also be used, for example.
For more information, view the documentation with :h i_ctrl-r
You can use this to paste from clipboard with Ctrlv:
set pastetoggle=<F10>
inoremap <C-v> <F10><C-r>+<F10>
And this for yanking visual selection into clipboard with Ctrlc:
vnoremap <C-c> "+y
If you also want to use clipboard by default for classic vim yanking/pasting (y/p) in normal mode, here is a config option that does it:
set clipboard=unnamedplus
With this configs you can e.g. yank first in normal mode and then paste with Ctrlv in insert mode. Also, you can paste text from different vim instances and different applications.
Another option is:
set clipboard=unnamed
Then you will be able to just select something by mouse dragging in your X environment and paste it into vim afterwards. But (for some reason) you won't be able to yank something (y) in Vim and shiftinsert it somewhere else afterwards, which is probably quite limiting.
Vim docs about this: http://vim.wikia.com/wiki/Accessing_the_system_clipboard
For pasting from custom registers you can follow the other answers :). This answer is mainly about integrating Vim with your system clipboard.
Note that for set clipboard=unnamedplus and set clipboard=unnamed to work, you need to use gvim or vimx (vim-X11): Those are compiled with +xterm_clipboard. You can optionally put this into your .bashrc to alias vim with vimx:
if [ -e /usr/bin/vimx ]; then
alias vim='/usr/bin/vimx'; # vim with +xterm_clipboard
fi
You can find out whether or not your vim has the +xterm_clipboard in the information provided by vim --version.
If you set Vim to use the system clipboard (:set clipboard=unnamed), then any text you copy in Vim can be pasted using Shift + Insert. Shift + Insert is simply an OS-wide paste key-combination (Ctrl + Insert is the corresponding 'copy').
You can also use the mouse middle button to paste in insert mode (Linux only).
You can enter -- INSERT (past) -- mode via:
Keyboard combo: y p
or
:set paste and entering insert mode (:set nopaste to disable)
once in -- INSERT (past) -- mode simply use your systems paste function (e.g. CtrlShiftv on Linux, Cmdv on Mac OS).
This strategy is very usefully when using vim over ssh.
Yes. In Windows Ctrl+V and in Linux pressing both mouse buttons nearly simultaneously.
In Windows I think this line in my _vimrc probably does it:
source $VIMRUNTIME/mswin.vim
In Linux I don't remember how I did it. It looks like I probably deleted some line from the default .vimrc file.
Just add map:
" ~/.vimrc
inoremap <c-p> <c-r>*
restart vim and when press Crtl+p in insert mode,
copied text will be pasted
Paste in Insert Mode
A custom map seems appropriate in this case. This is what I use to paste yanked items in insert mode:
inoremap <Leader>p <ESC>pa
My Leader key here is \; this means hitting \p in insert mode would paste the previously yanked items/lines.
Add this to vimrc or init file:
imap <silent> PP <ESC>pa
..to paste in insert mode with "PP" and stay in insert mode..

Resources