Automatically hard wrap lines at column in VSCode
Asked Answered
B

10

283

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.

Bagdad answered 30/3, 2017 at 15:29 Comment(3)
Please check https://mcmap.net/q/110076/-what-is-the-property-name-to-break-long-lines-in-vs-codeGynecic
@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.Anabatic
To enable the visual guides (vertical lines) at column 120, edit settings.json > and add/edit"editor.rulers": [120].Forseti
O
347

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

Officialism answered 23/8, 2017 at 11:57 Comment(10)
Marking this as the accepted answer and editing to mention that it now supports auto rewrapping!Bagdad
It seems this is only for block comments? what about code lines? I tried the latest version which is 1.9.1 and block comments do get auto wrapped, however for code lines (I am using C language) it does not.Clang
Worth mentioning it works super well with MarkDown ❤Newmodel
I cannot wrap code lines with ReWrap as @YusufHusainy pointed out.Frodi
Configs (e.g. what column to wrap at) at stkb.github.io/Rewrap/#/settings-vscodeMcgovern
This is very similar to Sublime Text's built-in Alt + Q hard-wrap feature.Symposiac
Can we automate this for all already written lines?Pointed
Took me some time to do the setup, thought this would not work anymore (outdated or commands overweighed by other shortcuts?). Alt+Q did not work on an example. One way: Press Ctrl+Shift+P, write rewrap, choose rewrap.toggleAutoWrap to choose rewrap automatically for the loaded document, enter your desired column number, and if you need a Shortcut, go to the settings symbol at the right in the rewrap.toggleAutoWrap settings field. I gave it Shift+Alt+Q (example recommendation on the website). It reacts for the whole file after any insert or Enter when over the column border.Thelma
Note to clumsy Mac users like myself, that's Option+Q, not Command+Q, which will close all of your workspaces :|Neron
Whoever wrote their documentation deserves an award. It's both thorough and concise.Archon
I
99

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'
Indifferentism answered 18/8, 2017 at 19:20 Comment(7)
You say "yet". Do you know of any plans or ongoing work?Officialism
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.Indifferentism
Requested feature in the VS code issue trackerSucking
What is meant by "visual line mode"? Using Shift+v just enters a capital V into the editorSemiramis
Type it when you're in normal mode, not insert modeIndifferentism
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 questionFornof
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.Peadar
T
20

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.

Tosha answered 9/10, 2020 at 18:2 Comment(3)
That works perfectly for code. In order to make it format Markdown files as well, set proseWrap to always (default is preserve).Gona
@Max Ivanov This is exactly what I was looking for!Bifocals
2022 - it's still working, but I have to press ALT+SHIFT+F (on Windows) to re-format itPaintbrush
L
15

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.

Ling answered 9/1, 2019 at 2:2 Comment(0)
S
6

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

Sucking answered 25/8, 2017 at 14:15 Comment(2)
Unfortunately the issue is now closed and the topic locked. "We try to keep VS Code lean" :(Headmistress
Yep definitely closed.Sucking
C
4

You can do this without any extension. You just use two regex search-and-replace.

  1. Isolate the lines you want to rewrap by moving them to a separate file.

  2. Join all the lines into one line. For example, ctrl+h, "\n" ==> " ". Note: make sure regex is enabled (the dot star icon)

  3. Split the line into multiple lines. For example, ctrl+h, "(.{100}) " ==> "$1\n". Notice the space after the paren.

  4. Copy the lines back to the original file.

There are many variations on this technique. For example, you could using comma instead of space "(.{100})," ==> "$1,\n". You could use Find in Selection alt+L instead of using a temp file.

Conformal answered 11/8, 2022 at 15:26 Comment(0)
B
1

Most of these didn’t work for me, but I found the extension Vsctoix, which does.

We start out with line breaks at column 80:

Mechanisms such as a “windfall clause” help distribute riches within particular 
futures. But for a windfall clause to be useful, many conjunctive assumptions 
have to be true. We present a new method to borrow against potential future 
windfalls today, when they have greater marginal use. The method also increases 
the probability and thus the expected value of the windfalls.

Then we execute “IX: Join Lines” (no parameter):

Mechanisms such as a “windfall clause” help distribute riches within particular futures. But for a windfall clause to be useful, many conjunctive assumptions have to be true. We present a new method to borrow against potential future windfalls today, when they have greater marginal use. The method also increases the probability and thus the expected value of the windfalls.

And then “IX: Break Line At” with parameter 100:

Mechanisms such as a “windfall clause” help distribute riches within particular futures. But for a 
windfall clause to be useful, many conjunctive assumptions have to be true. We present a new method 
to borrow against potential future windfalls today, when they have greater marginal use. The method 
also increases the probability and thus the expected value of the windfalls.

It would be neat if it respected paragraph breaks and did both steps at once, but so far it’s the only extension that works for me – except I haven’t tried the vim emulation yet.

Boozer answered 22/5, 2022 at 10:34 Comment(1)
this is the only solution that worked for me as of today Feb 2023Barnes
S
0

Edit: (below answer might be for a soft wrap, see here for difference between soft and hard wrap: https://mcmap.net/q/110077/-difference-between-hard-wrap-and-soft-wrap) In my version it is Preferences -> Settings then scroll down to "Editor: Word Wrap" where a dropdown box is available from which I selected wordWrapColumn. After choosing this and closing, when I click on View now at the bottom it says Word Wrap Alt+Z.

Silicle answered 21/8, 2021 at 3:7 Comment(1)
Yet another person confusing soft and hard wrap. Read previous comments!Anabatic
K
0

If anyone is running having issues, accessibility support/screen reader may need to be disabled. Go to preferences >> text editor >> accessibility support and toggle it off.

enter image description here

Kordofan answered 9/1, 2022 at 22:22 Comment(2)
json settings example would be better, UI can change more oftenPartee
@FilipSeman is right. In VS Code 1.71.2 on Linux, the way to get there is now: File -> Preferences -> Settings -> Text Editor -> Accessibility Support. Or [Ctrl+,] -> Accessibility Support.Ballonet
S
-1

You can easily set the column limit using ColumnLimit member in C_Cpp.clang_format_fallbackStyle in settings.json (You have to install Microsoft C/C++ extension)

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: WebKit, IndentWidth: 4, ColumnLimit: 80 }",

Then you can format the file using Shift + Alt + F

There are many options you can change in this format feature

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: WebKit, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 80, AccessModifierOffset: -4 }",

Name of the predefined style used as a fallback in case clang-format is invoked with style file but the .clang-format file is not found. Possible values are Visual Studio, LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU, none, or use {key: value, ...} to set specific parameters. For example, the Visual Studio style is similar to: { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }

Before

void Proc::Memory::getSramOff(const char* mem_name, uint dataSize, uint addrBits, uint& noOfBytes, uint& sram_off)

After

void Proc::Memory::getSramOff(const char* mem_name, uint dataSize,
    uint addrBits, uint& noOfBytes, uint& sram_off)
Spelling answered 11/2, 2022 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.