Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2024 :: Remapping Keys with xmodmap

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

<< First, < Previous, , Latest >>

Thumbnail of Jon Kulp
Hosted by Jon Kulp on Thursday, 2016-05-05 is flagged as Explicit and is released under a CC-BY-SA license.
tips and tricks, CLI, bash, linux, accessibility. 2.
The show is available on the Internet Archive at: https://archive.org/details/hpr2024

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:08:51

general.

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"

Links

  • 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

https://www.youtube.com/embed/hKEax8IqxAU


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2016-05-06 04:40:55 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-30 16:29:10 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

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?