.vimrc remap jj <ESC> not working - taken literally - vim

in my .vimrc I have the following
imap jj <ESC>
When I type jj however while in insert mode, instead of exiting insert mode VIM literally types <ESC> into my document. Hitting the escape button does take me from insert to command mode, as expected.
How do I remap jj to allow me to exit insert mode?
I am using vim 7.4 on Ubuntu 16.04

:help key-notation has this to say:
If you want to use the full <> notation in Vim, you have to make sure the '<'
flag is excluded from 'cpoptions' (when 'compatible' is not set, it already is
by default).
:set cpo-=<
This might have been accidentally set in your Vim session, and after a Vim restart the problem went away.

:set nocompatible worked for me. I think vi-compatibility mode may have been the problem.

Related

why is `ctrl -h` not work in insert mode?(version gvim 8.1.1)

In "Practical Vim" (second edition), the writer says <c-h> can delete back a character. I've tried in git bash, and it worked. However, it doesn't work in gvim in win10 as expected.
When I enter something in insert mode, I can use <c-h>, <c-w> and <c-u> before I leave insert mode. When I switch to insert mode without entering anything, <c-h>, <c-w> and <c-u> don't work.
Note that <c-h> is not mapped (:map <c-h> prints No mapping found).
Vim, by default, does only allow to delete characters that you typed since you entered insert mode. It does not allow to delete chars before the point where you started inserting. This behavior is inherited from the original Vi.
To change that, Vim has the option 'backspace'. It configures how <Backspace>, <Del>, <c-w> and <c-u> work. Add the following to your _vimrc and Vim will behave as you expect:
set backspace=indent,eol,start
Then you can backspace over autoindent, start of insert and end-of-lines.
See :help 'backspace'.

vim: remove delay when inserting text in visual block mode [duplicate]

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

How to map <Esc> to quit vim - vim quits immediately after opening if mapped to :q<CR>

I put this line in my .vimrc so that I could press escape to exit vim while in normal mode:
:nnoremap <Esc> :q<CR>
But now $ vim causes vim to flash up briefly and then exit every time. Same with $ vim filename.
Removing the line restores normal behaviour. Is there any way to get this functionality? I am pretty sure that I had it working before but can't think why it would have broken between then and now.
Vim can end up with some very strange behaviour when remapping <Esc>. I'd recommend using a different key for this if you don't want a headache.
See this question for reference: Mapping <esc> in vimrc causes bizzare arrow behaviour
Adding :set t_RV= ttymouse=xterm2 to .vimrc fixes the issue (see https://superuser.com/questions/553601/).

How to exit visual mode without a delay in Vim?

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

Ctrl-W in vim dont work in insert mode in terminal

when I use Ctrl-W (or Ctrl-H) in insert mode it does nothing. In gvim with the same vimrc works. I'm using Ubuntu 10.4 with gnome, what may be wrong?
Try adding to your .vimrc: set backspace=indent,eol,start
Type this from normal mode:
:verbose mapC-vC-wEnter
This will tell you two things:
you will see what your terminal emits for C-w (it is shown on the vim command line before you press Enter)
it will tell you whether C-w has any mappings in any mode
it will tell you where the mapping comes from
Subsequently look at
:verbose iabbrev
to see whether abbreviations are in the way
just try type "i" or "a"
maybe you terminal has reserved these key binds?

Resources