yzhang-gh / vscode-markdown

Markdown All in One
https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one
MIT License
2.9k stars 322 forks source link

Ctrl+B takes over "hide sidebar" shortcut #89

Open alensiljak opened 6 years ago

alensiljak commented 6 years ago

As noted in the title, Ctrl+B takes over "hide sidebar" shortcut, which is one of the most-used shortcuts in VSCode for me. It is also helpful for quickly toggling the document outline.

Is it possible to customize the shortcut for bolding the text to Ctrl+Shift+B, which seems to be standard in other editors? Thanks again!

yzhang-gh commented 6 years ago

Copied from #88.

image

image

alensiljak commented 6 years ago

Fantastic. I find ctrl+shift+b a better option since it is a Build shortcut, which might not be that significant for markdown files. Cheers!

gandalfsaxe commented 6 years ago

Shouldn't a more sensible default be set that doesn't override one of the arguably most important shortcuts in all of VSC?

All of these pairs are available in VSCode without any conflict:

I propose one of the first two since using ⌘ feels more natural coming from ⌘B and ⌘I. Perhaps the best is ⌥⌘B and ⌥⌘I since the latter can be invoked only with the right hand 🤙

gandalfsaxe commented 6 years ago

UPDATE for windows:

On windows, Ctrl+Alt+B is taken up by the rather important Build LaTeX Project from the LaTeX Workshop extension, and Ctrl+Shift+B is Run build task. But Ctrl+Shift+B is free (at least with the rather extensive set of extensions I have installed).

Both Ctlr+Alt+I and Ctrl+Shift+I is free for italic. I'd suggest Ctrl+Alt+I since it's much better ergonomically on many keyboards when typing it with just the right had.

gandalfsaxe commented 6 years ago

I understand if you don't want to change the rather simple Ctrl+B and Ctrl+I keys that people are used to from most other contexts. However the fact is that people who use VSCode as their markdown editor is very likely to be technically proficient, and most will need to invoke show/hide sidebar frequently.

So they and will have to change the keymap (many perhaps can't be bothered and live with the inconvenience), but my guess is that most would appreciate not having such an important shortcut overriden.

Just my 2 cents :) I can see why advertising the simple CMD+B, CMD+I makes for a simpler story.

yzhang-gh commented 6 years ago

These shortcuts are "inherited" from the common used WYSIWSW softwares (e.g. Word, Typora, Caret). Personally I think it is quite natural.

Default settings (including keybindings) are always hard choices. Everyone has his/her own preferences. I am always open to these discussions. Let's leave this issue open to collect more "cents"

gandalfsaxe commented 6 years ago

Yeah I totally understand where you’re coming from. I really wish that Cmd+B wasn’t already taken by something that prominent, because I would’ve accepted it to override most commands. But on a laptop screen I find myself using Cmd+B quite frequently. But as you mention, Cmd B/I are very well established.

I also think it’s reasonable to get more opinions, as I’m actually not so sure myself, what the majority sentiment is on this. Luckily it’s not the biggest big deal since you can change the keys yourself. But of course it’s always good to have the best possible defaults.

alistair-hmh commented 5 years ago

I just wanted to second what @GandalfSaxe said.

I think overriding the behavior of VSCode could be frustrating for users with visual acuity or motion problems.

What if the user is unable to use the mouse?

gandalfsaxe commented 5 years ago

I wouldn't mind it if it was a lesser used shortcut, but Cmd+B is, IMO, a quite essential shortcut.

Here is how I ended up tackling it (if anybody get any better ideas, let me know):

{
    "key": "ctrl+b",
    "command": "markdown.extension.editing.toggleBold",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown' && isMac"
  },
  {
    "key": "cmd+b",
    "command": "-markdown.extension.editing.toggleBold",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'" // FIXME: Windows only when supported (https://github.com/Microsoft/vscode/issues/56382)
  },
  {
    "key": "ctrl+shift+alt+b", // NO-GO: ctrl+shift+b Run Build Task, ctrl+alt+b Build LaTeX project, shift+alt+b Toggle Git Code Lens, alt+b Toggle File Blame Annotations
    "command": "markdown.extension.editing.toggleBold",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown' && isWindows"
  },
  {
    "key": "ctrl+b",
    "command": "-markdown.extension.editing.toggleBold",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'" // FIXME: Windows only when supported (https://github.com/Microsoft/vscode/issues/56382)
  },
  // toggleItalic: KEY CONFLICT - overwrites expandLineSelection
  {
    "key": "ctrl+i",
    "command": "markdown.extension.editing.toggleItalic",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown' && isMac"
  },
  {
    "key": "cmd+i",
    "command": "-markdown.extension.editing.toggleItalic",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'" // FIXME: Windows only when supported (https://github.com/Microsoft/vscode/issues/56382)
  },
  {
    "key": "ctrl+shift+alt+i",
    "command": "markdown.extension.editing.toggleItalic",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown' && isWindows"
  },
  {
    "key": "ctrl+i",
    "command": "-markdown.extension.editing.toggleItalic",
    "when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'" // FIXME: Windows only when supported (https://github.com/Microsoft/vscode/issues/56382)
  }
yzhang-gh commented 5 years ago

@GandalfSaxe Thanks for sharing

@alistair-hmh You can easily custom them to your preferences (change or delete)

image

image

gacallea commented 5 years ago

just a +1 to the keyboard shortcuts issues. I like the ⌥⌘B and ⌥⌘I proposed by @GandalfSaxe

simkimsia commented 5 years ago

I came across this while looking at other issues.

B is actually quite the standard practie for bold. Even right here, in GitHub comments, you bold using the same keys.

VSCode actually provides another way to toggle the sidebar aka explorer view. The below is taken directly from my own notes while keeping B for bold in markdown files.

Hope this helps other people without changing native keyboard shortcuts for vscode

Explorer view

When not visible and want to show explorer, use sidebar focus ⌘ cmd 0

When visible and want to hide explorer, use sidebar focus ⌘ cmd 0 then B

gandalfsaxe commented 4 years ago

@simkimsia I thought about it for a sec, but don't want the toggle to depend on where my focus is. On the other hand, I also figured that it would be easier to remap ⌘B instead of Bold + Italic, despite my using it more.

Also I noticed that ⇧⌘0 (Mac) / Ctrl+Shift+0 (Win) is free, so I ended up just remapping workbench.action.toggleSidebarVisibility to those instead and leave Bold / Italic default. Note that if you have multiple languages setup, you may have to disable the Ctrl+Shift shortcut to get it working (https://github.com/Microsoft/vscode/issues/2585).

I.e. Settings -> Devices -> Typing -> Advanced Keyboard Settings -> Change Key Sequence... -> Switch Keyboard Language [Not Assigned].

gandalfsaxe commented 4 years ago

Another possiblity could be to have ⌘0 toggle the sidebar if it's already selected. Though I'd have to do ⌘0 once if it's in focus , and ⌘0 twice if it's not. I thought this over also, but again didn't want to make it conditional, but just consistent.

wmayner commented 4 years ago

Just wanted to add my voice to the others who've commented that overriding a very common and basic VSCode keyboard shortcut is jarring and unexpected.

I appreciate that ⌘B is used in many other editors to mean “toggle bold”, but IMHO it's not worth it here.

kortina commented 4 years ago

In addition to remapping the extension keybinding, I had to remap the default to get this to work:

    // cmd+b, the default, is the built in shortcut to
    // Hide / Show Side Bar, so release extension binding:
    {
        "key": "cmd+alt+b",
        "command": "markdown.extension.editing.toggleBold",
    },
    {
        "key": "cmd+b",
        "command": "workbench.action.toggleSidebarVisibility",
    },
Spongman commented 3 years ago

never, ever override existing shortcuts. very poor form. i'm surprised vscode even allows this without prompting. i have uninstalled this extension, and now i'm having to spend time resetting my config back to how it was. i wish i had never installed it in the first place. avoid at all costs.

Lemmingh commented 3 years ago

i'm surprised vscode even allows this without prompting.

I guess it's technically impossible to prompt: How do you perform such complex analysis? Key binding precedence is determined dynamically at runtime. You can never know the actual key binding before getting in a specific context.

Besides, there're many keymap extensions. If such a prompt mechanism were introduced, users would receive a huge number of meaningless notifications.

See Visual Studio Code Key Bindings, or its page source at https://github.com/microsoft/vscode-docs/blob/5ae4732308b236cb87b8eaf750a4910d969d85aa/docs/getstarted/keybindings.md

Lemmingh commented 3 years ago

If you find a better solution than this, please share. We appreciate it.

https://github.com/yzhang-gh/vscode-markdown/blob/1ddf061ae766338b9669427ed5f9109c4f117914/package.json#L92-L97

We have tried to inform you: https://github.com/yzhang-gh/vscode-markdown/blob/1ddf061ae766338b9669427ed5f9109c4f117914/README.md#q-this-extension-has-overridden-some-of-my-key-bindings-eg-ctrl--b-alt--c

john-french commented 2 years ago

100% agree that overriding a major editor key binding is a no-no. I don't have an opinion on what it should change to, just think that it should definitely change to some available key binding.

krsche commented 1 year ago

My two cents would be to switch defaults to Ctrl+Shift+B for bold and Ctrl+Shift+I for italics.

Not too far from the usual keybindings of other editors and the Run build task command isn't really needed when in a markdown file, but I hide/unhide the sidebar with Ctrl+B all the time :)

RichardJECooke commented 1 year ago

Arg, why am I having to Google this weird error 6 years later. Please just turn off ctrl-b in your plugin, whoever made this thing that broke my VSCode

kortina commented 1 year ago

The main reason I was using this extension was to automatically get a "-" character when adding a newline to an unordered list. I uninstalled this extension eventually when I figured out how use vscode-vim to get this behavior with the following in my settings.json:

...
"vim.insertModeKeyBindings": [
    // insert the current date, insert mode
    {
      "before": ["<C-d>"],
      "after": [],
      "commands": [
    // remap enter key in insert mode to preserve indentation and insert a - if the current line begins with a hyphen (is a list item)
    {
      "before": ["<cr>"],
      "after": [],
      "commands": [
        {
          "command": "editor.action.insertSnippet",
          // not sure this is restricting to markdown files only...
          "when": "editorLangId == markdown",
          "args": {
            "snippet": "${TM_CURRENT_LINE/^(\\s*)(- )?.*$/\n$1$2/gm}"
          }
        }
      ]
    }
  ]
},
...

https://github.com/kortina/dotfiles/commit/a2728edfb9451fc66a83816c7022fd18966e6b00#diff-75113530365f5f42f8a09821bec608e4a45936a14d82caaef865fb7350cbb010R180

starball5 commented 3 months ago

Related on Stack Overflow: Prevent ctrl-B from setting bold font while working with md files with the Markdown All in One extension

pearj commented 2 months ago

I personally think the best option is to make the when condition more strict, so that ctrl + b only applies when text is highlighted. The key here is using editorHasSelection. That way when no text is selected the sidebar can still open, and when you have highlighted something then bold works.

I remember reading this a few years ago, but I don't know from where.

Perhaps the default shortcut in markdown all-in-one should be changed to include editorHasSelection in it?

keybindings.json fragment:

    {
        "key": "ctrl+b",
        "command": "markdown.extension.editing.toggleBold",
        "when": "editorHasSelection && editorTextFocus && !editorReadonly && editorLangId =~ /^markdown$|^rmd$|^quarto$/"
    },
    {
        "key": "ctrl+b",
        "command": "-markdown.extension.editing.toggleBold",
        "when": "editorTextFocus && !editorReadonly && editorLangId =~ /^markdown$|^rmd$|^quarto$/"
    }