Automatically hard wrap lines at column in VSCode

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.


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


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'