Show pressed keys in VIM normal mode statusline - vim

I use VIM, the Colemak keyboard layout, and a Microsoft Natural 4000 keyboard. Since the Natural 4000 has differently-shaped keys, I could not change the keycaps. This means each key is labeled with an "incorrect" key -- I press the key that says "O" and it types "Y".
That's fine in normal use, because I touch-type smoothly with Colemak and don't make many typos. But with VIM it presents a bit of a problem, because I often need to hit a key "out of sequence" -- i.e, just suddenly hit "g" without any context. For some reason that's harder to do, and I keep hitting the wrong keys.
I'd like to be able to hit a prefix key (say, the leader key twice -- \) and then have any further keys I hit within 1-2 seconds shown on the status line.
This way, I could quickly hit \ and then tap a key or two, and see what key it "really" is.
Any ideas how I could make that happen?

Try set this
:set showcmd
It displays your keystrokes in statusline as you enter them, without any need for a prefix key.
you can also turn it off by using below command in normal mode of vim (pressing Esc key).
:set noshowcmd

Related

In Vim, the 5-key on the numeric keypad inserts a line above the current line with "E"

While using Vim under Linux Mint 20.2 (with NumLock off), when I hit the NumPad center key (5), a line is inserted above the current line, a capital E appears, and I am left in Insert mode. I often mistakenly hit the 5 key when trying to navigate using the NumPad cursor keys, and I would love to redefine the NumPad 5 to do nothing. I've tried remapping the key in .vimrc (using noremap <Esc>[E <Esc>, for example), but it has no effect. Note, the ANSI sequence produced when I press the NumPad5 is \033[E
FYI, I have TERM=xterm-256color.
Note: this answer generally talks about the numpad in a no-numlock context. All numbers on the numpad can be mapped when numlock is on by using <k0>...<k9>. References to the mapability of keys is therefore in a no-numlock context
Note, the ANSI sequence produced when I press the NumPad5 is \033[E
Vim doesn't see it that way.
On my system (Mint 20.2 with Cinnamon under X11), no-numlock 5 produces ^[OE. That O is very important to the observed behavior. o means "go to a new line under and enter insert mode", and O means the exact same thing, but to a new line above. The next E is then interpreted as an insert mode character, because Vim.
Moreover, esc is represented as ^[, so for mapping purposes, you only need <esc>. This point is easier to demonstrate in Vim:
^[ is additionally treated as a single character. If you want to play around with this, pop into insert mode and press <C-v>. The next key(bind) you press will be printed literally to the screen
This means what you actually want is:
nnoremap <Esc>OE <nop>
Note that I'm not sure how terminal input processing applies here; this may only hold for certain terminals, or even only apply to X11.
Another trick in general here, and a way to make sure the keycode is correct for your terminal, is that you can type :nnoremap , and then press Ctrl+v+Numpad 5. This will give you the exact keycode as Vim sees it, which you then can map to whatever you have planned.
If you do this in gVim, however, you'll find that numpad 5 without numlock doesn't actually produce a keycode detectable by gVim. This presumably means it can't be mapped (unless there's some way I just don't know about - if there is, please prove me wrong). But because it doesn't spew out a terminal code, there's the added benefit of not needing to remap it for this particular purpose, because it doesn't have the problem you're trying to avoid here in the first place.
romainl mentioned :help key-notation in the comments, but there's something to keep in mind here:
gVim not producing a keycode usually means there isn't one. <C-v> followed by numpad 9 in gVim produces <kPageUp>, but followed by numpad 5, it doesn't produce anything. Secondly, if you read :h key-notation, you'll see that it only defines home, end, up, and down, as well as the various operators, enter, and the decimal point. 2, 4, 6, and 8 are all registered as normal arrow keys, meaning <Down>, <Left>, <Right>, and <Up>. There's no way to detect these on the numpad specifically, but that's not really important.
If you've been keeping track, you'll find that numpad 5 is excluded. Note: When numlock is on, <C-v> + numpad 5 doesn't output anything in Vim or gVim, because the definition is:
Insert the next non-digit literally.
See :h i_CTRL-V.
TL;DR:
<C-v> is your friend.
Use:
nnoremap <Esc>OE <nop>
Or use <C-v> to insert the keycode, that may or may not be terminal dependent.
The mapping isn't necessary in gVim, and may not be possible.
The reason why you see this behavior is that Vim uses only the termcap interface of the terminal database. If it used terminfo, it would see a wider range of information.
Typing the key on the keyboard is misleading, because Vim (like almost all terminal applications) puts the keypad into application mode, which causes the terminal to send Escape and O rather than Escape and [ as a prefix. In terminal notation, that is \EO. In XTerm Control Sequences, that is referred to as SS3 (technically a misnomer since that applies to output, while keyboard input is input — but since no standards apply to keyboard input, that's good enough).
The terminal description for xterm at one point assigned \EOE to the kbeg capability (i.e., terminfo, which termcap names #1). That was a while back, around 1996.
Terminfo names are generally 3-5 characters, while termcap names are always 2.
Standard terminfo has a limited number of special keys (such as those for the numeric keypad which you are familiar with). All of the standard termcap names correspond to these terminfo names. In the standard arrangement, there are only five keys associated with a keypad: ka1, ka3, kb2, kc1, and kc3, which correspond to termcap's K1, K2, K3, K4, and K5. (Why there are no standard k2, kb1, kb3, or kc2 is long forgotten).
Looking for ways to improve the terminal descriptions in ncurses, a few different layouts of the standard keys have been proposed. But the best solution lies in adding new keys, using extended capabilities (a feature of ncurses since 1999). Those extensions all have names, and are chosen to conform to terminfo naming conventions. So... in May 2019, the terminfo building blocks xterm+keypad and vt220+keypad were introduced. In the former (used in xterm-new and xterm-256color, that key is named kp5.
There's no kp5 in the standard names listed in the terminfo(5) manpage. It is an extended (user-definable) key. In the older scheme some other key was unavailable for applications.
An ncurses application would automatically be able to use all of the keys, because ncurses uses terminfo (and adds all of the keys in a terminal description to its special cases for timing).
Vim will not see that key's definition because it views only a fraction (about a third) of the terminal description. Since it does not see the definition, it does not add that to the special cases where it will wait (a short time) for the completion of the character-sequence sent on that key. As noted in the other answer, in that case, Vim will see the commands Escape and O to insert the third character in the sequence, "E".

How to display remapped key instead of original key in operator-pending mode in vim-airline?

I have discovered Vim a year ago and I am getting more and more addicted to it.
I remapped every key of my french dvorak-like bépo layout to stick to the qwerty layout for the normal mode, as described in "Reconfiguration totale des touches" on this page: https://bepo.fr/wiki/Vim, essentially because I learnt with Vim Adventures game.
It works very well:
For instance, yr remaps as cl : the command cuts the character to the right and enter insertion mode.
However, the vim status line displays y for one second before displaying c:
I changed my mapping from:
noremap y c
to this :
map <nowait> y c
without success.
Anyone knows if it is possible to display a remapped operator-pending key immediately, i.e. "c" in my case ?
This has nothing to do with vim-airline or any other plugin.
The pending operator is shown due to showcmd standard option. AFAIK, there's no way to change this, except to disable it completely (set noshowcmd), or to patch Vim's source code.
Instead of remapping every key combination, a simpler solution is to use langmap. This provides the additionnal benefit to show the remapped command key.
From :h langmap
This option allows switching your keyboard into a special language
mode. When you are typing text in Insert mode the characters are
inserted directly. When in Normal mode the 'langmap' option takes
care of translating these special characters to the original meaning
of the key. This means you don't have to change the keyboard mode to
be able to execute Normal mode commands.
To test it:
:set langmap=éw remaps é to w in normal mode.
:set showcmd displays w instead of é in the status line.

How to map reparted sequence in VIM?

I want to map 'space+u' as 'Ctrl+u', I have this at my config:
nnoremap <space>u <C-u>
But it isn't work as Ctrl+u. When I press 'space+u', firstly it works fine, but when I press 'u' second time - it works an 'undo' (I'm holding 'space' since first time). I want to make it works as , when I press 'Ctrl', hold it and can hit 'u' any times I want.
What can I do?
The mapping you made means "space first, then u in succession". Not simultaneous action there, which explains the behavior for subsequent calls.
Vim listens to key press events; the system reports it that this-and-that key was typed, with that-and-that modifier (ctrl, super). Few applications would understand to check arbitrary combinations of keys being held (outside of games).
For this sort of hacks there is an established project for Linux systems called https://github.com/alols/xcape. In combination of xcape and xmodmap you can basically map <space> as a modifier key like less used Hyper key (this is the part for xmodmap). Then you would use xcape to make sure that pressing space in isolation would result in a space character. People have used xcape to map return as a right Ctrl key (while Caps Lock would act as the left Ctrl).

How to rebind Ctrl-[ in .vimrc

In vim, Ctrl+[ acts like Esc by default. However, I would like to rebind Ctrl+[ to perform a custom action. (Specifically, to Ctrl-t because I find it more natural for [ and ] to have complementary actions for ctags.)
However, when I execute the command:
:nnoremap <C-[> <C-t>
then my arrow key navigation get messed up. I'm not sure what happens, but clearly that's not the ideal way to do it.
I've tried to unbind Ctrl+[ but vim reports that it wasn't bound, and I've tried some tricks like first binding Ctrl+[ to itself and then unbinding. Always the same result.
Side note: Interestingly, when I add it to my .vimrc (as the last command) it's even worse. Something nondeterministic happens and vim opens randomly in one of these 3 states:
The bottom status line says "E73: tag stack empty", implying it received a Ctrl+t-esque command, however the if I hit a nav key like j, it deletes the current and bottom line and then puts me into insert mode. (Happens ~70% of the time.)
Sometimes it's in replace mode. (Happens ~15% of the time.)
Sometimes it's in normal mode. (Happens ~15% of the time.)
How can I properly remap Ctrl+[ to a different function in .vimrc?
You can't rebind Ctrl[. Pressing the Escape key in a terminal sends Ctrl[, just like pressing the Tab key sends CtrlI. There's no separate Esc code.
Vim is probably acting crazily when you try this because basically every ANSI key sequence starts with Ctrl[, so your rebinding is firing on all terminal input, and whatever else is in the key sequence looks like more commands to vim.
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today, even in GVIM. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

Return to normal mode without leaving the home row

I've been trying out vim, and the emphasis on speed and accessibility makes sense for the veteran programmer. I've previously used emacs, and currently use a combination of nano, and stuff like gedit or geany.
I'm confused by the need to constantly switch modes, and that returning from insert mode to normal mode requires leaving the home row to press Esc. I've read that previously, this key was Tab on some systems where vim was first used, which makes a lot more sense, but not on current systems where tab is expected to participate in smart indenting when coding.
Returning to normal mode is an operation that you need to perform all the time. Is there some alternative key mapping that makes this quicker, or mappings that I simply don't know about that do this?
Ctrl+C also return you to normal mode
You can also use ^[. If you've mapped your caps lock key to control (highly recommended), this becomes a rather easy keystroke.
I personally use the Capslock key as the second Esc key, so it is very comfortable. You can do it by adding the following lines to your .xmodmap file:
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
You can map whatever you like to escape:
imap kj <Esc>
I haven't yet run across a situation where I need to type kj next to each other, besides when I talk about this mapping. I've seen other people use jk or jj, but kj works best for me.
I personally use ii to get out of insert mode while staying on the home row, but it is only a matter of personal preference:
inoremap ii <Esc>
i to go in insert and ii to go out. This is easy for your finger to remember. (I tend to still use <Esc> though)
I personally use Left Control as escape and Caps Lock as Left Control. It is not vim mapping, but some xmodmap+setxkbmap magic:
! ~/.Xmodmap
! Control_R
keycode 37 = Escape
and ctrl:nocaps in X keyboard options. You may have different keycode though.
Use Alt/Meta In a Terminal
If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself.
The terminals konsole and gnome terminal send the escape by default when you press alt/meta+normal_mode_key. For Xterm you can ctrl+click and select the option "Meta sends escape" or "Alt sends escape".
Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction. You can hold down alt when moving even while in normal mode since the additional ESC that is sent does no harm.
The advantage of using this scheme is that you can use the alt/meta+key combination with any normal mode key, e.g.
Alt+o opens a new line below the one you are currently editing,
Alt+A appends to the end of the current line,
Alt+p pastes at the current insert location.
Alt+k moves up
Thus using vim via a terminal gives you these short cut powers on any stock standard system without the need to edit each systems vim mappings.

Resources