Using key combinations in .vimrc - vim

imap ( ()<ESC>i
imap { {<CR> <CR>}<ESC>i<Up><Tab>
imap [ []<ESC>i
imap <S-BS> <Del>
I decided to create auto bracket and there I was a need key combination for fast delete double bracket (4 line in code). But it doesn't work. What's the problem? It work if I overwrite <S-BS> to (for example) symbol *.

When you do <S-BS>, Vim sees <BS>. To verify this, do CTRL-V then backspace or shift+backspace in insert mode. This will insert ^? for both, which is the terminal code for those special keys. See :help c_CTRL-V. So both backspace and shift+backspace have the same terminal code, which means Vim can't tell the difference between the two.
The mapping does work in gVim though.

Related

VIM: set term=xterm changes <BS> to <Del>, is it reversible?

I have added set term=xterm to my vimrc to be able to use 256-color vim schemes in terminal, but it comes at a price (at least for me). It also replaces (sort of) the BackSpace with Delete (i.e. BackSpace starts to delete forward just like Delete does) in insert mode and makes it "BackSpace" instead of moving left (or h) in normal mode. I have nothing against Ctrl-H as a way "to Backspace", but I simply don't want to have two delete buttons and ability "to BackSpace" (delete backward) in normal mode.
How can I reverse that change while retaining the setting I need?
PS I've read :h CTRL-h and a bit of :h xterm, but I couldn't find the solution.
Vim's inoremap and nnoremap commands can be used to adjust how keys are interpreted in Vim.
A solution is documented here for your specific context: https://conemu.github.io/en/VimXterm.html
The relevant quote:
"If you have problems with BS in Vim (BS acts like Delete key) under ConEmu when term=xterm, you may try to remap BS key:
inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>
"
In general, when a key does not do what you want, the trick is to find out what it actually sends to Vim. Sometimes hitting Ctrl-V followed by that key in insert mode might help figure it out. Then inoremap and nnoremap as shown above can be used to reassign it to the behaviour you want in insert and normal modes, respectively.

I can't use Delete key to delete a character (its normal functioning) in Vim on tmux

I played around with tmux config. I love tmux and I don't want to stop using it, but for some reason the delete key doesn't delete anymore. It deletes a character and works normally in vim, but not in vim when opened from tmux. Backspace works fine. Just the delete key stopped working only in vim running on tmux. Any ideas?
You may need to remap the delete key in your .vimrc. For instance, like this:
inoremap ^? <c-h>
cnoremap ^? <c-h>
" Debian-specific variation
inoremap <esc>[3~ <c-h>
cnoremap <esc>[3~ <c-h>
I have both of these in my .vimrc, and have for years, and it's worked everywhere for me. With or without tmux.
I will note though that the ^? in the first two lines is NOT the characters ^ and ?, it is a single embedded delete character. The way to insert this is to edit your .vimrc with vim, and use this sequence to insert it:
Press CtrlV, then press Delete.
If done properly, Vim should show it to you as a ^?, but it is a single character in the file. The rest of the lines are typed exactly as they are shown.
This will remap the delete key to send backspace, and you can use them interchangeably.

Adding to mapping rather than completely remapping

I'm working on a plugin to allow bracket completion (I know it's available, it's more of a learning exercise). To properly implement it, I need to add to the backspace mapping. However, as it's an important key, I'd rather keep the existing functionality and just add to it rather than reimplementing the functionality. The steps would basically be when in insert mode and press backspace, execute the original backspace key, then check for some conditions and maybe remove more characters.
I've tried something like imap <backspace> <backspace><call_func_here>, but that doesn't seem to work. Again, I know I could remap backspace to just the function and try to recreate the backspace functionality, but I'd prefer to not do that.
Is this possible in vim?
I think what you are trying to do is the following:
inoremap <silent> <BS> <BS><C-o>:call MyFunction()<CR>
inoremap allows to create a non recurrent mapping in insert mode (it is often a good idea to use nore in your mappings). :h :inoremap
<silent> precise that the mapping will not be echoed on the command line (You will not see :call MyFunction() in the command line) :h :map-silent
<BS> is the reference to the backspace key that you want to remap.
The second <BS> is here to issue a backspace in insert mode
<C-o> switches to normal mode for only a command. :h i_CTRL-O
:call MyFunction() is the call to your function the way you would do it in normal mode.
<CR> correspond to the Enter key which validate the call to your function.

Remap CTRL-A in command line mode

How can I remap my CTRL-A to CTRL-Q in the command line mode?
I use VIM within Tmux session (which has CTRL-A set as a prefix). When I try to insert all files which names match the pattern (i.e. for :bd command) I have to press CTRL-A twice (as it's also a prefix for tmux). I would like to use one key stroke <C-a> for that.
I've tried to put cnoremap <C-q> <C-a> to my .vimrc but it doesn't work. Any suggestions?
This works for me; note that in the terminal, Ctrl-S and Ctrl-Q are special sequences for flow control. See here for how to unconfigure that, or use another left-hand side for your mapping, e.g. <C-g>.
Thank you guys for letting me know that is special key, I wasn't aware of that.
In that case I decided to use different key (<C-b>). Key map for that:
cnoremap <C-b> <C-a>
I know that <C-b> is for scrolling but I guess in command line mode it should be ok to use that key.

Vim imap jk <Esc> not working

In my vimrc, I've remapped jk to escape using imap.
The problem emerges when I use jk; it doesn't escape, it only echoes out <Esc>.
Here's the entry in quiestion:
imap jk <Esc>
It's also a good point to make that I'm also using vim-X11 in fedora.
It looks like your 'cpoptions' settings contains <; then, special key codes like <Esc> are not recognized.
Find out where this got set via
:verbose set cpo?
or re-write the mapping to use a literal Escape character; enter it via Ctrl + V Esc; it should appear as ^[ in the buffer.
PS: Though not related, you should usually use :inoremap unless remapping is required.
For those who are here because of the title, the jk mapping will also not work when :set paste is enabled.

Resources