将选定的行填充到VSCode中的光标位置 [英] Pad selected lines to cursor position in VSCode

查看:330
本文介绍了将选定的行填充到VSCode中的光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2019中,我可以使用 Alt + Shift + (箭头键)来多行编辑``虚拟''空白然后按任意键以使所有行都用空格填充到所选列,我经常使用它来使初始化代码更易于阅读。但是,当我切换到Visual Studio Code时,找不到对应的代码。我能找到的最接近的东西是 Ctrl + Alt + (箭头键)。这并不是我所需要的,因为它仅将每个行光标放在最后一个示例的末尾,而不是在上一个示例中的虚拟空格。



这里是Visual中的可视示例Studio 2019(我不知道如何制作GIF):









VSCode中是否有任何等效项,或者我暂时没有使用它吗?

解决方案

您可以使用宏很好地做到这一点。使用






首先选择要填充的所有行,而不是触发您的绑定。然后,至少所有光标都与填充对齐,并且很容易一次与所有光标向左或向右移动。



您可能可以减小<$我只是出于演示目的使用c $ c> 30 取决于您最长的电话通常多久。


In Visual Studio 2019, I can use Alt+Shift+(Arrow Keys) to multi-line edit the "virtual" whitespace then press any key to make all the lines padded with space to the selected column, I use this a lot to make initialization code easier to read. However, when I switched to Visual Studio Code, I could not find the equivalent. The closest thing I was able to find was Ctrl+Alt+(Arrow Keys). This is not quite what I need since it only places each line cursor at the end instead of the "virtual" whitespace in the previous example.

Here's a visual example in Visual Studio 2019 (I don't know how to make GIFs):

Is there any equivalent in VSCode or am I stuck without it for now?

解决方案

You can do this fairly well with a macro. Using a macro extension like multi-command put this into your settings.json:

"multiCommand.commands": [

  {
    "command": "multiCommand.padTrailingSpaces",
    "sequence": [
      "editor.action.insertCursorAtEndOfEachLineSelected",
       "cursorHomeSelect",
      {
        "command": "editor.action.insertSnippet,

                       // pad end of each line with lots of spaces's'
        "args": {
          "snippet": "$TM_SELECTED_TEXT                                              ",
        }
      },

      "cursorHomeSelect",
      {
        "command": "editor.action.insertSnippet",  
        "args": {

          // keep first 30 characters, increase if you typically need more
          "snippet": "${TM_SELECTED_TEXT/(.{30}).*/$1/g}",  
        }
      }
    ]
  }
]

The above puts a cursor at the end of each line, adds way more spaces than you should ever need, and then keeps only the first 30 characters on each line.

Choose some keybinding (in keybindings.json):

{
  "key": "alt+s",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.padTrailingSpaces" },
  "when": "editorTextFocus" 
},


First select all the lines you want padded, than trigger your keybinding. Then al least you have all the cursors lined up with padding and it is easy to go left or right with all of them at once.

You probably can reduce the 30 that I use just for demonstration purposes by a some - depends on how long your longest line usually is.

这篇关于将选定的行填充到VSCode中的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆