Site Map - skip to main content - dyslexic font - mobile - text - print

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.

hpr2024 :: Remapping Keys with xmodmap

I describe how I use xmodmap to remap my spacebar to make underscores

<< First, < Previous, Latest >>

Host Image
Hosted by Jon Kulp on 2016-05-05 is flagged as Explicit and is released under a CC-BY-SA license.
Listen in ogg, spx, or mp3 format. | Comments (2)

In this episode I talk about how I tried to implement an idea that my son had when we were talking one day. I was complaining about file names with spaces in them, and he asked what if the computer automatically changed the spacebar so that it made underscores whenever somebody was trying to save a file? I thought this was a great idea. I even thought of a way implement it, though not quite as magically as he had envisioned. My solution involves the use of the command-line tools xev and xmodmap, and one blather voice prompt to launch the xmodmap command that will remap the spacebar to make underscores instead. Maybe somebody a whole lot smarter than me can figure out how to make this happen automatically whenever a save dialog box is open.

First you need to find the keycode for your spacebar. Run the xev command and then press the spacebar to see which key code it is. Here's the output on my laptop:

KeyPress event, serial 48, synthetic NO, window 0x4e00001,
    root 0xc0, subw 0x0, time 116149126, (-739,-226), root:(448,358),
    state 0x0, keycode 65 (keysym 0x20, space), same_screen YES,
    XLookupString gives 1 bytes: (20) " "
    XmbLookupString gives 1 bytes: (20) " "
    XFilterEvent returns: False

As you can see, my spacebar has the keycode of "65." Now we use xmodmap to reassign keycode 65 to make underscores:

xmodmap -e "keycode 65 = underscore"

Now to test it out. While xev is running, press spacebar. Notice that now when the spacebar is pressed it makes an underscore:

KeyPress event, serial 57, synthetic NO, window 0x2600001,
    root 0xc0, subw 0x0, time 116190619, (-520,-247), root:(667,337),
    state 0x0, keycode 65 (keysym 0x5f, underscore), same_screen YES,
    XLookupString gives 1 bytes: (5f) "_"
    XmbLookupString gives 1 bytes: (5f) "_"
    XFilterEvent returns: False

And to change it back:

xmodmap -e "keycode 65 = space"

Now whenever I want to change the spacebar to make underscores or switch it back, I speak one of the following commands, which are in my blather configuration file.

MAKE UNDERSCORES: xmodmap -e "keycode 65 = underscore"
MAKE SPACES: xmodmap -e "keycode 65 = space"


  • xmodmap man page: xmodmap is a utility for modifying keymaps and pointer button mappings in X
  • xev man page: use xev print contents of X events

Video Demonstration


Subscribe to the comments RSS feed.

Comment #1 posted on 2016-05-06T04:40:55Z by b-yeezi

Interesting approach

This is an interesting approach to prevent creating new files from bad names. You should also consider the command line tool called detox. This tool is especially useful when you are dealing with entire directories of existing files with bad file names.

Comment #2 posted on 2016-05-30T16:29:10Z by Dave Morriss

Nice idea

I have been bitten by this over the years of using Unix and Linux and this is quite an original solution.

Personally, I have got into the habit of either using Tab while typing an existing file name so that the shell formats it for me by adding backslashes before spaces and the like, or by enclosing such names in quotes. However, the strategy of avoiding creating such file names is a good one.

Leave Comment

Powered by Comment Script