I use Arpeggio to map jk to ESC
So when I press jk simultaneously I can exit to normal mode very quickly.
I found this trick from
Arpeggio plugin page
This is what I put in .vimrc
Arpeggio inoremap jk <ESC>
Exiting from Insert mode works perfectly, But I can not exit from Visual Mode.
It would be nice if I can exit both mode so I can stick with this key, It save my time a lot.
Any idea?
Edited:
I can not exit Insert(paste) mode too
:set paste
inoremap
is for insert mode mappings, use
xnoremap
for visual mode.
Assuming that plugin is able to handle visual mode mappings, duplicating that line in your ~/.vimrc and replacing the i with a x should be enough.
I need paste mode if I want to paste big files into a buffer, while using all of my plugins, syntax highlighting etc. Without paste mode this would take forever or even hang, depending on the filesize...
Therefore I have set a "paste toggle on F3" keybinding:
set pastetoggle=<f3>
So whenever I press F3 it will toggle paste mode on/off.
Additionally, to exit paste mode whenever I leave insert mode I have this auto-command in vimrc:
autocmd InsertLeave * set nopaste
So after I have pasted something (being in insert mode afterwards) I can press Ctrl-o or ESC and paste mode is turned off automatically.
Related
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
I'm using VIM 7.4 and I have this mapping in my .vimrc, imap jk <ESC> to get out of insert mode using jk but the problem is it doesn't work when I'm in insert mode while the paste option is set.
According to this wiki Mapping keys in Vim - Tutorial (Part 1)
if the 'paste' option is set, then insert mode maps are disabled.
Is there a way/mapping to make imap jk <ESC> work even when paste is set?
No. It's by design that all insert mode mappings are disabled. Having maps work in paste mode would screw up pasting which is counter productive.
You should only be in paste mode if you are pasting text. (So you should spend as little time as possible in this mode)
The only special key in paste mode is the paste toggle key so you should set that and hit it to exit paste mode. To set paste toggle to f9 you use
set pastetoggle=<f9>
After this setting is set hitting <f9> will enter and leave paste mode. Once you are no longer in paste mode your mappings will work again
Read :h 'pastetoggle'
I use vim on windows and linux. On linux I would like to set ctrl-Q to visual block selection, but still maintain behave mswin which sets ctrl-v to paste.
How can I keep behave mswin and use ctrl-Q for visual block mode?
edit: I though mswin would also map ctrl-Q to visual block mode, but in vim-gnome ctrl-Q does nothing
first of all, I highly recommend you to forget the windows vim shortcuts if you work on a linux box. such as: ctrl-v, ctrl-q, ctrl-c ...
well you must think this isn't the answer to your question. now I post the "answer".
to make ctrl-q work as ctrl-v (block selection) on a linux box, you have to tell, you work with gvim or vim in terminal.
Gvim
If it was gvim, it is easier, just create a mapping, like:
nnoremap <c-q> <c-v>
Terminal Vim
If you want to make <c-q> work in your terminal vim, you need to understand the default <C-q> has special meaning in your terminal settings.
In your terminal, pressing <c-q> will sent stty start signal. This is important when you first stop your terminal output scrolling(by ctrl-s), and then you want to resume. That is, in terminal vim, if you pressed C-q, it will be captured first by terminal. You can of course change that rule, by disable the stty start definition. like:
stty start undef
you could add this to your .bashrc file (I assume you were using bash) if you want to make it as default.
with this line executed, you can create the same mapping nnoremap <c-q> <c-v> in your vim, and pressing <c-q> in normal mode, vim is gonna enter block-wise selection mode.
After all, again, I suggest you forget the windows mapping if you work on linux box.
If you don't want to change your terminal settings (stty start undef to allow using ctrl q as mentioned in Kent's answer) to be able to use ctrl v for paste, you can make ctrl v paste only in visual and insert modes, and make it block select in normal mode:
" Paste from clipboard when in insert mode.
imap <C-V> <ESC>"+gpa
" Paste from clipboard when in visual mode. (Replace whatever is selected in visual mode.)
vmap <C-V> "+gp
You can also copy to the clipboard from visual mode:
" Copy selection to clipboard when in visual mode.
vmap <C-C> "+y
In Vim, when in Visual mode, I have to press Esc twice to exit it and turn off the selection. After one press of Esc I have to wait 2 seconds for the selection to turn off.
What can I do to exit Visual mode immediately when Esc is pressed?
Executing following command helped me:
set timeoutlen=1000 ttimeoutlen=0
see: http://www.johnhawthorn.com/2012/09/vi-escape-delays/.
As Ingo explained. Just thought I would post the solution:
https://github.com/Greduan/dotfiles/blob/47f92e4db29d4ead778d877a85082b271de130ed/vim/vimrc.vim#L332-L346
Works pretty well. It's a little bit confusing for me as well, so I can't really explain, but the code explains itself pretty well.
The point is it works, it simply makes <Esc> work immediately even when on Terminal. I believe if you do have mappings set to <Esc> it'll give you time to do those as well. However I'm not sure.
EDIT
Studied a bit and I can now explain it. Basically, if you're not using a GUI (like MacVim) then when you enter insert mode the ttimeoutlen will be set to 0. Meaning that as soon as you click <Esc> that'll work. However once you're in normal mode then it'll set the ttimeoutlen to the number you prefer, letting you do mappings with <Esc>.
Perfect solution I think, since if you have mappings in insert mode it'll be using control or something like that.
EDIT 2
Here's the code:
set timeout " Do time out on mappings and others
set timeoutlen=2000 " Wait {num} ms before timing out a mapping
" When you’re pressing Escape to leave insert mode in the terminal, it will by
" default take a second or another keystroke to leave insert mode completely
" and update the statusline. This fixes that. I got this from:
" https://powerline.readthedocs.org/en/latest/tipstricks.html#vim
if !has('gui_running')
set ttimeoutlen=10
augroup FastEscape
autocmd!
au InsertEnter * set timeoutlen=0
au InsertLeave * set timeoutlen=1000
augroup END
endif
With time I've removed the condition that the GUI isn't running and it still works as far as I can tell.
A quick workaround is using <C-c> instead, but you probably want to fix the timeout on <Esc>, which is caused by a mapping that starts with <Esc>, which makes Vim wait for 'timeoutlen' to check whether the mapping is complete.
This does not necessarily need to be a "real" mapping; many terminal workarounds (e.g. to make certain keys work) advise to set up such a mapping. (Unfortunately, this is a difficult and complex issue.)
You can find the mapping via:
:verbose map <Esc>
I have no mapping bound to <ESC> globally or for Visual mode (calling :verbose vmap <ESC> gives no results) but there is still a significant delay when exiting Visual mode. Even on fresh installs with no vimrc the delay is present. Using <C-c> does exit visual mode without delay.
Since I don't like pressing <C-c> to exit any mode, I currently map <ESC> to <C-c> in visual mode. This exits visual mode using <ESC> without any delay.
:vmap <ESC> <C-c>
Or put the following line in your vimrc
vnoremap <ESC> <C-c>
This will not work if you do have global or visual mode mappings bound to <ESC>.
First try the accepted answer of adding the following to .vimrc
set timeoutlen=1000 ttimeoutlen=0
If that doesn't work check if you have any keybindings to <esc>
:imap <esc>
If you use tmux you also need:
set -sg escape-time 0
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..