sindresorhus/refined-github

Do you want to work on this issue?

You can request for a bounty in order to promote it!

Shortcut to format selected text as code #2032

waldyrious posted onGitHub

There are already shortcuts for making text bold, italicized and linked in comment boxes. One formatting style that I use all the time (perhaps even more than bold and italics) is code, using backticks (`...`).

As someone who uses a keyboard layout where <kbd>`</kbd> is a dead key (so it allows typing accented letters like Γ , etc.), I find myself wanting a shortcut pretty often.

Even for those using keyboards where it works as a normal key, I can imagine that selecting some text and pressing a shortcut would be more convenient than moving the cursor to the start of the intended selection, typing <kbd>`</kbd>, then moving it to the end of the selection and typing <kbd>`</kbd> again.

(The same could be done for quoting selections with ' or ", by the way, but that's a separate request.)


@bfred-it, @notlmn β€” while I appreciate the auto-wrap functionality introduced in #2145, unfortunately it doesn't really address the dead key case I described above:

As someone who uses a keyboard layout where <kbd>`</kbd> is a dead key (so it allows typing accented letters like Γ , etc.), I find myself wanting a shortcut pretty often.

This is still the case even after the changes in #2145. If I select some text and type <kbd>`</kbd>, the text is simply replaced with it, whereas typing e.g. <kbd>"</kbd> indeed wraps the selection in "...".

Considering that the implemented feature is more of a smart substitution and not an actual shortcut (like <kbd>Ctrl</kbd>+<kbd>K</kbd> for inserting links), I'd request this issue to be reopened.

posted by waldyrious almost 6 years ago

If I select some text and type , the text is simply replaced with it, whereas typing e.g. " indeed wraps the selection in"..."`.

When using v19.6.26, pressing <kbd>`</kbd> does actually wrap selected text with that key. Are you using some other way to trigger insertion of backtick character into the textarea.


Considering that the implemented feature is more of a smart substitution and not an actual shortcut (like Ctrl+K for inserting links), I'd request this issue to be reopened.

Can you expand a bit more on this. Do you want something like <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>K</kbd> (or something similar) to wrap the selected text with <kbd>`</kbd>?


PS: Just discovered that GitHub lets you use <kbd>Ctrl</kbd> + <kbd>I</kbd> and <kbd>Ctrl</kbd> + <kbd>B</kbd> for wrapping and even unwrapping text (works with and without selection) to italicize and make text bold respectively.

posted by notlmn almost 6 years ago

When using v19.6.26, pressing <kbd>`</kbd> does actually wrap selected text with that key. Are you using some other way to trigger insertion of backtick character into the textarea.

Can you expand a bit more on this.

Yes, sure. I'm talking about keyboard layouts where <kbd>`</kbd> is a dead key. In my particular case, Portuguese. (That means that if I press the <kbd>`</kbd> key, nothing is actually inserted until I type another key that doesn't combine with it, such as space.)

I've tested this behavior in both macOS and Linux, and the result is the same: the selected text gets replaced by `.

Do you want something like <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>K</kbd> (or something similar) to wrap the selected text with <kbd>`</kbd>?

Yes, exactly!

PS: Just discovered that GitHub lets you use <kbd>Ctrl</kbd> + <kbd>I</kbd> and <kbd>Ctrl</kbd> + <kbd>B</kbd> for wrapping and even unwrapping text (works with and without selection) to italicize and make text bold respectively.

Indeed! That was my motivation to request this feature β€” in fact, it's the first sentence in the opening comment! πŸ˜„

I think that for a code hosting site like GitHub, it makes a lot of sense to have also a shortcut for formatting text as code.

and even unwrapping text (works with and without selection)

Yes, this is very convenient as well. I've noticed that since this feature went live, sometimes I select text wanting to actually replace it with a single or double quote mark, and this feature now wraps it in them, and there's no way to undo that (<kbd>Ctrl</kbd>+<kbd>Z</kbd> does nothing). So the toggling behavior would be great indeed.

posted by waldyrious almost 6 years ago

Now I understand what this is about, it seems I misunderstood this whole request right from the beginning. I went with the wrapping part as I frequently found myself using forward and backward keys while formatting code, wrapping selected text on keypress did make sense in that case (for me anyways πŸ˜…).

Apart from the keyboard layout changes and dead keys part, as you mentioned it does make a lot of sense to have a key combination to format text as code, and especially when GitHub already has implemented some for bold and emphasis. πŸ‘


I've noticed that since this feature went live, sometimes I select text wanting to actually replace it with a single or double quote mark, and this feature now wraps it in them, and there's no way to undo that (Ctrl+Z does nothing). So the toggling behavior would be great indeed.

If you are testing this in Firefox, the undo bug is real, and this limitation is mentioned right in insert-text-textareas readme.

If we needed wrapping and unwrapping support for the new shortcut, then this goes way beyond than insert-text-textarea, as GitHub does that for text that is selected or otherwise. Maybe we can dig into GH's implementation to figure that part out.


Technically speaking we could add a keyboard shortcut to each formatting type available in Markdown (like heading, and lists), but that would be way too much code. And according to the Wikipedia article you mentioned above, it looks like <kbd>~</kbd> (used for strike-through) is also a dead character in some keyboard layouts.

So if that were the case, where should we draw the line. Just add shortcut for <kbd>`</kbd> and call it a day, or also add another keyboard shortcut to <kbd>~</kbd>.

IMO, adding shortcut just for <kbd>`</kbd> should be enough, as it is often used more than <kbd>~</kbd>.


@bfred-it @sindresorhus WDYT, should we open this, what are your thoughts?

posted by notlmn almost 6 years ago

If you are testing this in Firefox, the undo bug is real, and this limitation is mentioned right in insert-text-textareas readme.

Thanks, I've started following the bug. πŸ‘

Technically speaking we could add a keyboard shortcut to each formatting type available in Markdown (like heading, and lists), but that would be way too much code.

Agreed.

it looks like <kbd>~</kbd> (used for strike-through) is also a dead character in some keyboard layouts.

Yup. The Portuguese layout has that as well.

IMO, adding shortcut just for <kbd>`</kbd> should be enough, as it is often used more than <kbd>~</kbd>.

Sounds reasonable to me.

posted by waldyrious almost 6 years ago

Saw this now. Interestingly, when either one of those keys is pressed on a Portuguese keyboard, I get this info whether it's <code>`</code> or <code>Β΄</code>:

Event {
    key: 'Dead',
    keyCode: 192,
    code: 'BracketRight'
    ...
}

You can try submitting a PR for this after #2441, because I don't know how to detect such keys.

posted by fregante over 5 years ago

@fregante since this issue was about a shortcut from the start, rather than an extra mode to the one-key-formatting feature, could it be re-opened to preserve context, as I and @notlmn suggested in the previous comments?

posted by waldyrious over 5 years ago

this issue was about a shortcut from the start, rather than an extra mode to the one-key-formatting feature

A shortcut to do what? From your first post, it seems you're describing exactly one-key-formatting, except that currently it doesn't work with dead keys.

posted by fregante over 5 years ago

Indeed it could have been written more clearly, though guess it depends on how you interpret what "shortcut" means. I was referring to "keyboard shortcut", like <kbd>Ctrl</kbd>+<kbd>B</kbd> for bold, not shortcut as "a faster way to do something".

In fact, if the latter were the case, instead of

selecting some text and pressing a shortcut would be more convenient than moving the cursor to the start of the intended selection, typing <kbd>`</kbd>, then moving it to the end of the selection and typing <kbd>`</kbd> again.

I'd probably have written

selecting some text and pressing <kbd>`</kbd>...

I didn't want to tie my proposal to a specific shortcut like, say, <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>K</kbd>, but I realize now that not mentioning a specific example introduced ambiguity. Sorry about the confusion. 😞

posted by waldyrious over 5 years ago

So, again, it seems you're describing exactly one-key-formatting, except that currently it doesn't work with dead keys.

If that's the case, open a new bug issue asking to make one-key-formatting work with dead keys, perhaps adding a gif of what happens.

posted by fregante over 5 years ago

Hello! πŸ‘‹πŸΌ Just letting you know that this is now available directly on GitHub.com via <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>e</kbd> 😊

https://twitter.com/natfriedman/status/1375188490393022465?s=21

posted by lumaxis about 4 years ago

Not exactly and not fully. This feature’s intent is to match a regular editor’s behavior when pressing one of these keys, not just wrapping code. Plus we support 11 keys/characters https://github.com/sindresorhus/refined-github/blob/eecde20bf1c517c8db47d019865358ca96444062/source/features/one-key-formatting.tsx#L9

posted by fregante about 4 years ago

@fregante I'm confused. While one-key-formatting is a very useful feature, it's not what I understand @waldyrious originally requested here.

Either way, seemed like this new built-in feature was at least tangentially related, so wanted to leave a note here πŸ™‚

posted by lumaxis about 4 years ago

Yeah, @lumaxis is right that the functionality now introduced by github is what I had in mind when I opened this bug. Unfortunately, the conversation got tangled with the one-key-formatting feature, which is a different (arguably more convenient) way to obtain the same effect, but unlike the keyboard shortcut approach, it doesn't work with characters that are dead keys in certain keyboard layouts. Either way, I'm happy this is now available!

posted by waldyrious about 4 years ago

Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests