35
  1. This is not about new macros like \newcommand, \def and others.

  2. This is not about autocompletion like using .cwl (in TexStudio)

  3. This is also not about using shortcuts for predefined commands that are listed in the shortcuts sections.

It is more closely related to the 3. Though. I want to define shortcuts for commands that are not listed there.

e.g. When I type Ctrl + Shift + B, I want

\begin{bmatrix} \end{bmatrix}

to be inserted just like Ctrl+ Enter inserts \\.

3
  • You mean new key bindings?
    – Joseph Wright
    Aug 20, 2013 at 19:40
  • 1
    @JosephWright, what is the difference between keyboard shortcut and keybinding? Regardless of "terms", I want the \begin... stuff to be inserted at cursor position when I press the Ctrl+Shift+B combination. That's all. Problem is, unlike latex commands like \begin{equation} \end{equation}, I cound not find the \begin{bmatrix} command itself there. So, I cannot define the shortcut.
    – user35451
    Aug 20, 2013 at 19:49
  • I, sadly, cannot illustrate with screenshot.
    – user35451
    Aug 20, 2013 at 19:59

2 Answers 2

51

In TeXstudio, to create a new code snippet and associate it with a keyboard shortcut, you can create a Macro and then assign a Shortcut to it.

To do this:

1. Go to the "Macros" menu and choose "Edit Macros"

2. In the "Edit Macros" dialog, click on the "Add" button, in the lower left corner of the dialog

3. Give the "Macro" a Name, fill the "Abbreviation" text field with $..$ and put your LaTeX code in the "`LaTeX Content" text area with your LaTeX code in this method leave the "Type" radio button with the default value of "Normal"), as illustrated below:

"Edit Macros" dialog

EDIT: As an alternative method, in this "Step 3", (Step of the "Edit Macros" dialog) - and since the idea is to "create" a new environment (in this case, bmatrix) - you may choose the "Environment" radio button, in the "Type" option, instead of the default value of "Normal". If you do that, make sure that %bmatrix (yes, that percentage sign before bmatrix is supposed to be there) is the content in the "LaTeX Content"text area (and clear the content of the "Abbreviation" text field). The advantage of this alternative method, as I see it, is that the cursor will be "correctly" placed inside the environment when the Macro is activated. Here's a screenshot for this step with this alternative method:

enter image description here

4. Click in the "OK" button to close the dialog

5. Go to the "Options" Menu and choose the "Configure TeXstudio..." command (first command of the Options Menu)

6. In the "Configure TeXstudio..." dialog, choose "Shortcuts" in the vertical navigation in the left side, and expand the "Menus" -> "Macros" option in the right hand side, to reveal your Macros, including your newly added "3: My Bmatrix Macro" like shown here:

"Configure TeXstudio" dialog

7. Double click the "Current Shortcut" column and press the "Ctrl+Shift+B" key combination. This will change the value of the column to "Ctrl+Shift+B"

You'll probably get a warning dialog box saying that the Keyboard shortcut "Ctrl+Shift+B" is already assigned to the command "Unnamed Bookmark". Click on the "Yes" button to accept the replacement of the Keyboard Shortcut.

8. Click on the "OK" button on the "Configure TeXstudio..." dialog to close it.

That's it! :-) Pressing Ctrl+Shift+B in your documents will insert the code for the bmatrix environment, from now on.

5
  • 2
    Is there any way to save this configuration so I could send it to myself on another computer? Mar 26, 2015 at 22:55
  • 1
    The macros I define seem to document specific; is there a way to create new macros which holds for every .tex document I edit?
    – MJS
    Apr 27, 2015 at 16:14
  • 1
    Is there any way to put the cursor within the \begin... and \end... environment after I press the shortkey? The default position of the cursor after using the shortcut is at the end of the code, which is not very convenient. Jul 13, 2017 at 20:53
  • 7
    @XiaodongQi Place %| in the line between \begin... and \end... in the macro script.
    – Troy
    Oct 22, 2017 at 7:56
  • @Troy: You should absolutely add the %| tip to your answer. Nov 11, 2021 at 16:42
0

I suggest using the "trigger" option instead of shortcuts given the macros are so useful for faster typing and if we use shortcuts could be more complex to remember them.

For example, I created a macro to a definition environment that is triggered by the specific command. In this way, when I type ::def, it is replaced by

\begin{definition}

\end{definition}

You must log in to answer this question.

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