Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add different number at end of multi line edit?

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:

enter image description here

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.

like image 685
allenski Avatar asked Jan 28 '19 19:01

allenski


People also ask

How do you edit multiple lines at the same time in vs code?

Select the lines you want and then press: Windows: Shift + Alt + i. Mac: shift + option + i.

How do I add multiple cursors?

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.

How do I add to the end of every line Vscode?

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.


2 Answers

There are a few extensions that allow you to do this:

  • Text Pastry
  • Increment Selection
  • NumberMonger
like image 175
jabacchetta Avatar answered Oct 10 '22 21:10

jabacchetta


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:

Sample Arithmetic Example

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"
    },
},
like image 35
OdatNurd Avatar answered Oct 10 '22 23:10

OdatNurd