Disabling the Ctrl-W shortcut on Firefox


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"
-         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"
     <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 *
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).


  • Anonymous

    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.

  • cv
    Quick fix for Ctrl-w

    cv (2023-03-21 14:32:39 EDT)

    Go to System, Preferences, Hardware, Keyboard Shortcuts, assign Ctrl-w to something and it will quit working in Firefox. Beware that it will probably quit working in other apps.

  • notajake
    Remapping a non-reserved key with browser extension is not work anymore?

    notajake (2023-05-05 01:51:04 EDT)

    I test it on Firefox 102.5.0esr. I can confirm that the successful modification (reserved="false") of chrome/browser/content/browser/browser.xhtml. But the remapping is not working (the extension that listen to Ctrl+W is not triggered)

    Here’s the description that i found on MDN (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/commands). Which is the API that used by browser extension to define browser scope shortcuts.

    If a key combination is already used by the browser (like “Ctrl+P”) or by an existing add-on, then you can’t override it. You can define it, but your event handler will not be called when the user presses the key combination.

    The description above didn’t cover the “reserved” option, I’m not dive into the source code yet. So I can’t confirm that this is 100% not working. If anyone can confirm it or figure out how to make the remapping work, please leave a comment.

  • MusicSky

    MusicSky (2023-05-09 00:36:09 EDT)

    Hello! Thank you for writing this blog post. I really need to remap C-w because of my vim habit. However, I get the following error:

    โœ– ./patch-firefox-omni.zsh
    +./patch-firefox-omni.zsh:10> [[ -d /tmp/firefox-omni ]]
    +./patch-firefox-omni.zsh:10> rm -rf /tmp/firefox-omni
    +./patch-firefox-omni.zsh:11> mkdir /tmp/firefox-omni
    +./patch-firefox-omni.zsh:12> cd /tmp/firefox-omni
    +./patch-firefox-omni.zsh:13> unzip -q /usr/lib/firefox/browser/omni.ja
    +./patch-firefox-omni.zsh:15> patch chrome/browser/content/browser/browser.xhtml /home/username/repo/important/firefox-omni/src/firefox-omni.patch
    patching file chrome/browser/content/browser/browser.xhtml
    Reversed (or previously applied) patch detected!  Assume -R? [n] yes
    Hunk #1 succeeded at 270 (offset 7 lines).
    Hunk #2 FAILED at 284.
    1 out of 2 hunks FAILED -- saving rejects to file chrome/browser/content/browser/browser.xhtml.rej

    Any input is much appreciated. Thank you!

  • Gautam Iyer
    Re: error

    Gautam Iyer (2023-05-10 08:59:52 EDT)

    patching file chrome/browser/content/browser/browser.xhtml
    Reversed (or previously applied) patch detected!  Assume -R? [n] yes
    Hunk #1 succeeded at 270 (offset 7 lines).
    Hunk #2 FAILED at 284.

    This error means you already applied the patch, and don’t need to use it again.

    I can confirm that this patch works on Firefox 113 (on Arch Linux) with firenvim as of 2023-05-10

๐Ÿ“ฎ 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.