How to increment a variable, like the line number, in a vscode snippet
I have a multi-line snippet in VScode. The problem is, TM_LINE_NUMBER gives the number of the line where the snippet was triggered, I need to increase that number by 1 so it equals the line number that it is actually on.
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER');"
]
,
How can I do that?
visual-studio-code
add a comment |
I have a multi-line snippet in VScode. The problem is, TM_LINE_NUMBER gives the number of the line where the snippet was triggered, I need to increase that number by 1 so it equals the line number that it is actually on.
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER');"
]
,
How can I do that?
visual-studio-code
add a comment |
I have a multi-line snippet in VScode. The problem is, TM_LINE_NUMBER gives the number of the line where the snippet was triggered, I need to increase that number by 1 so it equals the line number that it is actually on.
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER');"
]
,
How can I do that?
visual-studio-code
I have a multi-line snippet in VScode. The problem is, TM_LINE_NUMBER gives the number of the line where the snippet was triggered, I need to increase that number by 1 so it equals the line number that it is actually on.
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER');"
]
,
How can I do that?
visual-studio-code
visual-studio-code
edited Nov 27 '18 at 5:03
Mark
13.4k33857
13.4k33857
asked Nov 14 '18 at 17:15
AsafAsaf
4,060124798
4,060124798
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There are at least these two options:
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$1:$TM_LINE_NUMBER');"
// "console.log('Line #$TM_LINE_NUMBER');"
]
With the above at least the line number is selected and you could easily change it yourself.
More interesting is to make this into a "macro" which will accomplish exactly what you want.
You will need something like the multi-command extension.
Change above snippet to :
"Console_Log_Test":
"prefix": "clg",
"body": [
"console.log('Line #$TM_LINE_NUMBER');"
]
so now the snippet only prints the line with the TM_LINE_NUMBER
in it.
In your User Settings:
"multiCommand.commands": [
"command": "multiCommand.lineNumber",
"sequence": [
"command": "type",
"args":
"text": "//Debugging (remove)n"
,
"command": "editor.action.insertSnippet",
"args":
// "langId": "csharp",
"name": "Console_Log_Test"
]
Now the snippet is actually triggered on the line number you want.
In your keybindings.json:
"key": "ctrl+alt+l",
"command": "multiCommand.lineNumber"
,
Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.
[EDIT]
I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.
Using this snippet:
"log line number on second line":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER"
]
,
That snippet has everything but the final ');
Now this macro:
"command": "multiCommand.lineNumber",
"sequence": [
"command": "editor.action.insertSnippet",
"args":
"name": "log line number on second line"
,
"editor.emmet.action.incrementNumberByOne",
"command": "type",
"args":
"text": "');n"
]
,
will work!! The line number will be incremented by one and then ');n
will be added to the end of that line.
And you can do fancier math using "editor.emmet.action.evaluateMathExpression"
in place of the incrementNumberByOne
command.
To add 10 to the line numbers, use
"console.log('Line #$TM_LINE_NUMBER+11"
in the snippet and "editor.emmet.action.evaluateMathExpression"
in place of "editor.emmet.action.incrementNumberByOne"
in the multicommand macro.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305549%2fhow-to-increment-a-variable-like-the-line-number-in-a-vscode-snippet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are at least these two options:
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$1:$TM_LINE_NUMBER');"
// "console.log('Line #$TM_LINE_NUMBER');"
]
With the above at least the line number is selected and you could easily change it yourself.
More interesting is to make this into a "macro" which will accomplish exactly what you want.
You will need something like the multi-command extension.
Change above snippet to :
"Console_Log_Test":
"prefix": "clg",
"body": [
"console.log('Line #$TM_LINE_NUMBER');"
]
so now the snippet only prints the line with the TM_LINE_NUMBER
in it.
In your User Settings:
"multiCommand.commands": [
"command": "multiCommand.lineNumber",
"sequence": [
"command": "type",
"args":
"text": "//Debugging (remove)n"
,
"command": "editor.action.insertSnippet",
"args":
// "langId": "csharp",
"name": "Console_Log_Test"
]
Now the snippet is actually triggered on the line number you want.
In your keybindings.json:
"key": "ctrl+alt+l",
"command": "multiCommand.lineNumber"
,
Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.
[EDIT]
I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.
Using this snippet:
"log line number on second line":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER"
]
,
That snippet has everything but the final ');
Now this macro:
"command": "multiCommand.lineNumber",
"sequence": [
"command": "editor.action.insertSnippet",
"args":
"name": "log line number on second line"
,
"editor.emmet.action.incrementNumberByOne",
"command": "type",
"args":
"text": "');n"
]
,
will work!! The line number will be incremented by one and then ');n
will be added to the end of that line.
And you can do fancier math using "editor.emmet.action.evaluateMathExpression"
in place of the incrementNumberByOne
command.
To add 10 to the line numbers, use
"console.log('Line #$TM_LINE_NUMBER+11"
in the snippet and "editor.emmet.action.evaluateMathExpression"
in place of "editor.emmet.action.incrementNumberByOne"
in the multicommand macro.
add a comment |
There are at least these two options:
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$1:$TM_LINE_NUMBER');"
// "console.log('Line #$TM_LINE_NUMBER');"
]
With the above at least the line number is selected and you could easily change it yourself.
More interesting is to make this into a "macro" which will accomplish exactly what you want.
You will need something like the multi-command extension.
Change above snippet to :
"Console_Log_Test":
"prefix": "clg",
"body": [
"console.log('Line #$TM_LINE_NUMBER');"
]
so now the snippet only prints the line with the TM_LINE_NUMBER
in it.
In your User Settings:
"multiCommand.commands": [
"command": "multiCommand.lineNumber",
"sequence": [
"command": "type",
"args":
"text": "//Debugging (remove)n"
,
"command": "editor.action.insertSnippet",
"args":
// "langId": "csharp",
"name": "Console_Log_Test"
]
Now the snippet is actually triggered on the line number you want.
In your keybindings.json:
"key": "ctrl+alt+l",
"command": "multiCommand.lineNumber"
,
Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.
[EDIT]
I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.
Using this snippet:
"log line number on second line":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER"
]
,
That snippet has everything but the final ');
Now this macro:
"command": "multiCommand.lineNumber",
"sequence": [
"command": "editor.action.insertSnippet",
"args":
"name": "log line number on second line"
,
"editor.emmet.action.incrementNumberByOne",
"command": "type",
"args":
"text": "');n"
]
,
will work!! The line number will be incremented by one and then ');n
will be added to the end of that line.
And you can do fancier math using "editor.emmet.action.evaluateMathExpression"
in place of the incrementNumberByOne
command.
To add 10 to the line numbers, use
"console.log('Line #$TM_LINE_NUMBER+11"
in the snippet and "editor.emmet.action.evaluateMathExpression"
in place of "editor.emmet.action.incrementNumberByOne"
in the multicommand macro.
add a comment |
There are at least these two options:
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$1:$TM_LINE_NUMBER');"
// "console.log('Line #$TM_LINE_NUMBER');"
]
With the above at least the line number is selected and you could easily change it yourself.
More interesting is to make this into a "macro" which will accomplish exactly what you want.
You will need something like the multi-command extension.
Change above snippet to :
"Console_Log_Test":
"prefix": "clg",
"body": [
"console.log('Line #$TM_LINE_NUMBER');"
]
so now the snippet only prints the line with the TM_LINE_NUMBER
in it.
In your User Settings:
"multiCommand.commands": [
"command": "multiCommand.lineNumber",
"sequence": [
"command": "type",
"args":
"text": "//Debugging (remove)n"
,
"command": "editor.action.insertSnippet",
"args":
// "langId": "csharp",
"name": "Console_Log_Test"
]
Now the snippet is actually triggered on the line number you want.
In your keybindings.json:
"key": "ctrl+alt+l",
"command": "multiCommand.lineNumber"
,
Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.
[EDIT]
I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.
Using this snippet:
"log line number on second line":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER"
]
,
That snippet has everything but the final ');
Now this macro:
"command": "multiCommand.lineNumber",
"sequence": [
"command": "editor.action.insertSnippet",
"args":
"name": "log line number on second line"
,
"editor.emmet.action.incrementNumberByOne",
"command": "type",
"args":
"text": "');n"
]
,
will work!! The line number will be incremented by one and then ');n
will be added to the end of that line.
And you can do fancier math using "editor.emmet.action.evaluateMathExpression"
in place of the incrementNumberByOne
command.
To add 10 to the line numbers, use
"console.log('Line #$TM_LINE_NUMBER+11"
in the snippet and "editor.emmet.action.evaluateMathExpression"
in place of "editor.emmet.action.incrementNumberByOne"
in the multicommand macro.
There are at least these two options:
"Console_Log_Test":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$1:$TM_LINE_NUMBER');"
// "console.log('Line #$TM_LINE_NUMBER');"
]
With the above at least the line number is selected and you could easily change it yourself.
More interesting is to make this into a "macro" which will accomplish exactly what you want.
You will need something like the multi-command extension.
Change above snippet to :
"Console_Log_Test":
"prefix": "clg",
"body": [
"console.log('Line #$TM_LINE_NUMBER');"
]
so now the snippet only prints the line with the TM_LINE_NUMBER
in it.
In your User Settings:
"multiCommand.commands": [
"command": "multiCommand.lineNumber",
"sequence": [
"command": "type",
"args":
"text": "//Debugging (remove)n"
,
"command": "editor.action.insertSnippet",
"args":
// "langId": "csharp",
"name": "Console_Log_Test"
]
Now the snippet is actually triggered on the line number you want.
In your keybindings.json:
"key": "ctrl+alt+l",
"command": "multiCommand.lineNumber"
,
Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.
[EDIT]
I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.
Using this snippet:
"log line number on second line":
"prefix": "clg",
"body": [
"//Debugging (remove)",
"console.log('Line #$TM_LINE_NUMBER"
]
,
That snippet has everything but the final ');
Now this macro:
"command": "multiCommand.lineNumber",
"sequence": [
"command": "editor.action.insertSnippet",
"args":
"name": "log line number on second line"
,
"editor.emmet.action.incrementNumberByOne",
"command": "type",
"args":
"text": "');n"
]
,
will work!! The line number will be incremented by one and then ');n
will be added to the end of that line.
And you can do fancier math using "editor.emmet.action.evaluateMathExpression"
in place of the incrementNumberByOne
command.
To add 10 to the line numbers, use
"console.log('Line #$TM_LINE_NUMBER+11"
in the snippet and "editor.emmet.action.evaluateMathExpression"
in place of "editor.emmet.action.incrementNumberByOne"
in the multicommand macro.
edited Nov 27 '18 at 4:57
answered Nov 15 '18 at 17:18
MarkMark
13.4k33857
13.4k33857
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305549%2fhow-to-increment-a-variable-like-the-line-number-in-a-vscode-snippet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown