0

I need to execute multiple commands in keybindings.json in vscode to be able to log variable name, filename and the variable value.

ex in dart

double x = 2.2; print("main.dart -> x $x"); 

the default behavior is to insertSnippet in the same line but i need to go to add new line below my variable then insert my custom snippet.

In addition i tried macros and multi-command. Unforgettably i couldn't reach the VSCode environment variables like TM_FILENAME_BASE and CLIPBOARD. They just print as a plane text something like

double x = 2.2; print("$TM_FILENAME_BASE x $x") 

Not able to resolve the env var to it's value.

this is from mutli-command extension settings.json

"multiCommand.commands": [ { "command": "myCommand", "sequence": [ "cursorDown", "insertSnippet", { "command": "type", "args": { "text": "print(\" log-> ${TM_FILENAME_BASE} -> ${TM_SELECTED_TEXT} -> ${${TM_SELECTED_TEXT}} \" );" } } ] }, ], 

and this one from keybindings.json

{ "key": "alt+p", "command": "editor.action.insertSnippet", "when": "editorTextFocus && resourceExtname == .dart", "args": { "snippet": "print(\" log-> ${TM_FILENAME_BASE} -> ${TM_SELECTED_TEXT} -> ${${TM_SELECTED_TEXT}} \" );" } } 

SOLUTION This is a tweaked solution for my issue inspired by Mark

from settings.json

 "multiCommand.commands": [ { "command": "multiCommand.printVariable", "sequence": [ "editor.action.clipboardCopyAction", // Copy the selected to the clipboard "editor.action.insertLineAfter", { "command": "editor.action.insertSnippet", "args": { "snippet": "print(\"${TM_FILENAME_BASE} -> ${CLIPBOARD} -> ${${CLIPBOARD}}\");" // then add it here } }, ] }, ], 

from keybindings.json

 { "key": "alt+p", "command": "extension.multiCommand.execute", "args": { "command": "multiCommand.printVariable" }, "when": "editorTextFocus && resourceExtname == .dart" } 
1
  • Please show your snippet and/or macro. So we can help fix what you wrote rather than writing it ourselves. Commented Jun 10, 2020 at 18:27

1 Answer 1

1

If you select your variable, this gives your desired output:

"multiCommand.commands": [ { "command": "multiCommand.printVariable", "sequence": [ "editor.action.clipboardCopyAction", "editor.action.insertLineAfter", { "command": "editor.action.insertSnippet", "args": { "snippet": "print(\"${TM_FILENAME_BASE} -> " } }, "editor.action.clipboardPasteAction", { "command": "type", "args": { "text": " $" } }, "editor.action.clipboardPasteAction", { "command": "type", "args": { "text": "\");" } }, ] }, 

You used the ${TM_FILENAME_BASE} so I did as well - it will strip off the extension - but you showed main.dart as part of your desired output so perhaps you wanted TM_FILENAME?

and your keybinding:

{ "key": "alt+p", // or whichever keybinding you choose "command": "extension.multiCommand.execute", "args": { "command": "multiCommand.printVariable" }, "when": "editorTextFocus && resourceExtname == .dart" }, 

Similarly to my answer How to create a shortcut to print a variable (vscode)

if your input is of the general form

double x = 2.2; // where the desired variable is right before an `=` 

you can simplify this alot and only use the following keybinding (and no macro) with your cursor at the end of the line - no selection:

{ "key": "alt+p", "command": "editor.action.insertSnippet", "args": { // works with cursor end of line, no selection "snippet": "\nprint(\"${TM_FILENAME_BASE} -> ${TM_CURRENT_LINE/\\s*\\w*\\b\\s*(.*?)\\s*=.*/$1 $$1\");/}", }, "when": "editorTextFocus && resourceExtname == .dart" }, 

Demo of both approaches. First with the selection and macro. Second, no selection, cursor at end of line and simple snippet.

Note I am in my keybindings.json file and so ${TM_FILENAME_BASE} = keybindings.

print variables demo

Sign up to request clarification or add additional context in comments.

2 Comments

Great, this is very helpful thanks a lot. I just tweaked the multiCommand a little bit i will add it in the description.
Of course, I temporarily forgot about the ${CLIPBOARD} variable which I am well aware of - slip of the mind, good catch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.