0

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?

1
  • 1
    Do 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
    Jan 25, 2023 at 14:37

2 Answers 2

1

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
    1. Start the Tweaks application.
    2. Select "Keyboard & Mouse" from the left-hand menu.
    3. Click "Additional Layout Options".
    4. Click "Ctrl position" or "Caps Lock behaviour" on the window that opens and choose yours.
  • with KDE Plasma, that part is available by default.
    1. Start the System Settings app.
    2. Go to "Input Devices" panel.
    3. Click "Keyboard" category.
    4. Click "Advanced" tab.
    5. 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)
    /usr/bin/setxkbmap -option "$flag"
    
    where the $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.
5
  • 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)
    – gildux
    Jan 25, 2023 at 9:23
  • 1
    Done. Sorry for the delay, I was on work.
    – gildux
    Jan 25, 2023 at 11:56
  • I would like to add that I ran into the problem that Debian with GNOME completely ignored any setxkbmap options. It worked fine with gnome-tweak-tool, however.
    – Friedrich
    Jan 25, 2023 at 12:45
  • 1
    Windows users may want to take a look at uncap. A bit offtopic as the question is specifically about Linux.
    – Friedrich
    Jan 25, 2023 at 12:47
  • I think setxkbmap seemed to be ignore because GNOME is having prevalence? Some DE use to have higher priority on everything set outside them. Thanks for uncap I didn't know, and their README lists all known ways (with loadkeys I forgot to mention)
    – gildux
    Jan 25, 2023 at 13:43
0

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:

  1. Go to this directory /etc/X11/xorg.conf.d/
  2. 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)
  3. 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

  1. 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:

1
  • 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 or sudo -e
    – gildux
    Jan 25, 2023 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.