Latex-workshop: Feature Request: emphasize/italicise selected text with Keyboard Shortcut.

Created on 25 Feb 2018  路  6Comments  路  Source: James-Yu/LaTeX-Workshop

Description

In Texmaker one can select a text, that should be emphasized and press CTRL+B to wrap the text portion with \textbf{%TEXT%}.

Additional Information

CTRL+B is probably already in use, but one could certainly use other combinations.

Proposal

Would be great if one could select a text (%TEXT%) and press

  • CTRL+Shift+B to wrap it inside \textbf{%TEXT%} - alt. {\bfseries %TEXT%}, and
  • CTRL+Shift+I to wrap it inside \textit{%TEXT%} - alt. {\itshape %TEXT%}

Most helpful comment

You can use Visual Studio Codes internal keybindings.json for this, here is an example:

    {
        "key": "ctrl+shift+B",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "\\textbf{${TM_SELECTED_TEXT}}$0"
        }
    },
    {
        "key": "ctrl+shift+I",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus && editorLangId == latex",  // chained clause
        "args": {
            "snippet": "\\textit{${TM_SELECTED_TEXT}}$0"
        }
    }
Explanation
  • key clause declares which key should be pressed to trigger from the accepted keys.
  • command clause let's vscode know that this is a snippet insert command read more here.
  • when clause let's vscode know what context it is used in, as described here
    Note:_You can "chain" multiple clauses using && in for instance the when clause, see example above._
  • Variable in args.snippet ${TM_SELECTED_TEXT} wraps around as you asked. you can see possible variables here.
  • $0 sets position of cursor after command has triggered.

All 6 comments

You can use Visual Studio Codes internal keybindings.json for this, here is an example:

    {
        "key": "ctrl+shift+B",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "\\textbf{${TM_SELECTED_TEXT}}$0"
        }
    },
    {
        "key": "ctrl+shift+I",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus && editorLangId == latex",  // chained clause
        "args": {
            "snippet": "\\textit{${TM_SELECTED_TEXT}}$0"
        }
    }
Explanation
  • key clause declares which key should be pressed to trigger from the accepted keys.
  • command clause let's vscode know that this is a snippet insert command read more here.
  • when clause let's vscode know what context it is used in, as described here
    Note:_You can "chain" multiple clauses using && in for instance the when clause, see example above._
  • Variable in args.snippet ${TM_SELECTED_TEXT} wraps around as you asked. you can see possible variables here.
  • $0 sets position of cursor after command has triggered.

Great! Worked instantly. Thanks a lot. :blush:

Is there a way to activate those keybindings only for certain file extensions (e.g. tex)? :thinking:

Yes, by using multi-clause in when clause.

Like this: "when": "editorLangId == latex && editorTextFocus",

Keep in mind that you probably need to do some tweaking here.

Almost. :wink:

{r, engine='bash', code_block_name} ... "when": "editorlangId == latex && editorTextFocus",

Anyway, thank you.

oh, for me the L has to be capitalized or it wont work.

If you want to make it active for a precise file extention such as ".tex" you can use resourceExtname == .tex
I also replaced editorTextFocus by editorHasSelection which is better suited to my needs.
I finally end up with the following:

"when": "editorHasSelection && resourceExtname == .tex",
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jose-a-sa picture jose-a-sa  路  4Comments

dslemusp picture dslemusp  路  4Comments

iainmstott picture iainmstott  路  5Comments

LordScree picture LordScree  路  5Comments

TiemenSch picture TiemenSch  路  6Comments