Disabling the Ctrl-W shortcut on Firefox

2022-03-29

Many vim users use ctrl-w to delete a word. Unfortunately on Firefox, this closes the current tab. So if you’re editing a text area / filling out a form you may accidentally close the tab ๐Ÿ™„. There’s no option to change or disable default keyboard shortcuts (Firefox 98, 2022-03-29). So if you want to do it, you need to manually edit the file /usr/lib/firefox/browser/omni.ja.

First unzip it to a temporary directory:

mkdir /tmp/firefox-omni
cd /tmp/firefox-omni
unzip /usr/lib/firefox/browser/omni.ja

Now you need to edit chrome/browser/content/browser/browser.xhtml to make your changes. You can change as many shortcuts as you want here. The ctrl-w key is called close-shortcut. You can either comment out the offending line, or change modifiers="accel,shift" to require ctrl-shift-w to close tabs.

I personally use Firenvim (which uses an embedded neovim to edit text areas). In this case all you need to do is changed reserved="false" in the corresponding line. This will allow Firenvim to take over the shortcut ctrl-w when it’s active (in text areas), and you won’t accidentally close tabs when trying to delete words. While I was at it, I also made ctrl-t / ctrl-n un-reserved, so that I can use these shortcuts when editing text areas with Firenvim.

diff -ru firefox-omni-orig/chrome/browser/content/browser/browser.xhtml ./chrome/browser/content/browser/browser.xhtml
--- firefox-omni-orig/chrome/browser/content/browser/browser.xhtml  2010-01-01 00:00:00.000000000 -0500
+++ ./chrome/browser/content/browser/browser.xhtml  2022-04-05 20:26:31.880470112 -0400
@@ -263,9 +263,9 @@
     <key id="key_newNavigator"
          data-l10n-id="window-new-shortcut"
          command="cmd_newNavigator"
-         modifiers="accel" reserved="true"/>
+         modifiers="accel" reserved="false"/>
     <key id="key_newNavigatorTab" data-l10n-id="tab-new-shortcut" modifiers="accel"
-         command="cmd_newNavigatorTabNoEvent" reserved="true"/>
+         command="cmd_newNavigatorTabNoEvent" reserved="false"/>
     <key id="focusURLBar" data-l10n-id="location-open-shortcut" command="Browser:OpenLocation"
          modifiers="accel"/>
     <key id="focusURLBar2" data-l10n-id="location-open-shortcut-alt" command="Browser:OpenLocation"
@@ -284,7 +284,7 @@
     <key id="openFileKb" data-l10n-id="file-open-shortcut" command="Browser:OpenFile"  modifiers="accel"/>
     <key id="key_savePage" data-l10n-id="save-page-shortcut" command="Browser:SavePage" modifiers="accel"/>
     <key id="printKb" data-l10n-id="print-shortcut" command="cmd_print"  modifiers="accel"/>
-    <key id="key_close" data-l10n-id="close-shortcut" command="cmd_close" modifiers="accel" reserved="true"/>
+    <key id="key_close" data-l10n-id="close-shortcut" command="cmd_close" modifiers="accel" reserved="false"/>
     <key id="key_closeWindow" data-l10n-id="close-shortcut" command="cmd_closeWindow" modifiers="accel,shift" reserved="true"/>
     <key id="key_toggleMute" data-l10n-id="mute-toggle-shortcut" command="cmd_toggleMute" modifiers="control"/>
     <key id="key_undo"

If you save the above (or your custom changes) to a patch, then you can apply it by:

patch /tmp/firefox-omni/chrome/browser/content/browser/browser.xhtml firefox-omni.patch

Now switch to root, and install your changes:

cd /tmp/firefox-omni
zip -0DXqr /tmp/omni.ja *
cd
cp /usr/lib/firefox/browser/omni.ja /usr/lib/firefox/browser/omni.ja.orig
cp /tmp/omni.ja /usr/lib/firefox/browser/omni.ja
rm -rf /tmp/omni.ja /tmp/firefox-omni

Now as a normal user, clear your startup cache.

rm -rf ~/.cache/mozilla/firefox/*/startupCache

Restart firefox, and it should work.

Note: You will have to do this every time you reinstall firefox. I automated the above into a script. First save firefox-omni.patch as ~/local/src/firefox-omni.patch, and then download and run this script as a normal users (that has sudo privileges).

๐Ÿ—ซ Comments

  • Anonymous
    Ctrl-W still bothers

    Anonymous (2022-06-17 09:34:22 EDT)

    this does not work, no effect

  • Gautam Iyer
    Re: Ctrl-W still bothers

    Gautam Iyer (2022-06-20 14:34:30 EDT)

    It works (at least for me as of 2022-06-20), as I use it every day.

    Note, the above instructions only make Ctrl-W an unreserved shortcut. This means extensions can now remap it. If you don’t have any extensions that remap it then Ctrl-W will still close your current window.

    If you’d like to simply disable Ctrl-W, then just comment out the above shortcut definitions entirely (instead of only changing reserved="true" to reserved="false" as I suggest above).

    GI

  • Anonymous
    Thanks

    Anonymous (2022-10-05 20:13:15 EDT)

    Thanks for this, works for me.

  • Vinny
    Can't follow instructions on Ubuntu Mate

    Vinny (2022-12-22 13:04:18 EST)

    Firefox 108 assigned Shift + ESC to invoke the Process Manager, but it is also a shortcut for Discord to mark a server as read. Found this page via Google and tried to follow the instructions on my Ubuntu Mate machine, but unfortunately the directory /usr/lib/firefox/browser doesn’t exists. Even using find I can’t find the omni.ja file. Maybe it’s because Ubuntu handle Firefox via snap instead of apt-get? I don’t know, need to research more.

  • Gautam Iyer
    Re: Can't follow instructions on Ubuntu Mate

    Gautam Iyer (2022-12-22 20:38:07 EST)

    Maybe itโ€™s because Ubuntu handle Firefox via snap instead of apt-get? I donโ€™t know, need to research more.

    Yeah; the snap files may not be in the standard location. Try searching the filesystem via find or locate.

๐Ÿ“ฎ Leave a comment (Spammers beware: All comments are moderated)

Sorry. There was an error submitting your comment. Please try again, or contact me if the problem persists.
Sending comment; please wait.
Thanks. Your comment was successfully submitted. It will appear here shortly if it isn't spam.