代码之家  ›  专栏  ›  技术社区  ›  Nomura Nori

如何在visualstudio代码中增加多行选择数

  •  1
  • Nomura Nori  · 技术社区  · 3 年前

    我想为visual studio代码中的多选插入符号添加增加的数字。 现在,当我打字的时候写同样的单词。

    enter image description here

    但我想添加一些快捷键增加的数字,这样我就不需要手动更新每个数字。 首选结果应该是这样的。

    enter image description here

    我想知道这在vs代码中是否可行。

    谢谢

    1 回复  |  直到 3 年前
        1
  •  3
  •   Mark    3 年前

    您的用例不需要扩展,尽管这可能会使它变得更容易。以下是如何在没有扩展的情况下做到这一点。

    1. 查找: (?<=index:\s*)\d+ :这只选择以下数字 index:
    2. Alt + 进来 将选择所有这些数字。

    现在,您可以运行一个简单的代码片段,将这些数字替换为一个递增的数字,该数字可以是基于0的,也可以是基于1的。制作此键绑定以在 keybindings.json ):

    {
      "key": "alt+m",        // whatever keybinding you want
      "command": "editor.action.insertSnippet",
      "args": {
        "snippet": "$CURSOR_NUMBER"  // this will increment and is 1-based
      }
    }
    
    1. 触发上述密钥绑定。演示:

    demo of incrementing a digit with a snippet


    这里有一个扩展方法,使用我写的一个扩展, Find and Transform ,这就很容易了。制作此密钥绑定:

    {
      "key": "alt+m",        // whatever keybinding you want
      "command": "findInCurrentFile",
      "args": {
        "find": "(?<=index:\\s*)\\d+",   // same find regex
        "replace": "${matchNumber}",     // this variable will increase, 1-based
        "isRegex": true
      }
    }
    

    这将查找和替换结合在一个步骤中。

    increment digit with Find and Transform extension


    这里有另一种方法,所以您不需要对起点进行硬编码。

    {
      "key": "alt+m",                  // whatever keybinding you want
      "command": "findInCurrentFile",
      "args": {
        "preCommands": [
          "editor.action.addSelectionToNextFindMatch", 
          "editor.action.clipboardCopyAction"
        ],
        "find": "(?<=index:\\s*)\\d+",
        "replace": [
          "$${",
            // whatever math you want to do here
            "return Number(${CLIPBOARD}) + ${matchIndex};",
          "}$$",
        ],
        "isRegex": true,
        "postCommands": "cancelSelection"
      }
    }
    

    将光标放在要作为起点的数字旁边或选择该数字。实际上,这个数字可以在文档中的任何位置。

    sequence from selection demo

        2
  •  2
  •   rioV8    3 年前

    您可以使用扩展 Regex Text Generator

    定义以下密钥绑定

    {
        "key": "ctrl+shift+f9",  // or any other key combo
        "when": "editorTextFocus",
        "command": "regexTextGen.generateText",
        "args": {
          "generatorRegex" : "{{=i+1}}"
        }
      }
    
    • 将多光标放在 index:
    • 按组合键
    • 接受或修改输入
    • 查看预览,按 Enter 如果你喜欢, Esc 中止
        3
  •  0
  •   Hoàng Huy Khánh    3 年前