Having trouble finding a way to do this, maybe it is not even possible?
In my case, for testing flow of if-statements/user-interaction, am temporarily adding 40 lines of console.log('trigger-fired-1');
throughout our code.
However, to tell them apart would like each to end with a different number, so in this case, numbers one to forty like so:
In the screen recorded gif, to replicate what I am going for, all I did was copy/paste the numbers one to nine. What I really would like is a shortcut key to generate those numbers at the end for me to eliminate that step of typing out each unique number.
Am primarily coding in Visual Studio Code or Sublime Text, and in some cases shortcuts are similar, or at least have same support but for different shortcut keys.
Select the lines you want and then press: Windows: Shift + Alt + i. Mac: shift + option + i.
A common way to add more cursors is with Shift+Alt+Down or Shift+Alt+Up that insert cursors below or above. Note: Your graphics card driver (for example NVIDIA) might overwrite these default shortcuts. Ctrl+D selects the word at the cursor, or the next occurrence of the current selection.
Insert cursor at the beginning of every selected line: Mac: Cmd + A to select all lines. Windows: Ctrl + A to select all lines. Alt/Option + Shift + I to insert cursor at the end of every line.
There are a few extensions that allow you to do this:
For Sublime Text, the solution to this problem is the internal Arithmetic
command. Something similar may or may not be available in VS Code (possibly with an extension of some sort) but I'm not familiar enough with it to say for sure.
This command allows you to provide an expression of some sort to apply to all of the cursor locations and/or selected text.
By way of demonstration, here's the example you outlined above:
The expression you provide is evaluated once for every selection/caret in the buffer at the time, and the result of the expression is inserted into the buffer (or in the case of selected text, it replaces the selection). Note also that when you invoke this command from the input panel (as in the screen recording) the panel shows you a preview of what the expression output is going to be.
The special variable i
references the selection number; selections are numbered starting at 0
, so the expression i + 1
has the effect of inserting the selection numbers starting at 1 instead of 0.
The special variable x
refers to the text in a particular selection instead. That allows you to select some text and then transform it based on your expression. An example would be to use x * 2
immediately after the above example (make sure all of the selections are still present and wrapping the numbers) to double everything.
You can use both variables at once if you like, as well as anything in the Python math
library, for example math.sqrt(i)
if you want some really esoteric logs.
The example above shows the command being selected from the command palette interactively, where the expression automatically defaults to the one that you want for your example (i + 1
).
If you want to have this as a key binding, you can bind a key to the arithmetic
command and provide the expression directly. For example:
{
"keys": ["super+a"],
"command": "arithmetic",
"args": {
"expr": "i+1"
},
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With