gVim 8.0
When I press Ctrl+V in insert mode (or in Ctrl+: mode) I got this v-like symbols inserted instead of my current clipboard text.
Why does it happen? How do I get it back to normal?
Thank you.
CTRL-V means "Insert next non-digit literally." I.e. if you press Ctrl-V twice vim inserts "CTRL-V" into the text and displays it as "^V".
To paste a buffer switch to normal mode and use p or P. You can temporary switch to normal mode with Ctrl-O; e.g. Ctrl-Op.
The problem was caused by the omission of this line: source $VIMRUNTIME/mswin.vim from _vimrc file.
If you are on insert mode use Ctrl-r + .
Looking at :h registers you will see that + means clipboard register and Ctrl-r helps us to insert any register on insert mode.
In normal mode you can just type: "+p
As being said Ctrl-v on insert mode is used to insert special chars. To learn more :h i_Ctrl-v
*i_CTRL-V*
CTRL-V Insert next non-digit literally. For special keys, the
terminal code is inserted. It's also possible to enter the
decimal, octal or hexadecimal value of a character
|i_CTRL-V_digit|.
The characters typed right after CTRL-V are not considered for
mapping.
Note: When CTRL-V is mapped (e.g., to paste text) you can
often use CTRL-Q instead |i_CTRL-Q|.
Related
In vim, when you type a in normal mode, you go into insert mode after cursor. But daw will delete word under cursor and caw will change word under cursor. I use these combinations very often, it very helpful. But what does a mean in this context and can it be useful in other cases?
Vim being a (the?) modal editor, the keys on your keyboard have different meanings depending on which mode you are in.
In normal mode, a means "append". It enters insert mode and whatever key you press after that is inserted in your buffer.
In normal mode, c, d and y are called "operators". When you press one of those keys, you quit normal mode and enter another mode called "operator-pending mode" where Vim waits for you to feed it a motion or text-object.
In operator-pending mode, a single a means nothing but aw is one of those text-objects and could be translated as "around word". There's also iw for "inner word" or at for "around tag" and many others…
Reference:
:help vim-modes
:help navigation
and more specifically
:help operator
:help text-objects
:help word-motions and following
Based on the idea of quick command in insert mode I want to insert my OS clipboard when I am in the insert mode. To make that happen as I want, I have to add a whitespace in the inoremap call, but I do not know how?
This is done with
inoremap VV <Esc>"+gP
Using this:
"vim" is in the OS clipboard
Typing in insert mode "work smart with VV"
leads to the result
work smart withvim
What I want is a whitespace between with and vim
work smart with vim
Any suggestions?
Your issue is caused by the P in "+gP and by the fact that leaving insert mode moves the cursor one character to the left, on the <space>.
P pastes before the cursor so your mapping pastes before the <space>. Changing the P to p should "fix" your problem, in a superficial way.
Here is a more solid alternative that inserts the content of the clipboard register right after the cursor without leaving insert mode:
inoremap VV <C-r>+
Well… what about simply using <C-r>+?
Working around a side effect (here, pasting after the cursor) is not the same as avoiding that side effect (here, not leaving insert mode to begin with). Guess which one is the right approach? ;-)
Use
inoremap VV <Esc>"+gp
P places the clipboard before cursor, p after cursor.
Option 1.
inoremap VV <C-R><C-o>+
Ctrl-R tells vim to insert the contents of a register, and + is the OS clipboard register. It inserts the clipboard contents as if you typed them. The additional <c-o> makes the register contents get inserted literally, so that things like <esc> or ^H (backspace) aren't interpreted like you typed them, but are inserted as text.
Option 2.
inoremap VV <C-o>"+gp
C-o tells vim to go to normal mode for just one command, so you don't need to add <Esc> at start, or i at the end.
I know I can do this by pressing Insert in INSERT mode, but that requires some stretching. Is there any more convenient shortcut to go directly from NORMAL mode to REPLACE mode?
From the ViM manual:
5. Replace mode *Replace* *Replace-mode* *mode-replace*
Enter Replace mode with the "R" command in normal mode.
Of course you can map any key to R, for example by doing
:map <F5> R
You can press R and you'll get into the REPLACE mode.
R brings you into replace mode.
You have to press R to go to replace mode. For this, you must first be in non-editing mode
In normal mode, press Shift+R.
r will replace a single character.
I'm using imap <c-v> <ESC>"+PA in gVIM to do the pasting , however , each time i tried to paste i got unexpected result:
aa${CURSOR}aa , and press ^V right now , i got a${PASTE_TXT}aaa , but i wanted aa${PASTE_TXT}aa.
How can i fix this ?
I would suggest that you do not remap CTRL-V, it is really useful when you want to insert raw characters. In order to paste in insert mode, you needn't switch to normal mode. Use CTRL-R then CTRL-O, then +. That's not so long. You can remap an F-Key to do that:
:inoremap <F1> <C-r><C-o>+
or
:inoremap <C-v> <C-r><C-o>+
See :help i_CTRL-R for more reference. You might also like this answer I gave about registers.
If you still want to keep your mapping using normal mode, replace P with p and A with a. After all you want to paste after the last character you have stopped when leaving insert mode, and to continue inserting after pasted text, not at end of line.
:inoremap <C-v> <Esc>"+pa
Change imap <c-v> <ESC>"+PA to imap <c-v> <ESC>"+pA
Upper case P is paste before cursor position, lower case p is after cursor position.
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..