ChatGPT解决这个技术问题 Extra ChatGPT

How can I switch word wrap on and off in Visual Studio Code?

When using code files, you typically don't need longer lines to wrap around. However, with .md files this is in fact rather useful. However, I can't seem to find the option to enable word wrap so longer lines will be wrapped.

To reproduce, open Visual Studio Code resized to a small-enough window, and enter the following text in a new document:

This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum. This is my test lorem ipsum.
A linebreak before this. 

The effect is this:

https://i.stack.imgur.com/mgitS.png

I'm trying to get the horizontal scrollbar to stay away, having line 1 wrap around at the right side of the window.

I've done a few things to answer my own question:

Search Stack Overflow: zero results at the time of writing this;

Meticulously going through the menu of Visual Studio Code: didn't find it;

Using the Command Palette with "wrap": gives no matching commands.

Perhaps it's not possible, and I'd need to file a feature request? Or am I missing something?

Note that I'd like to be able to turn it on and off quickly. For one, @PanagiotisKanavos mentioned in comments this solution to change wrapping behavior in the settings, but I'm looking for a quick command or menu option to do this (much like Notepad++ and Sublime Text 2 have).

I think this was just added in VS Code 0.3. Check the list of new features for wrapping control
This question from another StackExchange site shows that you can simply add "editor.wrappingColumn": 0 in your user settings
@PanagiotisKanavos Thanks for the suggestion. That would be somewhat of a workaround, because word wrap is typically something I want to switch on/off quickly. I've updated my question accordingly. It may still be useful to turn your comment into an answer though, for others that land here who may be fine with that workaround.
Its now "editor.wordWrap": true

P
Peter Mortensen

Since v1.0 you can toggle word wrap:

with the new command editor.action.toggleWordWrap, or

from the View menu (*View** → Toggle Word Wrap), or

using the ALT+Z keyboard shortcut (for Mac: ⌥+Z).

It can also be controlled with the following settings:

editor.wordWrap

editor.wordWrapColumn

editor.wrappingIndent

Known issues:

renderLineHighlight should highlight the entire logical line

If you'd like these bugs fixed, please vote for them.


Completely agree that this should be a thing we can manually enable/disable on a per-window basis. Right now I'm going though a .txt file with a bunch of log entries, and having word-wrap enabled makes it challenging to read. Usually I don't mind it, but in this instance would like it disabled.
Just implement it as it is currently in Sublime. That's perfect!
@Benjamin, what about 'wrap to window'? That's something I personally use a lot in certain code files (like MarkDown as he said). Personally, I'd love to see three options accessed quickly: Off (No wrapping), Window (wraps to viewable extents) and Specified Column (which would pick up the value in the preferences.) Then be able to assign keyboard shortcuts to flip between those three.
This is no acceptable solution if you need to set "editor.wrappingColumn": -1 by default. Use case is: I work a lot with code files. Only in some case (*.md files) I must toggle word wrap but cannot!
For that you can leverage our language specific settings introduced in 1.9 (see code.visualstudio.com/updates/v1_9#_settings)
P
Peter Mortensen

Go to menu File → Preferences → User Settings.

It will open up Default Settings and settings.json automatically. Just add the following in the settings.json file and save it. This will overwrite the default settings.

// Place your settings in this file to overwrite the default settings
{ "editor.wrappingColumn": 0 }

https://i.stack.imgur.com/ouXmw.png


with latest version its, "editor.wordWrap": "on" is setting above one wont work.
Yes, this setting was changed in VSCode 1.10. Here's the documentation: code.visualstudio.com/updates/…
Open settings.json for me is easiest F1 > backspace key > enter settings.json > find the one in .config
P
Pixelomo

wrappingColumn has been deprecated in favour of wordWrap.

Add this line to settings.json to set wordWrap on by default:

"editor.wordWrap": "on" 

or open user settings:

Mac: ⌘ + ,

Windows: Ctrl + ,

Then search for "wordWrap" or scroll through the 'Commonly Used' settings to find it and select 'on'

https://i.stack.imgur.com/t76yJ.png


Thanks for the addition, but don't the top answer and this other earlier answer also mention exactly that already?
not exactly, they reference wordWrap but I thought my addition would be useful for anyone looking for a simple piece of code to just copy and paste
P
Peter Mortensen

Since version 0.3.0, wrapping has been put in the command palette. You can activate it with Toggle Word Wrap or Alt + Z.


Unfortunately this does not seem to get remembered and has to be applied again every time.
P
Peter Mortensen

Check out this screenshot (Toogle Word Wrap):

https://i.stack.imgur.com/c0kn0.png


Now the answer is correct, but duplicates the info from the top answer. Thx for taking the time to help others, but there's probably other questions where new answers are more badly needed.
@Jeroen Yes, but it will be great, if answers can get some appreciation too. I can see in this question itself, even duplicates have got upvotes.
P
Peter Mortensen

If you want to use text word wrap in your Visual Studio Code editor, you have to press button Alt + Z for text word wrap. Its word wrap is toggled between text wrap or unwrap.


This is covered by the highest voting, accepted answer.
P
Peter Mortensen

Go to the Preferences tab (menu File → Settings), and then search as “word wrap”. The following animated image is helpful too.

https://i.stack.imgur.com/Hf25r.gif


P
Peter Mortensen

Since 1.9, it's possible to select a specific language for word wrap settings (or any settings). You can find this in the command palette under:

Preferences: Configure Language Specific Settings...

Which will take you to your "settings.json" for a selected language where you might include:

"[markdown]": {
  "editor.wordWrapColumn": 100,
  "editor.wordWrap": "wordWrapColumn"
},

How do you get to "Configure Language Specific Settings"?
One route is from the menu View => Command Palette... You can then type "Configure Language Specific Settings..." (or just "Lang set"). The Command Palette is a really good way of finding anything in vscode, it's worth learning the shortcut.
S
Sabito 錆兎 stands with Ukraine

Here you go with word-wrap on Visual Studio Code.

https://i.stack.imgur.com/9AEGO.png


P
Peter Mortensen

I am not sure when it was added, but I'm using v0.10.8 and Alt + Z is the keyboard shortcut for turning word wrap on and off. This satisfies the requirement of "able to turn it on and off quickly".

The setting does not persist after closing Visual Studio Code. To persist, you need to set it through Radha's answer of using the settings.json file...

// Place your settings in this file to overwrite the default settings
{ "editor.wrappingColumn": 0 }

I had to use { "editor.wrappingColumn": -1 } to make it the default.
editor.wrappingColumn has been deprecated, use editor.wordWrap instead.
Z
Zoltan Toth

Word wrap settings redesign

Here are the new word wrap options:

editor.wordWrap: "off" - Lines will never wrap.
editor.wordWrap: "on" - Lines will wrap at viewport width.
editor.wordWrap: "wordWrapColumn" - Lines will wrap at the value of editor.wordWrapColumn.
editor.wordWrap: "bounded" 

Lines will wrap at the minimum of viewport width and the value of editor.wordWrapColumn.


Thx for the answer. But this info was also already in the top-voted answer. I suggest adding the short bits of explanation for the new options to that answer as an edit.
Was looking some clarity regarding the meaning of the different options. I played with the values and it was basically between boundend and on, in the end I decided to go with on as if you have more space in your window it will not wrap, while bounded wraps to editor.wordWrapColumn (default is 80) even if there is more space in the window so for me personally on the most sensible but it's personal of course. Hope it helps :)
W
WurmD

press ctrl+ shift + p Preferences open keyboard shortcut search Toggle Wrap word Set your preferences toggle wrap word

NOTES:

Works for version 1.55.2

Default one is alt+z


for the latest version
You can edit your answer if you wished to add "for the latest version" :)
that default note alt + z was really helpful!
R
Ricardo stands with Ukraine

Explained here Language-specific editor settings but specifically:

Ctrl+Shift+P and type "Preferences: Configure Language Specific Settings" Select the language or add section in the file (start typing "[" to see list of suggestions) or edit section as you like if already there. If set it to bounded you might need to adjust the editor.wordWrapColumn value to wrap depending on the screen size. With bounded Lines will wrap at the minimum of viewport and editor.wordWrapColumn

Example:


    "editor.wordWrapColumn": 200,
    "[markdown]": {
        "editor.wordWrap": "on",
    },
    "[plaintext]": {
        "editor.wordWrap": "bounded",
    },
    "[typescript]": {
        "editor.tabSize": 2,
        "editor.wordWrap": "off",
    },

Should be the accepted answer for users who want to permanently turn it off. Turning it off in preferences has no effect if it is set to on in the language preference.
T
Tim Skov Jacobsen

This is from the VS Code docs as of May 2020:

Here are the new word wrap options: editor.wordWrap: "off" - Lines will never wrap. editor.wordWrap: "on" - Lines will wrap at viewport width. editor.wordWrap: "wordWrapColumn" - Lines will wrap at the value of editor.wordWrapColumn. editor.wordWrap: "bounded" - Lines will wrap at the minimum of viewport width and the value of editor.wordWrapColumn.

So for example, if you want to have the lines wrapped at the boundary of the window, you should:

Open settings.json (Hit CTRL+SHIFT+P and type "settings.json") Put "editor.wordWrap": "bounded" in the json file, like this: { ... , "editor.wordWrap": "bounded", ... , }

and then it should work.


R
Royer Adames

If it's not working in mac,

make sure to tell VScode that you are not using a screen reader. I had word wrap on and restarted VScode, and it gave me a notification window saying that if I'm in a screenreader, yes or no, and to note that word-wrap does not work in screen readers.


This is what work for me
In my case it was due to grammarly app
B
Bahman.A

Windows: Ctrl + Shift + press the key "P". Now on the command line, type Toggle Word Wrap and press Enter.

Mac: Command + Shift + press the key "P". Now in the command line, type Toggle Word Wrap and press Enter.


A
Adam Smaka

For Dart check "Line length" property in Settings.


T
Tomerikoo

Accessibility support is on by default and it will override your selected wrapper behavior. So disable Accessibility Support first.

Then choose "on" for the Word Wrap option. You don't need to go into settings.json to enable word wrap.

Picture of the accessibility support option


D
Daniel Danielecki

Mac: Code -> Preferences -> Settings -> Type wordwrap in Search settings -> Change Editor: Word Wrap from off to on.

Windows: File -> Preferences -> Settings -> Type wordwrap in Search settings -> Change Editor: Word Wrap from off to on.


P
Peter Mortensen

If you want a permanent solution for wordwrapping lines, go to menu FilePreferenceSettings and change editor.wordWrap: "on". This will apply always.

However, we usually keep changing our preference to check code. So, I use the Alt + Z key to wrap written code of a file or you can go to menu View → Toggle Word Wrap. This applies whenever you want not always. And again Alt + Z to undo wordwrap (will show the full line in one line).


Can you fix it?
r
rmb

In version 1.52 and above go to File > Preferences > Settings > Text Editor > Diff Editor and change Word Wrap parameter as you wish


A
Adam Erickson

The language-specific example by @Riga is great. For a general setting, I would recommend the following:

"editor.wordWrap": "bounded",
"editor.wordWrapColumn": 90,
"editor.wrappingIndent": "same",

This wraps text if your viewport is smaller than the column limit (90 here) and uses the same indent when wrapping.


C
Carlos Cuellar

Step 1: Access to Dart extension settings

https://i.stack.imgur.com/m7XXj.png

https://i.stack.imgur.com/e9llV.png

https://i.stack.imgur.com/54HCL.png


Isn't that formatting instead of wrapping though?
Formatting (ALT+ SHIFT + F) uses the wrapping configurations.
Ah I see what you mean. I was thrown off because my original question (and most answers) was about "Soft Wrapping" to prevent scrolling, not so much hard wrapping (in your screenshot there's still a horizontal scroll bar, I was trying to prevent exactly that when writing the question back then).
m
mk22

Click on Settings in VS Code editor Search for wordwrap Select "on" for the Editor Word Wrap as shown in screenshot below

https://i.stack.imgur.com/oOpSz.png