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

耗尽温柔 提交于 2020-01-05 04:37:07

问题


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.


回答1:


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



回答2:


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

  • Text Pastry
  • Increment Selection
  • NumberMonger


来源:https://stackoverflow.com/questions/54409129/how-to-add-different-number-at-end-of-multi-line-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!