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.
-
1Please 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 Answers
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
-
3Marking this as the accepted answer and editing to mention that it now supports auto rewrapping! – Brian Schlenker Feb 15 '18 at 19:34
-
3It 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. – Yusuf Husainy Nov 13 '18 at 17:28
-
3
-
3I cannot wrap code lines with ReWrap as @YusufHusainy pointed out. – Fawwaz Yusran May 09 '19 at 03:58
-
Configs (e.g. what column to wrap at) at https://stkb.github.io/Rewrap/#/settings-vscode – Aidin May 26 '21 at 22:43
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.
- Open VSCode Settings via
Code => Preferences => Settings
. 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.
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
.
- Install Vim emulation via VSCode extensions.
- Open VSCode Settings via
Code => Preferences => Settings
. 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.
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.
- Block all text using visual line mode (Shift + v)
- Type 'gq'
![](../../users/profiles/7152771.webp)
- 1,614
- 8
- 19
-
-
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
-
-
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
Now VSCode support auto wrapping out of the box.
Settings --> Text Editor --> Last 3 options (as on today) is for autowrapping.
- Word Wrap (Controls how lines should wrap)
- Word Wrap Column (Controls the wrapping column of the editor)
- Wrapping indent (Controls the indentation of wrapped lines)
By default Word Wrap is off.
![](../../users/profiles/4858908.webp)
- 1,124
- 10
- 17
-
47This 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
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.
![](../../users/profiles/4500152.webp)
- 27,580
- 6
- 49
- 65
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.
![](../../users/profiles/912829.webp)
- 8,090
- 4
- 56
- 72
-
2That 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
There is currently an Open request for this in the VS Code Issue tracker on GitHub, You Can Find It Here
![](../../users/profiles/5063323.webp)
- 595
- 1
- 11
- 26
-
Unfortunately the issue is now closed and the topic locked. "We try to keep VS Code lean" :( – rbrtl Sep 15 '20 at 23:46
-