1

This is so easy in vim, I just select the lines and type gq. Surely there is a simple way or extension to do the same in Visual Studio Code. To be clear, I am not looking to automatically wrap every line. I want to select a block of text, such as this example JavaScript template literal in a .js file:

div.innerHTML = `<p>Tempore voluptatem cupiditate ullam. Et eum possimus et qui placeat sit voluptatem. Aut omnis similique eum nisi molestias laborum officia. Consectetur debitis sequi sapiente. Vero incidunt perspiciatis aut ut libero tenetur saepe eaque. Hic voluptates quis quasi expedita nemo hic omnis.</p>`;

Then do something to have it wrapped to a maximum line length of say, 80 characters.

This is what I tried already. By "no changes" I mean that nothing visible happened, i.e. the long lines remained:

  1. Selected the code and with right-click, chose "Format Selection". No changes.
  2. Looked it up on two search engines, found no suitable answer yet.
  3. Installed the prettier extension. If does format a larger selection, but no changes to the long lines.
  4. Installed the Rewrap extension. After selecting the block I do ALT + Q but no changes. I also did CTRL + SHIFT + P and chose the Rewrap Comment / Text command, but no changes.
  5. Looked at a similar question but the answers there only apply to HTML.

So what is the way to do it that just works? I am happy to use vim but I quite like using VS Code as well, so would like to know this basic feature. Thank you!

Note: At the time of writing, I am on version 1.60.1 on Windows and WSL.

Nagev
  • 10,835
  • 4
  • 58
  • 69

1 Answers1

1

I'm not sure if this is the answer you want to hear but, there is an extension for vscode called vscodevim. Ctrl+Shift+X to open the extensions tab and just search for vim. This extension runs an emulation of vim on vscode and it includes the gq command since this.

So after you install this you should be all fine!

P.S I tried opening the keybind debug tool with the command palette => (Ctrl+Shift+P) => "toggle keyboard shortcuts troubleshooting" to see what happens when you press g and then q and how it translates to vscode but unsuccessfully did so.

Costa
  • 1,794
  • 1
  • 12
  • 21
  • I remember coming across this but shrugged it off probably because I'd rather use `vim` on the command line. That said, I just tried it and it does indeed work, so I've upvoted this answer. However, I'm hoping that there is a more native solution. One reason is that it takes over the keys just like `vim` and for example, `CTRL + A` no longer selects all, and I don't want to spend time configuring it. Another reason is that I was hoping that this would be done with better formatting than `vim`, for example, with appropriate indentation. Thanks for the answer. – Nagev Sep 21 '21 at 12:40
  • Personally what I do is have prettier, select what I want to format and `Ctrl+Shift+P` then "format selection" and ofc you can keybind this action. By default prettier formats everything up to Col 80 but you can set this in the .json file. However yes, it doesn't break long strings and they don't intend to change that... – Costa Sep 21 '21 at 13:04
  • >However yes, it doesn't break long strings... It's the main reason I would want a formatter extension for now, so I've uninstalled it. Thanks for clarifying that it doesn't support it, I wasn't sure if I was missing some configuration. – Nagev Sep 21 '21 at 14:09