Bash – xmodmap key bindings reset when I open a new tab in the shell

bashbashrcrangervimxmodmap

When I open a shell in ranger with shift-s, open vim and then open a shell with :sh or open a new shell tab with ctrl-shift-t, my xmodmap keybindings (swapped caps lock and escape keys) are reset. This is what the terminal spits out:

xmodmap:  please release the following keys within 2 seconds:
    t (keysym 0x74, keycode 28)
    Shift_R (keysym 0xffe2, keycode 62)

My keybindings are very important to me as I am an active vim user currently working with Typescript (I need quick access to the shell to compile my code). I set my keybindings with xmodmap ~/.Xmodmap (the .Xmodmap file contains my xmodmap commands) and I added that command to my .bashrc.

For some more context I am using Ubuntu 16.4 and I recently created a ranger configuration file (~/.config/ranger/rifle.conf) to add support for the .ts file extension. I not an expert ranger or bash user, but both are very important for my workflow. Help would be greatly appreciated.

edit: this problem is solved when I remove xmodmap ~/.Xmodmap from my .bashrc, but now I have no way of having my xmodmap keybindings enabled on startup. Adding @reboot xmodmap ~/.Xmodmap to my crontab file does not work (maybe I should post a separate question for that, but I'll leave this question here for future reference).

Best Answer

Don't run xmodmap in .bashrc.

It belongs in ~/.xinitrc so your window manager will run it once upon startup. The same keybindings will continue to be available when you open your 2nd or 3rd shell tab.

Your .bashrc commands run in several contexts, including an ssh login session that may lack a $DISPLAY, so that init script is usually not the right place for X11 commands.

Related Question