I know there already are questions (1, 2) on creating keyboard shortcuts in TeXworks. The discussion (also in the Google thread referred to), however, seems a little outdated.

By now, there is the possibility to create keyboard shortcuts via the shortcuts.ini file (link) as well as keywords via adding a .txt file to the completion folder (link, p.23).

What I am trying to achieve is: create a keyboard shortcut (say, ctrl+shift+v) which inserts a code snippet such as \vec{#INS#}• (which would insert the snippet \vec{}, move the cursor to #INS# and add a placeholder at ).

While the first part (create a keyboard shortcut for some command) as well as the second one (create a keyword which expands to the full snippet) can be carried out separately, I can't get the combination to work - I only figured how to add shortcuts for commands listed at the bottom of this explanation.

Any help on how to perform the desired task would be appreciated. If my question is not clear, please let me know.

1 Answer 1


I fancied a challenge answering an ancient question with no answers or comments. This is possible using scripts within TeXWorks (note: I have a custom dark theme for TeXWorks hence the background):

enter image description here

The steps to create a custom command (in your case \vec{}) mapped to a keyboard shortcut (in your case Ctrl+Shift+v):

  1. From file explorer, go to: C:\Users\[Your User]\.texlive2022\texmf-config\texworks\scripts on Windows and its equivalent on Linux or Mac (I only use Windows so do not know exactly).
  2. Your folder should look similar to this (assuming you do not already have custom scripts):

enter image description here

  1. Within \.texlive2022\texmf-config\texworks\scripts, create a new folder name "Custom" or something similar.

  2. Open notepad or an equivalent plain-text programme, name it vec.js (needs to be .js, javascript. Can be named whatever you desire as long as it is unique) and save it within /scripts/Custom/.

  3. Right click and edit with whatever text programme you desire vec.js and paste the following into it (credit for the .js addOrRemove code is with Jonathan Kew):

// TeXworksScript
// Title: vec
// Description: Inserts \vec{} into text
// Author: JamesT (based on work by Jonathan Kew)
// Version: 0.1
// Date: 2022-02-16
// Script-Type: standalone
// Context: TeXDocument
// Shortcut: Ctrl+Shift+v

function addOrRemove(prefix, suffix) {
  var txt = TW.target.selection;
  var len = txt.length;
  var wrapped = prefix + txt + suffix;
  var pos = TW.target.selectionStart;
  if (pos >= prefix.length) {
    TW.target.selectRange(pos - prefix.length, wrapped.length);
    if (TW.target.selection === wrapped) {
      TW.target.selectRange(pos - prefix.length, len);
    TW.target.selectRange(pos, len);
  TW.target.selectRange(pos + prefix.length, len);

addOrRemove("\\vec{", "}•");
  1. Save and close the vec.js folder. Open TeXWorks and navigate to the Scripts -> Custom folder to verify that it has been created:

enter image description here

  1. Press Ctrl+Shift+v to verify it has worked.

This is how you can insert code snippets via keyboard shortcuts in TeXWorks and it looks very useful now I know how to do it too, good question!

You must log in to answer this question.

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