Vim imap jk <Esc> not working even with escape character - vim

i read this answer about mapping <ESC> key to jk. i tried the answer but nothing worked for me. these are i have tried in my vimrc:
inoremap jk <ESC>, inoremap jk <^[>, inoremap jk ^[, inoremap <special> jk <ESC
are they all wrong? i also tried setting cpo-=< above the mapping definition. and there is no comment after the key
here is my vimrc, the command is on the bottom of the file
im new in vim. thanks!

inoremap is spelled wrong. (I also assume you put each of those on a separate line.) This should work.
inoremap <special> jk <ESC>
<special> forces <ESC> to be act as escape regardless of what cpo is set to.

problem solved. the reason why <esc> doesnt work is because of this function in my vimrc:
if ! has("gui_running")
set ttimeoutlen=10
augroup FastEscape
autocmd!
au InsertEnter * set timeoutlen=0
au InsertLeave * set timeoutlen=1000
augroup END
endif
inoremap jk <ESC> works perfectly after getting rid of above function.
i guess its augroup FastEscape that makes <esc> mapping not working.

For those that are here because of the title, the jk mapping will not work when :set paste is enabled.

Related

How do I map <CR>/<Enter> properly in Vim

I found vimrc key remapping of / in vim-better-default plugin and added to my config.
The remapping works as expected, but this remap also causes an issue with jump using the relative number. For example, when I run 5<CR>, I need to type enter once more for it to work and it only jumps for 4 lines. The output in the bottom display is :.,.+4
I tried adding these but it did not work either.
autocmd CmdwinEnter * nnoremap <CR> <CR>
autocmd BufReadPost quickfix nnoremap <CR> <CR>
Thanks,

How to do Vim mapping on help page only

I would like to go-to a tag when pressing enter (instead of clicking with the mouse when mouse=a) or pressing ctrl-], which is a bit of a stretch for me. Is it possible to do the mapping in the help section only, such as:
:nnoremap *help* <CR> <C-]>
I do exactly that in an ftplugin for :help files: put your mapping in ~/.vim/after/ftplugin/help.vim and use <buffer>:
nnoremap <buffer> <CR> <C-]>
Create a buffer-local mapping when a help file is loaded:
:autocmd BufReadPost $VIMRUNTIME/doc/help*.txt nnoremap <buffer> <CR> <C-]>
If the "help page" you meant is vim doc/help, they have ft=help. You can verify it by :echo &ft or :set ft?.
Then it is easy if you want to create mapping only for those filetypes. You can do it using autocommand:
autocmd FileType help map....
If you want it to be buffer local mapping, you add <buffer> in mapping.

Prevent certain command mappings while in NERDTree window in Vim

I have the following keys mapped in my .vimrc file:
noremap <silent> <C-h> :bprev<CR>
noremap <silent> <C-l> :bnext<CR>
The commands they execute are provided from the buftabs script.
What I would like to do is prevent those key mappings from being executed when I'm in the NERDTree split. The reason for this is if the commands are run while in NERDTree, a file buffer gets loaded in the split instead. Then, to fix it, the window needs to be closed and opened again.
This is a similar problem as explained in another question, but the problem there was corrected by configuring the plugin, while the buftabs script does not have such an option.
In order to disable a mapping in certain buffers, one can define
a buffer-local mapping for the same key sequence, overriding the
original mapping with a no-op:
:autocmd FileType nerdtree noremap <buffer> <c-h> <nop>
:autocmd FileType nerdtree noremap <buffer> <c-l> <nop>
(See :help :map-arguments and :help <nop> for details on
<buffer> and <nop>, respectively.)
I updated my vimrc by looking at ib.'s solution.
autocmd FileType nerdtree noremap <buffer> <A-PageDown> <ESC>:wincmd w <bar> bnext<CR>
autocmd FileType nerdtree noremap <buffer> <A-PageUp> <ESC>:wincmd w <bar> bprevious<CR>
It goes back to the previous window and executes the command.

How do I map ctrl x ctrl o to ctrl space in terminal vim?

After searching a bit on the net it seems that I can't map CtrlSpace to anything/alot. Is there a way to do it today, what I found was usually 2 years old.
I've run into the same issue, the short answer is yes you can, and not only in the gui version. Adding this on you .vimrc is enough:
inoremap <C-Space> <C-x><C-o>
inoremap <C-#> <C-Space>
The problem seems to be that Terminal.app doesn't interpret <C-Space> correctly and Vim understands it as <C-#> which is a built-in mapping (:help CTRL-#).
Maybe you could go with something like the following in your .vimrc:
if !has("gui_running")
inoremap <C-#> <C-x><C-o>
endif
which seems to work, here, but I don't like the idea of overriding built-ins like that.
Instead you should try with <Leader> (:help leader), it gives you huge possibilities for defining your own custom mappings and (depending on the mapleader you choose) won't interfere with OS/app specific shortcuts/limitations and hence be more portable.
With this in my .vimrc:
let mapleader=","
inoremap <leader>, <C-x><C-o>
I just hit ,, to complete method names.
The nitpicker broke pablox solution. The crux of the solution was just about remapping. So when you disable remapping, it cannot work.
If you really want to throw in a noremap, this is what it looks like:
inoremap <expr><C-space> neocomplete#start_manual_complete()
imap <C-#> <C-Space>
What will not work: inoremap <C-#> <C-Space> 'cause the <C-Space> part will not be remapped itself.
Have you tried :inoremap <c-space> <c-x><c-o> ?
Does CtrlX CtrlO do anything when you type in insert mode? Is omnifunc set?
Add the following code to ~/.vimrc:
" Ctrl-Space for completions. Heck Yeah!
inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-#> <C-Space>
Source: https://coderwall.com/p/cl6cpq
To accommodate both Windows and Linux I applied this to ~/.vimrc
if has("unix")
inoremap <C-#> <c-x><c-o>
elseif has("win32")
inoremap <C-Space> <c-x><c-o>
endif
I had better results with this set of mappings across all modes on Mac OS. Have not tested Windows or Linux.
I don't understand how the excepted answer is supposed to work in terminal mode.
inoremap <C-space> <ESC>
vnoremap <C-space> <ESC>
cnoremap <C-space> <C-c>
" When in terminal, <C-Space> gets interpreted as <C-#>
imap <C-#> <C-space>
vmap <C-#> <C-space>
cmap <C-#> <C-space>
Like the others said, using inoremap with the correct key for your term (as discovered using i_Ctrl_v) should work. I will add to this another possible cause for problems with insert mode mappings: paste mode. As the docs state:
When the 'paste' option is switched on (also when it was already on):
- mapping in Insert mode and Command-line mode is disabled
This may seem irrelevant, but this very thing tripped me up trying to get a similar inoremap binding to work in Vim 8.2. I had set paste in my .vimrc, and had to chop it up with :finish statements (as [recommended in the vim faq) to isolate the line causing the problem.

Plugins breaking vim mappings?

I just installed DelimitMate and CloseTag plugins for MacVim and they seem to have broken these two mapping in my .vimrc:
"F7 WordProcessorOn
:map <leader>w :set linebreak <CR> :set display+=lastline <CR> :set wrap <CR> :setlocal spell spelllang=en_gb <CR>
"F8 WordProcessorOff
:map <leader>c :set nowrap <CR> :set nospell <CR>
Does anyone have any ideas as to what the problem could be? Thanks.
You can find out what is 'hijacking' the mappings by doing
:verbose map <leader>c
:verbose map <F8>
etc.
But likely your problem is with submappings firing: use
:noremap <leader>c .......
to prevent remapping
PS: consider mapping for specific modes (nnoremap over noremap); Being explicit avoids funny interfering mappings a lot of the time

Resources