Having to press the esc
key every time that you want to enter normal mode is a pain.
I have seen multiple answers where people map combinations like jj
. But why use that when you can map the capslock
key?
-
1Do you want to change CapsLock to send Esc at the OS/Keyboard level, or at the Vim level? The former is not on-topic here, and the latter has an unfortunately short answer ("it's not possible").– D. Ben Knoble ♦Commented Jan 25, 2023 at 14:37
3 Answers
Edit: my first answer missed the question title, sorry.
why vi-map instead of system keys-swap?
Many people can have access to a Vi-like editor but not to system settings. Think of your university or enterprise account, or any online account in a box that's not your own computer. In those cases you cannot map a key to another because you don't have administrator privileges.
However some systems may let you do it on user's level: I think it's the case with Mac OS X and also GNU/Linux distros if allowed by root.
Some operating systems may be a pain to switch such keys even when you have full admin rights (hi Windows)
Well, when people can swap two special keys of their keyboards, they may choose to no do it ...because they have some other needs than Vi or Vim. For example, with standard keyboards, I also like to have Escape at CapsLock location when using the editor, but I prefer to have Control at the same location for other usage in the shell.
When the need is for a program only and not all stuffs, it's very nice and handy to have the feature inside. Plus, your Vi' setting can easily follow you from an account to another (here we loop with the first part of my answer) ...and it's easier to give a answer that is contained with the program instead of having to deal with all OS related nightmares. (last, we're on vi.SE here and not on: SU/U&L.SE/askubuntu/etc. haha)
how keys-swap on GNU/Linux per user?
They're many answers on how to do it, and the OP found some of them. But it's system wide and not all users want that... Like on Mac OS X, it may be a user preference easily configured via the Desktop Environment interface:
- with GNOME3, you'll need the Tweaks app installed (that's how the administrators allow the users to customise that parts of their desktop with ease, and the package is often named
gnome-tweak-tool
). From that point- Start the Tweaks application.
- Select "Keyboard & Mouse" from the left-hand menu.
- Click "Additional Layout Options".
- Click "Ctrl position" or "Caps Lock behaviour" on the window that opens and choose yours.
- with KDE Plasma, that part is available by default.
- Start the System Settings app.
- Go to "Input Devices" panel.
- Click "Keyboard" category.
- Click "Advanced" tab.
- Click "Ctrl position" or "Caps Lock behavior" then choose yours.
- to found and complete for LXQt and LXDE
- the Xfce desktop environment doesn't have a handy tool for managing these kinds of settings, we'll have to go console by way by running (put it in your
~/.profile
or appropriate)
where the/usr/bin/setxkbmap -option "$flag"
$flag
is one of (not exhaustive)ctrl:nocaps
— ⇪ as ⎈.caps:none
– Disables ⇪.caps:super
– ⇪ becomes an additional ❖ or ⌘ or ⊞.caps:ctrl_modifier
– ⇪ becomes an additional ⎈.caps:numlock
– ⇪ becomes an additional ⇭.caps:escape
– ⇪ becomes an additional ⎋.caps:backspace
– ⇪ becomes an additional ⌫.caps:swapescape
– ⇪ becomes ⎋, and ⎋ becomes ⇪.- note that, in previous descriptions, I try to use ISO/IEC 9995 labels.
- still in console, one may use XModMap (commands like
xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
or use of~/.xmodmap
file) As it's deprecated in favour of SetXKbMap, I won't elaborate anymore.
-
You're right @Friedrich and that's because I missed the "How" in the title first, and was answering the "why use that when you can map" I though was the main concern. I'll edit a bit later to provide alternatives (for Linux according to the title)– gilduxCommented Jan 25, 2023 at 9:23
-
1
-
I would like to add that I ran into the problem that Debian with GNOME completely ignored any
setxkbmap
options. It worked fine withgnome-tweak-tool
, however. Commented Jan 25, 2023 at 12:45 -
1
-
1I think
setxkbmap
seemed to be ignore because GNOME is having prevalence? Some DE use to have higher priority on everything set outside them. Thanks foruncap
I didn't know, and their README lists all known ways (withloadkeys
I forgot to mention)– gilduxCommented Jan 25, 2023 at 13:43
The first solution by @gildux shows you can put :
/usr/bin/setxkbmap -option "caps:escape"
in your ~/.bash_profile
or ~/.profile
My suggestion would be keyd for more customizability or simplicity. Their README shows example for your usecase. You would just need enable to keyd
systemd unit (sudo systemctl enable keyd
) and put the following in /etc/keyd/default.conf
:
[main]
# Maps capslock to escape when pressed and control when held.
capslock = overload(control, esc)
I did find the answer but it was split up between sites and I thought that it would be a good idea to unite them in a question.
Truth is that you can't do this natively in vim, but you need to implement it system-wide. As the capslock
key doesn't mean anything on it's own.
How to do this:
- Go to this directory
/etc/X11/xorg.conf.d/
- There you will find a text file named like something like
90-custom-kbd.conf
(this may vary, in my case it was preceded by a 00 instead of a 90) - Enter the file with vim and write the following
#keybind capslock as esc
Section "InputClass"
Identifier "keyboard defaults"
MatchIsKeyboard "on"
Option "XKbOptions" "caps:escape"
EndSection
6.in order to save it you need to enter vim normal mode and write
:w !sudo tee %
- w writes the buffer,
- !sudo calls the shell with sudo,
- tee redirects the output of vim :w to the output with tee, and
- % is the current filename
Enter the sudo password and press L
- Reboot the computer
if you still wish to use the capslock
key you can by in the line before the EndSection
where it says caps:escape
type caps:escape_shifted_capslock
In order to use it you will need to press the shift
key
Sources:
- Script and location of the file https://wiki.archlinux.org/title/X_keyboard_extension
- Being able to still use the
capslock
key https://www.reddit.com/r/vim/comments/k5twce/switching_esc_with_caps_lock/ - Being able to edit a readonly permission file https://unix.stackexchange.com/questions/71109/how-can-i-exit-from-read-only-mode-in-vim
-
Welcome vi.SE @milintime :) You can avoid the 4th point, I think it's better in the 3rd step to enter the file with
sudoedit
orsudo -e
– gilduxCommented Jan 25, 2023 at 8:17