154

How can I automatically hard wrap lines in VSCode? By that I mean if a line reaches a specified column, automatically insert a newline at the word boundary closest to that column without going over. Vim has a setting called textwidth that does this that I like to use when editing Markdown. It doesn't seem like VSCode does, as far as I can tell. It just has ways to control softwrapping.

Brian Schlenker
  • 3,694
  • 4
  • 23
  • 38
  • 1
    Please check https://stackoverflow.com/a/40782188/4238189 – Mable John Dec 10 '20 at 15:16
  • 1
    @MableJohn That's about _soft_ wrap (_displaying_ long buffer lines split onto multiple screen lines), this question is about _hard_ wrap (_modifying the buffer lines_ to give a certain maximum line length). Hard wrap may affect only the line you are currently typing or may include "reflowing" or "filling" whole paragraphs so that every line is as close to the target line length as possible without overflowing. – Denis Howe Mar 21 '21 at 15:16

6 Answers6

191

VSCode doesn't support this out of the box. But you can install the Rewrap extension, which allows you to format the block that your cursor is currently in by pressing Alt + Q.

Rewrap requires no further settings, since it reads VSCode's settings to obtain the column at which to break.

Rewrap also supports automatic wrapping (off by default): https://github.com/stkb/Rewrap/wiki/Auto-wrap

msridhar
  • 2,417
  • 4
  • 19
  • 22
oli_obk
  • 22,881
  • 2
  • 66
  • 85
50

Unfortunately, VSCode doesn't have this feature yet. But, we still can make it to be as close as vim automatic word wrapping beautiful feature.


First Step

We need to setup soft word wrap feature in VSCode.

  1. Open VSCode Settings via Code => Preferences => Settings.
  2. Add these 3 lines of editor settings.

    "editor.wordWrap": "wordWrapColumn",
    "editor.wrappingIndent": "same",
    "editor.wordWrapColumn": n
    

    Don't forget to change (n) with your preferred length of columns line. For me, I feel more comfortable to set it to 60.

  3. Save this setting.

The main purpose of this first step is to make us feel more comfortable when we're typing because we don't need to manually type Enter and see a long line of text.


Second Step

We need to install Vim emulation for VSCode and set vim textwidth.

  1. Install Vim emulation via VSCode extensions.
  2. Open VSCode Settings via Code => Preferences => Settings.
  3. Add this line of vim setting.

    "vim.textwidth": n,
    

    Don't forget to change (n) with your preferred length of columns line. For me, I will set this to be the same with (n) in the first step.

  4. Save this setting.


Actual Use

When you finish to write your whole document, you can format it to be hard wrap lines using this way.

  1. Block all text using visual line mode (Shift + v)
  2. Type 'gq'
Wanda Ichsanul Isra
  • 1,614
  • 8
  • 19
  • You say "yet". Do you know of any plans or ongoing work? – oli_obk Aug 21 '17 at 09:42
  • No, I'm not sure. I have a plan to propose this feature to VSCode dev team. That's why I say "yet" because there still might be a possibility this feature being added in the future of VSCode. – Wanda Ichsanul Isra Aug 21 '17 at 10:05
  • 1
    [Requested feature in the VS code issue tracker](https://github.com/Microsoft/vscode/issues/27772) – Mark Carpenter Jr Aug 25 '17 at 14:13
  • What is meant by "visual line mode"? Using Shift+v just enters a capital V into the editor – user1081679 Apr 09 '19 at 11:35
  • Type it when you're in normal mode, not insert mode – Wanda Ichsanul Isra Apr 10 '19 at 10:04
  • You said ""Add these 3 lines of editor settings." ,... but you did not say where and how to add it ... don't assume your readers are all with plenty of knowledge of VS code ... if so they won't be asking this type of question – Ben Oct 07 '20 at 05:14
  • This answer is out of date. Please see the answer of @Arun Kumar Khattri below and give it a upvote, so that i can replace this answere here. – benni Nov 04 '20 at 16:27
21

Now VSCode support auto wrapping out of the box.

Settings --> Text Editor --> Last 3 options (as on today) is for autowrapping.

  1. Word Wrap (Controls how lines should wrap)
  2. Word Wrap Column (Controls the wrapping column of the editor)
  3. Wrapping indent (Controls the indentation of wrapped lines)

By default Word Wrap is off.

Arun Kumar Khattri
  • 1,124
  • 10
  • 17
  • 47
    This is for "soft" wrapping, aka, wrapping when it's displayed in the editor, but not wrapping your actual text. – forivall Jul 17 '19 at 19:55
  • since the word "**hard**" wrap is mentioned both in the question title and the question description, I am going to downvote this answer. – Aidin May 26 '21 at 22:39
6

Hard Wrap Comments

Use the Rewrap extension.

Soft Wrap Code

Add the following setting (replace column width with your preference): "editor.wordWrapColumn": 100

Then add either "editor.wordWrap": "wordWrapColumn" (wraps at the column) or "editor.wordWrap": "bounded" (wraps at either the column or the viewport).

Hard Wrap Comments and Soft Wrap Code

Unfortunately the extension and VSCode settings do not play nicely.

Feel free to upvote this feature request.

jabacchetta
  • 27,580
  • 6
  • 49
  • 65
4

As of 2020 and if you're using the Prettier - Code formatter plugin:

Go to Plugins -> Find Prettier -> Cog -> Extension Settings -> Prettier: Print Width Fit code within this line limit and set to whatever you want. By default it's 80.

When you save the file, Prettier will format automatically.

ACV
  • 8,090
  • 4
  • 56
  • 72
  • 2
    That works perfectly for code. In order to make it format Markdown files as well, set `proseWrap` to `always` (default is `preserve`). – Max Ivanov Oct 23 '20 at 01:10
3

There is currently an Open request for this in the VS Code Issue tracker on GitHub, You Can Find It Here

Mark Carpenter Jr
  • 595
  • 1
  • 11
  • 26