54

I am using Chrome on a MacBook. I am used to the Emacs keyboard shortcuts available almost everywhere in the window manager, shell prompt and browser, such as Ctrl+A, Ctrl+E, Ctrl+F, Ctrl+B, Ctrl+N, Ctrl+P, Ctrl+K and Ctrl+Y for cursor movement and simple editing.

The Stack Overflow editor redefines some of these by default: Ctrl+B for bold, Ctrl+K for code, etc. while not the others. How can I disable such a redefinition?

There is a user profile option to enable keyboard shortcuts, but it does not apply to these.

8
  • 6
    On MSE: Is there a way to disable the hotkeys? status-ignored
    – jscs
    Commented Mar 15, 2016 at 1:39
  • 1
  • @JoshCaswell: I was wondering how to ask you in person this question. SO designers do not seem keen on helping those of us who have acquired medullar reflexes. Do you have a fix or are you still annoyed many times a day?
    – chqrlie
    Commented Mar 15, 2016 at 1:47
  • I installed RobW's script documented in this answer: meta.stackexchange.com/a/123256/285279 but it almost never works.
    – chqrlie
    Commented Mar 15, 2016 at 1:49
  • 1
    @chqrlie: This still drives me crazy, yes. :) At this point I've resorted to copy-pasting the post text that I want to write/edit into TextMate or Emacs! I also tried that script, or something very similar, and likewise had inconsistent results.
    – jscs
    Commented Mar 15, 2016 at 2:20
  • @JoshCaswell: I have a solution! I modified RobW's script along an idea by Rod Neyrehm: blog.rodneyrehm.de/archives/… . The idea is to prevent any element from grabbing certain keys. I will post an answer.
    – chqrlie
    Commented Mar 15, 2016 at 2:35
  • 4
    Try being a German Java, Ruby, or Perl developer. @ on a German keyboard is Ctrl+Alt+q which the StackOverflow editor interprets as Ctrl+q, or blockquote. It is simply impossible to type an @ sign into the SO editor with a German keyboard layout. My "fix"? Spend 1500€ on a Mac, where the @ sign lives on a different key. (Well, I admit, that wasn't the reason I bought it, but it was a nice side-effect.) Commented Mar 16, 2016 at 20:42
  • 3
    This is StackOverflow's biggest UI problem. It hits me literally every time I ask or answer a question, and those two activities are supposedly what this site is all about. I can't believe it has gone unanswered for so long. Commented Aug 10, 2016 at 20:00

2 Answers 2

27

I had installed RobW's script to disable the hotkeys in the SO editor, but it does not work anymore. Here is a different script that disables specific keys for all elements in the document. I have put the 4 emacs bindings that are overridden by the SO editor (WMD), but other shortcuts should be added for MacOS users with muscle memory.

I got the idea from http://blog.rodneyrehm.de/archives/23-Reclaim-Your-Keyboard-Shortcuts-in-Firefox.html

Install as a User Script:

// ==UserScript==
// @name           OSX shortcut disabler 
// @namespace      chqrlie
// @version        1.0
// @match          https://stackoverflow.com/*
// @match          https://*.stackoverflow.com/*
// @match          https://superuser.com/*
// @match          https://meta.superuser.com/*
// @match          https://serverfault.com/*
// @match          https://meta.serverfault.com/*
// @match          https://askubuntu.com/*
// @match          https://meta.askubuntu.com/*
// @match          https://*.stackexchange.com/*
// @match          https://mathoverflow.net/*
// @match          https://*.mathoverflow.net/*
// @match          https://stackapps.com/*
// @run-at         document-end
// @grant          none
// ==/UserScript==

(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window).$(function() {
    document.addEventListener('keydown', function(e) {
        // Prevent propagation of emacs key bindings
        if (e.ctrlKey) {
            switch (e.keyCode) {
              case 0x42: // ^B  backward-char
              case 0x49: // ^I  indent-line
              case 0x4B: // ^K  kill-line
              case 0x59: // ^Y  yank
                e.stopImmediatePropagation();
                return;
            }
        }
    }, true);
});

Install as a Chrome extension:

  1. Create a directory, and store the following two files in it.
  2. Visit chrome://extensions/
  3. Enable Developer mode
  4. Click on the button Load unpacked extension....
  5. Select the directory from step 1.

manifest.json

{
    "name": "Chqrlie shortcut disabler",
    "manifest_version": 2,
    "version": "1.0",
    "content_scripts": [{
        "js": ["contentscript.js"],
        "matches": [
            "https://stackoverflow.com/*",
            "https://*.stackoverflow.com/*",
            "https://superuser.com/*",
            "https://meta.superuser.com/*",
            "https://serverfault.com/*",
            "https://meta.serverfault.com/*",
            "https://askubuntu.com/*",
            "https://meta.askubuntu.com/*",
            "https://*.stackexchange.com/*",
            "https://mathoverflow.net/*",
            "https://*.mathoverflow.net/*",
            "https://stackapps.com/*"
        ]
    }]
}

contentscript.js

document.addEventListener('keydown', function(e) {
    // Prevent propagation of emacs key bindings
    if (e.ctrlKey) {
        switch (e.keyCode) {
          case 0x42: // ^B  backward-char
          case 0x49: // ^I  indent-line
          case 0x4B: // ^K  kill-line
          case 0x59: // ^Y  yank
            e.stopImmediatePropagation();
            return;
        }
    }
}, true);
7
  • 3
    Nice. You should post this on stackapps.com ;) Commented Mar 15, 2016 at 9:27
  • @404: Thanks, updated. Is there an official list? Is there a way to coalesce domain.com and *.domain.com?
    – chqrlie
    Commented Mar 16, 2016 at 7:50
  • 1
    This doesn't work on Mac/OS X v10.11.6, Chrome v58.0.3029.110. Still activates the text formatting shortcuts of Stack Overflow. Commented Jun 26, 2017 at 20:50
  • 2
    @user3112401 I fixed the code above by switching the match clauses to use https: URLs instead of http:. Commented Dec 9, 2017 at 0:12
  • // @include /^https?:\/\/(?!chat)\w*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/.*/
    – user4639281
    Commented Dec 9, 2017 at 19:21
  • Userscripts (apps.apple.com/us/app/userscripts/id1463298887?mt=12) is a free extention that helped me to save the script. Could I also as a keybinding for backward kill a word? @chqrlie
    – alper
    Commented May 21, 2020 at 10:49
  • Can I apply the exact code on other editors such as hackerrank's code editor bottom of the page (hackerrank.com/challenges/py-hello-world/problem)?
    – alper
    Commented Jun 7, 2020 at 22:04
0

Install my open source Chrome extension: No Stack Exchange Editor Keyboard Shortcuts.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .