how to add different number at end of multi line edit? - visual-studio-code

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.

There are a few extensions that allow you to do this:
Text Pastry
Increment Selection
NumberMonger

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"
},
},

Try this one ...
its not like sublime
but works g
https://github.com/kuone314/VSCodeExtensionInsertSequence

Related

When double-clicking a number in VS Code, is it possible to have it ignore the unit? (ignore "px" in "12px")

When double-clicking a number:
100%: selects "100" (good)
100px: selects "100px" (bad)
To more rapidly change number values, it would be great if VS Code could ignore units of measure when double-clicking a number like it does with symbols.
This seems like a feature that would be part of Editor: Word Separators, but it looks like you can only add single characters to the list.
Does anyone know if it's possible to change this? Thanks!
With the extension Select By you can specify with an regular expression what you consider a "word" and with a keybinding you are faster than using the mouse in selecting.
Use the surround property, see the link.

VS Code Refactoring: Change all occurences - but only in block scope

When using "change all occurences" in VS Code, it will just search the whole file for matches and change them. Is there a similar feature doing the same thing, but limiting it to function or block scope?
Let's take an example where I would need that: I'm having a React file with several components and want to refactor a class component to a functional component, so I'm changing all occurences of this.props to props. However, I obviously don't want to change all the other class components as well that are supposed to stay class components. :-)
This seems like such a standard use case, but I'm not able to find it anywhere in VS Code. If it's not possible (yet, or for some good reasons) is there another way to achieve what I'm trying to do?
Check out the 'Add Selection To Next Find Match' functionality. It allows you to highlight the first occurrence you'd like to change, then using a keyboard shortcut, highlight the next occurrence and so on until you've selected all the instances you want to change. When all to-be-changed occurrences are selected, you can edit the selected text normally. Just remember to hit the escape key a couple times after editing to return to a single cursor!
Here are the keybindings for the command, it's Cmd+d on Mac:
https://code.visualstudio.com/docs/getstarted/keybindings
I find it very useful when renaming variables, there's also a shortcut to skip occurrences (Cmd+k Cmd+d) in case there is text you don't want to change in between.

Is it possible to type different value for multiple cursors?

When doing multi cursor selection, often you need to type a different value for each, you've to cancel selection even though all cursors are where you want them to be.
Is it possible to activate some sort of mode and press tab to automatically iterate through each one by one on every tab and type your value. Esc to cancel the mode.
Note: how in end I had to type 1, 2, 3, 4 manually. Those could have been food categories, clothing size, select options etc.
For the special case of when you want to insert values that follow a sequence at your multiple cursor locations, then the powerful Insert Nums package for Sublime Text can help.
Insert Nums can fill in numbered blanks (1,2,3,4, ... 10), and much more. Just about anything you could write as a for loop can be generated and inserted at the multiple cursors. This includes integer/float/string, count up/down, arbitrary start/stop/step values, and formatting to hex, binary, etc., representation. You can also evaluate an expression at each index, so for example you can create a geometric or power series or a pattern of bit shifts.
The default case for Insert Nums is integers, starting at 1, incrementing by 1, and ending when all selections are filled. For the example problem in the question of numbering Items, all you'd have to do is Ctrl+Alt+N, then Return. The numbers 1 through 5 would be filled in.
In case you don't want to install extension, you can copy sample data (which is generated by your favorite scripts or tools, for example) then paste it to the current cursors.
For example, I generate numbers by a cent JavaScript:
Array(10).fill(0).map((a, i) => i + 1).join('\n')
Then,
I can only answer for Sublime, and there core Sublime can't do this out of the box, but you can add the capability to do it to via a third party package.
The PowerCursors package is one way to go with this. With it installed it's as simple as Alt+[ and Alt+] to cycle between existing cursors (the binds use Ctrl instead if you're on MacOS) and type what you like, amongst other capabilities.
There may well be other packages that incorporate something similar as well, so it may be worth casting around a little bit on packagecontrol.io to see what you can dig up.
For CudaText editor, plugin "Carets Numbering" exists, which does this job. You enter starting number, etc, and it inserts increasing numbers at multi-carets positions.
Text Pastry is a vscode extension which does exactly these type of things.

Sublime Text Macro Command Move to Specific Column Number

I'm trying to move to a specific column number in a Sublime Text macro command, so that I can delete everything after that point, but I can't get the move command to work.
I've compared the old and new versions of the unofficial documentation, and it looks like the move command used to have an "amount" parameter, but it hasn't been working for me. So the reason I'm writing is because I feel like there's something the docs are leaving out, but I don't know what it is, or how to even debug it in Sublime Text.
Here's an example of what I'm trying to do:
/*//////////////////////////////////////////////////////////
// Comment Block ///////////////////////////////////////////////
//////////////////////////////////////////////////////////*/
When the caret is at the end of the phrase "Comment Block", I need to run a command that advances the caret to a specific column number. After that, I can expand the selection to the end of the line and delete, trimming the line to be equal with its counterparts.

Eclipse-RCP: Hide lines of text permanently, keep correct line numbers

I have text files which contain code inside an Editor. The user can run an analysis on a certain part of his code, which will result in a set of lines which should be hidden. Next I want to present the user with only the remaining lines, but with correct linenumbers, as from the original document. Possible solutions I thought of:
Open a new Editor which does not contain the hidden lines, but *somehow* still has correct line numbers
Hide the lines in the original editor, and offer a button for the user to 'unhide'. Probably a similar solution required as in 1.
I don't really know how to go about this. Folds would be a weird solution, because they can be unfolded individually, and seem to be more semantically tied to things like methods or classes. Also, simply creating a new document without the hidden lines results in wrong linenumbers.
Use a ProjectionViewer and reflection to invoke the private method ProjectionViewer.collapse(int offset int length). This method is only used internally to hide a certain portion of the text, by manipulating the ProjectionDocument (see http://eclipse.org/articles/Article-Folding-in-Eclipse-Text-Editors/folding.html).
After this, folding text in the editor using the annotations(the little +/- icons) WILL break everything, so this solution and regular folding are mutually exclusive.