Remap Win, Ctrl and Alt keys on Linux - linux

The question:
How can I remap (swap) modifier keys system-wide on Linux?
Explanation:
I want to install Debian Jessie on my PC to use it with Emacs. I have a MacBook with Emacs and use it primarily. Here is how keys are arranged on laptop:
fn ctrl alt cmd spacebar cmd alt
fn is unused in my Emacs workflow, other keys configured as listed below:
ctrl acts as Hyper
alt acts as Meta
cmd acts as Control
These configuration eases for me Emacs experience (big thumb hits Control cmd key) and I got used to this kind of key bindings.
by default alt is used as Meta, while ctrl is used as Control, the keys are swapped and big thumb lays on Meta
I want to configure my keys on Linux same way, here is what I have on my PC's keyboard:
ctrl win alt spacebar alt win menu ctrl
So, to correspond to my laptop keys, I want win keys to be mapped to Alt modifier, and alt keys to be mapped to Control modifier. In this case ctrl could be used as Super modifier, while menu could be kept untouched.
Is it possible to make such configuration system-wide on Linux (Debian)?
Finally, I have managed to achieve my goal with following configuration:
!file ~/.Xmodmap
clear control
clear mod1
clear mod4
keycode 37 = Super_L
! left Ctrl becomes Super
keycode 64 = Control_L
! left Alt becomes Control
keycode 133 = Alt_L Meta_L
! left Win becomes Alt
keycode 108 = Control_R
! right Alt becomes Control
keycode 134 = Alt_R Meta_R
! right Win becomes Alt
keycode 105 = Super_R
! right Ctrl becomes Super
add control = Control_L Control_R
add mod1 = Alt_L Meta_L
add mod4 = Super_L Super_R
Also, here is similar question with quite helpful and simple guides.

Additionally to and possibly easier than using xmodmap might be to check in the keyboard layout options given by setxkbmap. These options implement common xmodmap changes, so they might well contain what you want.
For example, I like -option ctrl:nocaps which adds me another control where the caps lock was. Consider the file /usr/share/X11/xkb/rules/base.lst for a list of the standard options.
However, neither xmodmap nor setxkbmap include keyboard settings for the text-only tty terminals (non-X).

xmodmap is the place to start, keeping in mind that X knows nothing about Hyper or Meta, so you have to investigate to see which numbered modifier (mod4, mod5, etc), emacs uses for these modifiers.
Here are a few useful links:
XModMap
How to separate alt and meta keys in emacs
MetaKeyProblems
Emacs does not use swapped alt&win after xmodmap runs? (GUI emacs)

Full Disclosure
I am the author of Kinto.
You may find Kinto to be useful. It's a project I recently created, after a few years of tackling this problem a few different ways and I just now feel like it's solid enough to share.
The Kinto installer simply uses python to do the initial install, after that it's just a systemd service, bash, xprop, setxkbmap and xkbcomp. As far as I am concerned it is the fastest and most native solution to this problem that you are going to find.
Also Xmodmap is defunct, it will not work going forward with Wayland.
https://github.com/rbreaves/kinto
https://medium.com/#benreaves/kinto-a-mac-inspired-keyboard-mapping-for-linux-58f731817c0
Here's a Gist as well, if you just want to see what is at the heart of it all, it will not alternate your keymap when needed though. The Gist also does not include custom xkb keymap files that setup macOS style cursors/word-wise manipulations that use Cmd and the arrow keys.
https://gist.github.com/rbreaves/f4cf8a991eaeea893999964f5e83eebb
gist content
# permanent apple keyboard keyswap
echo "options hid_apple swap_opt_cmd=1" | sudo tee -a /etc/modprobe.d/hid_apple.conf
update-initramfs -u -k all
# Temporary & instant apple keyboard keyswap
echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd
# Windows and Mac keyboards - GUI (Physical Alt is Ctrl, Physical Super is Alt, Physical Ctrl is Super)
setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win
# Windows and Mac keyboards - Terminal Apps (Physical Alt is Super, Physical Super is Alt, Physical Ctrl is Ctrl)
setxkbmap -option;setxkbmap -option altwin:swap_alt_win
#
# If you want a systemd service and bash script to help toggle between
# GUI and Terminal applications then look at project Kinto.
# https://github.com/rbreaves/kinto
#
# Note: The above may not work for Chromebooks running Linux, please look
# at project Kinto for that.
#
# If anyone would like to contribute to the project then please do!
#

xmodmap/setxkbmap methods are great until you have 2nd keyboard that doesn't require any remapping. For example laptop and external keyboard.
I found swapping via udev hwdb the best because it changes only specific keyboard. For example in case of Framework laptop and Pop!OS (also should work on most Debian derivatives) to swap alt and win keys it is required to create a file /etc/udev/hwdb.d/99-fw-meta-alt.hwdb:
evdev:input:b0011v0001p0001*
KEYBOARD_KEY_38=leftmeta
KEYBOARD_KEY_db=leftalt
and then run
sudo systemd-hwdb update
sudo udevadm trigger
This makes alt-win swap only on internal laptop's keyboard only without remapping all other keyboards I connect to my laptop.

if you're using KDE you can assign the command key to Alt using the following method, but it will render the option key useless.
meaning you'll end up with two alts in command and option keys, but still better than the default
you can go to settings > keyboard > advanced tab > alt/win key behavior > check Alt is mapped to Win and the usual Alt > OK

Related

How to swap Ctrl and Meta (Alt) keys under FreeDOS OS

I'm looking for a solution to swap the Ctrl and Alt keys under FreeDOS OS. Does anybody know anything about this? Maybe there is a program similar to 'loadmap' that is used in GNU OS.
I have not tried that, but the command to use to make your own keyboard mappings file is KC and the command to load a keyboard mapping (typically called from AUTOEXEC.BAT) is KEYB.
If it is only for a limited number of shortcuts you want to modify you can also have a look at XKEYB and KEYMAN that together will allow you to make your own keyboard macros that can include the use of CTRL and ALT keys.

How to type Numpad Enter, if there's no such key on the keyboard?

I'm working with an ancient platform emulator, so sometimes I need to press Numpad Enter, but my notebook keyboard has no options for doing that. Is is possible to emulate Numpad Enter key somehow? I don't mind if the solution is clumsy.
You can remap the keyboard using xmodmap. For example, to turn F12 into numpad enter, use this command:
xmodmap -e 'keycode 96 = KP_Enter'
You can find out the keycode of keys by starting xev and pressing them. The list of possible keysym values can be found in keysymdef.h. Omit the XK_ prefix. The numpad keys start with XK_KP_.
I would think that your laptop has 'fn' key and little blue numbers on keys; 7,8,9,u,i,o,j,k,l and m. these are your numeric 'key pad', But if it doesn't...
In KDE > YaST2 > there are several on screen keyboards typically used for 'other' languages and Accessibility situation. Kvkbd seems to be installed on my openSuSE / KDE systems by 'default' as I did not specifically install it. On the right edge of Kvkbd, there is a << arrow looking thing that will expand it and show the numeric keypad.
screenshot: of kvkbd:
Kicker > (in search, type) keyboard and you should see a list generate of all the keyboard related apps you have installed. If not, back to above, YaST2 and install one.

How to remap keyboard keys to avoid RSI in VIM

I hope this does not come across as an attempt to spark conversation purely for the sake of doing so.
I use vim a great deal (5-10 hours a day) and I noticed my left wrist is the first to start aching. The standard keyboard layout (see image below) is almost guaranteed to give you arthritis.
Currently, I have remapped
Ctrl to Shift
Shift to Caps Lock
Caps Lock to Ctrl
This helps with common commands such as Ctrlf or Shift2 (#), but I still have to twist my wrist to get at those combinations. Is there anyway to remap the keys so as to avoid this twisting on the left wrist (maybe use the Tab key)? I just find it odd that for a text editor designed specifically for programmers, it would make heavy use of these out of the way keys. I have considered remapping to the old keyboard layout (see below image), but I don't want to learn that if it is going to have the same fundamental problems as my current remapping.
Note: I am not a doctor, just another typist that suffered from wrist pain.
In my experience, just mapping Caps to Ctrl (or swapping the two if you must have a Caps) and using Ctrl+[ instead of Esc make a big difference. Ctrl+[ does not require an additional mapping in vim as it is equivalent to Esc. If you can teach yourself this small change you reduce your movement by a great deal when you consider how often you use Esc in vim.
The Caps to Ctrl mapping can't be done inside vim and will have to be made system-wide, however, having Ctrl in the more convenient location will also help in other applications that use Ctrl combinations. There are different methods of remapping keys depending on which OS you're using. A few common options are:
xmodmap (GNU/Linux)
KeyRemap4Macbook (OSX)
KeyTweak (Windows)
While this will probably improve your situation you really should consider seeing a doctor and/or purchasing a more ergonomically designed keyboard if your pain persists. I own an older version of the Kinesis Advantage that I picked up used on Ebay for about $100 (USD) and I couldn't be happier. The keys can be programmed directly on the keyboard so no software is needed to swap keys or create your own keyboard macros for frequently used key strokes.
Edit: I see in your post you also mention combinations like Ctrl+f and Shift+2 requiring you to twist your wrist to press. It sounds to me like you're only using the Ctrl and Shift keys on the left side of your keyboard. You should really be using both sets of these keys, using whichever one is on the opposite hand of the key you're modifying. That would negate this wrist movement.
Use caps as ctrl systemwide. (How often do you cut/copy/paste?)
This also makes ctrl[ work in vim.
Alongside I use the vim 'arpeggio' plugin.
Arpeggio lets you define vim bindings that are activated when two keys are simultanously pressed.
jk pressed serves me as ESC.
Also the 'surround.vim' plugin might be interesting for you.
Eliminated all my wrist pains.
post scriptum:
Use 'vundle'!
Its the best vim plugin manager by far, and you will wonder how you could ever do without, especially if you use a customized vim on several machines. Don't be scared if you have trouble setting it up initially, take your time.
I switched to a Kinesis Advantage keyboard, and remapped Caps Lock to Esc. This effectively removed any wrist pain I felt as a vim user.Once again not a doctor, but looking at how my hands move, I don't see my wrist twisting even when I hit Shift.
You could probably do some great remappings to the thumb keys to further minimise tension, but to be honest I've never felt the need.
Not a $0 solution, I'm afraid, but it was very effective for me.
I mapped both Escape (single tap) and Ctrl (hold) to CapsLock:
xmodmap -e "remove Lock = Caps_Lock"
xmodmap -e "remove Control = Control_L"
xmodmap -e "keysym Control_L = Caps_Lock"
xmodmap -e "keysym Caps_Lock = Control_L"
xmodmap -e "add Control = Control_L"
xcape -t 1000 -e "Control_L=Escape" # Fire nothing if 1000ms hold time out
Probably I'm messing things up here, but xmodmap isn't the best option to modify mappings anyway, so I will change that to pure xkb config.
I have also swapped : with ; and ` with ~, since I use : and ~ more often:
xmodmap -e "keycode 47 = colon semicolon"
xmodmap -e "keycode 49 = asciitilde grave"
I also use Compose Key to be able to type ūmlauts et c.
In vim, the most useful change I have ever made is mapping of space to leader. All kinds of leader-key combinations will make your life easier.
I couldn't get used to H and L for _ and $, since it doesn't seem to work with surround.vim and some other tricks people advise.
Just don't use arrows!
I'd make this part a comment to Randy's answer on Esc if I could, but it demands a little more space. There's one important use case where you can't just remap Windows keys: Windows 7 without admin rights. It's common enough in an enterprise environment; at the very least, it describes me and that keyboard in the pic. keymapper's repo has decent info on this issue:
Important note for Windows 7 Users
Windows 7 does not support per-user key mappings. Until the next release of Key Mapper, you will have to manually switch to 'Boot' mappings from the Mappings menu, reboot to change the mappings, and you will need to have Administrative access to your computer to set key mappings that will work in Windows 7.
The same is true for any Microsoft tools, KeyTweak, SharpKey, etc.
So you'll have to use something like AutoHotkey. I personally prefer mapping Caps Lock directly to Esc for Vim, and I've had issues just mapping it to Ctrl:
#IfWinActive ahk_class Vim
*CapsLock::Esc
#IfWinActive
Another alternative I like is inoremap jj <Esc>. Unless you plan to write about a hajj or something it works pretty well.
More to your point
In any case, have you considered doing the above for easy escaping and then remapping some of the hard-to-reach insert mode commands to Alt (hit it with your thumb) and then using mappings to your leader?
let mapleader=","
nnoremap <leader>f <C-F>
I would suggest remapping
Caps lock to left Control
Left Control to Escape
Escape to Caps lock
. If in linux first can be done by using setxkbmap -option '...,ctrl:nocaps' or changing XkbOptions in /etc/X11/xorg.conf. Second requires using of xmodmap. After some X server update I failed to get the third working, previously used xmodmap as well.
Using dvorak layout (maybe even programming) that is optimized for touch typing and mappings like ,s->()<Left> might also help.
Note: I am not a doctor as well. I am touch typist, but unlike #Randy Morris I never suffered from the wrist pain, just moved keys to the most convenient locations.
I always press the left ctrl key by curling up my pinky and pressing with the first knuckle. I find this requires less hand movement.
Update
I don't find Shift uncomfortable, so I can't help you with
that. However Ctrl does bother me, and even with my
knuckle technique I prefer to avoid it where possible. So for
insert mode (and command-line mode) I use a leading ;
instead of Ctrl. E.g.
:map! ;w <C-W>
:map! ;a <C-A>
:map! ;r <C-R>
:ino ;t <C-T>
:ino ;d <C-D>
You get the idea.
In normal mode I use v and V instead of Ctrl+F and
Ctrl+B (I only use visual blockwise mode), though
I imagine most people would prefer to map different keys,
such as <Space> and - for paging (- isn't really
needed). Instead of F you could search forwards with f,
and then hit ,.
I've never really seen much point in Ctrl+D or Ctrl+U.
However I do find z., z<Enter>, and z- useful for
scrolling the line with the cursor to the middle, top, and
bottom of the window respectively. H, M and L are also
useful for moving the cursor around the screen. But in
general I prefer to just use / with 'incsearch' set for
navigating about.
There's no right or wrong way to do this, these are just my personal preferences, but I hope it gives you one or
two ideas. Try experimenting and see what works for you.
I've developed a vim module to do some simple remappings for exactly this issue:
https://github.com/vim-scripts/vim.ergonomic
The biggest things it does is make it easier to type bracket sets. It also makes it much easier to get into command mode (jj->ESC)
It runs on the principle that you hardly ever used the J key while editing, but it is right there in the home row... so let's abuse it!
from the documentation:
Remapping commonly used commands
key combo mapping purpose
; : removes the need to hit Shift to use commands
jj <ESC> can exit edit mode quickly and easily
Jj j can type 'j' when you need to
JJ J can type 'J' when you need to
Making it easier to insert various kinds of braces:
key combo mapping purpose
JU ()<left> easier to create parenthesis
JI []<left> easier to create square brackets
JM {}<left> eaiser to create curly braces
J< <><left> easier to create carrot braces
Make it easier to move small distances (especially useful with braces commands):
key combo mapping purpose
JL <left> move left in insert mode
JH <right> move right in insert mode
Removing any need for enter, backspace or delete:
key combo mapping purpose
JB <esc>ldbi delete the previous word
JW <esc>dwi delete the next word
JO <esc>oi insert line and go to it

How to map Menu key ("Application key") to Escape key in vim?

I think that using Menu key to quit vim's insert mode would be a great thing. It would be also nice to use Super key for that, but I'm not sure if it possible since Super key is a modifier.
Anyway, I couldn't find anything related to this. Looking for your help and thanks in advance!
I don't think there's any way you can configure Vim to pay attention to the Menu key as such,
but depending on your system there are various ways to turn the Menu key into an Escape key.
If you're using X11 on Linux:
The command xmodmap -e 'keycode 135 = Escape' will turn your Menu key into an Escape key for the current session, but is not permanent. To make it permanent under Gnome, you might try adding it under System → Preferences → Startup Applications.
The xkeycaps program will give you a GUI for similar remappings.
For more information:
http://46dogs.blogspot.com/2008/05/remap-keys-in-ubuntu-804-hardy-heron.html
http://ubuntuforums.org/archive/index.php/t-106209.html
I haven't found a way to map it in gVim yet, but I was able to successfully map the Menu key in a urxvt+screen+vim stack by the following method:
In a terminal, type Ctrl+v and press Menu. This is mapped to the quoted-insert function in Zsh and in Readline-based tools like Bash.
It will generate an escape sequence like ^[[29~ at the prompt. (The initial ^[ must be translated to <Esc> for use in .vimrc.)
Open up .vimrc and add a line like this:
imap <Esc>[29~ <Esc>
(or imap <Esc>[29~ <Esc><Esc> if you don't want it to wait for further input like the Escape key does.)
Note that not all keys return something usable from Ctrl+v. This is a limitation of terminal emulators and can be remedied by remapping the key. You may be able to do that at the level of the terminal emulator rather than for all X apps.
For example, for urxvt, I had to add the following lines to ~/.Xresources and run xrdb -merge ~/.Xresources to apply them:
! Unbreak zsh keys in URxvt
URxvt*keysym.Home: \033[1~
URxvt*keysym.End: \033[4~
(\033 is <Esc> in ~/.Xresources syntax.)
Just try using ctrl+[ instead of binding another key. This combination is a standard one in vim, btw.
This is even easier when having rebound capslock into an additional ctrl.

Linux: Mapping Windows key to M-x for the purpose of emacs usage

I am an emacs user (on linux laptop) looking to make better use of my keyboard settings. The windows key is unused on my keyboard... is there any way to map it to m-x? This might make many emacs commands faster.
Thanks,
SetJmp
Use xmodmap to make it the Menu key, as in
keycode 115 = Menu
You will have to use xev to find out if the Windows key is key 115 on your keyboard.
Just to provide a little more info, here's a link to a quick series of steps to remap the CapsLock Key in both X and Virtual Consoles:
The steps can be identically followed for Windows key or whatever else you may wish to remap.

Resources