Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Vim word wrap options vimrc

Tags:

vim

I'm trying to configure Vim so that it always wraps at x-number of columns automatically, at all times such as in this other editor that I provided a screenshot of-

like image 203
Shane Thornton Avatar asked Jun 18 '13 08:06

Shane Thornton


People also ask

How do I turn on word wrap in Vim?

Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap. So using either: :set wrap!

How do I change Vim default settings?

1. Enable the options for an individual file inside the Vim session using :set Open the desired file in Vim, type any option using the :set command in the Normal mode, and press Enter. 2. Enable the options permanently for all the files by specifying them in the local Vim configuration file ~/.

How do I edit vimrc?

Opening vimrc Using file name completion, you could type :e $M then press Tab until you see the desired variable. If you only want to see the path, type :echo $M then press Tab to see the variable, and press Enter. In gvim, the Edit menu includes "Startup Settings" which will use $MYVIMRC to edit your vimrc file.


2 Answers

Use :set wrap and :set textwidth=N . For permanent effect add the wrap settings to the vim start-up file .vimrc

set wrap
set textwidth=X

To apply the wrapping to an existing file, move to the start of the file (you can use gg to do this). Then type gqG to apply the re-formatting to the entire file.

Also setting a margin may be useful:

set wrapmargin=0
like image 136
suspectus Avatar answered Oct 21 '22 16:10

suspectus


If you want hard wrapping, i.e. real reformatting of the text by inserting newline characters, the 'textwidth' setting (as outlined in suspectus's answer) is the key. You need to reformat existing text, e.g. via the gq command.

For soft wrapping, i.e. where long lines just appear to be broken in the editor window, :set wrap will always fill the entire available window space; you cannot restrict that to take less. Either resize the Vim window, or add a padding buffer to the right that restricts the available space to the current window. For the latter, my LimitWindowSize plugin may be helpful.

like image 33
Ingo Karkat Avatar answered Oct 21 '22 16:10

Ingo Karkat