What is the simplest way to swap the functionality of the Esc key and the ` key?
System ➜ Preferences ➜Keyboard ➜ Layouts ➜ Options let me swap my Ctrl key and Capslock key, which was good, but it doesn't seem to let me do other keys in a generalized way.

The xmodmap answer that got accepted here was useful. Now my ` key is on the physical Esc key, and vice versa. The problem is that when I Shift+Esc I still only get a ` when I'd like to get a tilde.

  • 1
    FYI: Escape the backtick and other special characters like this: \`
    – djeikyb
    Apr 4, 2011 at 9:07

3 Answers 3


You want to follow the five-point list of instructions on the ubuntuforums by Andy Meier: http://ubuntuforums.org/showthread.php?p=10286878#post10286878

The keys you'd have to modify in your /usr/share/X11/xkb/symbols/mynewlayout would be:

key <ESC> {[ quoteleft, asciitilde ] };

key <TLDE> {[ escape ] };

You then have to register this new layout as described on the forum. Good luck.

  • if you s/escape/Escape then this is all good.
    – oadams
    May 10, 2012 at 5:20
  • Yep, needs s/escape/Escape for me on Fedora 23. Can the answer be edited?
    – markwatson
    Nov 23, 2015 at 18:20

Xmodmap replaces keycodes. shift+esc is not a different key. Check the output of xev when you press a and shift+a. You'll find the state changes from 0x0 to 0x1 while the keycode stays the same. What you want to do is change the definition of a key, not just tack on the A/a or ~/` keys. Redefining the output of key's state change is outside the scope of xmodmap.

I don't know exactly how to do what you want, but I think creating a keyboard definition is the right way. I'll edit with more help if or when I figure out how to do this. Maybe this will set you on the right path.


I read elsewhere that Ubuntu is no longer using xmodmap. It's using xkb instead. There's some info on that in this post:

Permanent xmodmap in Ubuntu 13.04

However, I'm still using 12.04LTS, and I wanted to swap my Esc and grave keys. I did it with xmodmap and it worked.

I created a small script that runs xmodmap and I have that script run when I login. The xmodmap commands are:

xmodmap -e "keycode 49 = Escape"
xmodmap -e "keycode 9 = grave asciitilde"

I don't know if this solution will work in versions of Ubuntu newer than 12.04.

  • This still works as of 2023. However, it's not super reliable as it gets lost randomly, e.g. when the keyboard gets reattached, or when the computer goes to sleep. Then you need to re-run it. Jan 24 at 10:21

Your Answer

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

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