0

I would like to create a keyboard shortcut that opens a terminal tab and then runs a command that includes the filename. I have tried using multi-command with Terminal Here in the following way:

"multiCommand.commands":[
    {
        "command": "multiCommand.formatLaTeX",
        "interval": 750,
        "sequence":[
            "workbench.action.files.save",
            "terminalHere.create",
            {
                "command": "workbench.action.terminal.sendSequence",
                "args": {"text": "pdftex -ini -jobname=\"${fileBasenameNoExtension}\" \"&pdflatex\" mylatexformat.ltx ${fileBasename}\u000D"},
            }
        ]
    }
]

This saves the file, opens a terminal in a new window in the same directory, and then tries to run the command.

When I use "normal" characters that don't include filenames, i.e. no ${fileBasename}, it works perfectly.

How can I make it work when I use the file names? For example, if I'm working in foo.tex, I want it to run the command

pdftex -ini -jobname="foo" "&pdflatex" mylatexformat.ltx foo.tex

and then execute it.

rioV8
  • 24,506
  • 3
  • 32
  • 49
btshepard
  • 131
  • 5
  • Does this question help you? https://stackoverflow.com/q/52786022/4652564 – Nicolapps Nov 23 '22 at 16:10
  • No, I already am using the "args": {"text": } setup – btshepard Nov 23 '22 at 16:22
  • I tried to run your example on my machine and it seems to be already working as expected. What issues are you running into? – Nicolapps Nov 23 '22 at 16:36
  • Does the variable substitution work if you omit the `terminalHere.create` command? And does variable substitution work if you use `workbench.action.terminal.new` instead of the the `terminalHere` command? – Mark Nov 23 '22 at 16:39
  • It does work if I omit the `terminalHere.create`, but the whole point of my command was to have a single command that does both. Using `workbench.action.terminal.new` doesn't help at all – btshepard Nov 23 '22 at 16:45
  • The issue is that it opens a terminal but never actually "writes" the command nor runs it. I just see a blank terminal screen. – btshepard Nov 23 '22 at 16:46
  • I understand what you are trying to do, I was just trying to figure out if the `terminalHere` command was the problem. I'll post something else to try. – Mark Nov 23 '22 at 16:54

1 Answers1

0

Make this keybinding (in keybindings.json), if it works you can make it into a setting if you wish - obviously I can't test your exact shell command:

  {
    "key": "ctrl+shift+/",
    "command": "extension.multiCommand.execute",
    "args": {
      "interval": 750,
      "sequence": [
        "workbench.action.files.save",
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {"text": "cd '${fileDirname}' && echo  Howdy \u000d"}
        }
      ],
    },
    "when": "editorTextFocus"
  },

Also, you should wrap your variables that might have spaces in them like this: '${fileDirname}', etc. Replace the echo... with your shell command and see if it works.

It may be necessary to split the two shell commands into two separate commands so the second command is run in the new directory:

  {
    "key": "ctrl+shift+/",
    "command": "extension.multiCommand.execute",
    "args": {
      "interval": 750,
      "sequence": [
        "workbench.action.files.save",
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {"text": "cd '${fileDirname}'\u000d"}
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "echo '${fileDirname}'\u000d"  // this does work for me
          }
        },
      ],
    },
    "when": "editorTextFocus"
  },
Mark
  • 143,421
  • 24
  • 428
  • 436