Learn Practice Daily golf Reference

How to turn word wrap on or off in Vim

Type :set nowrap and press ↵ to keep each long line on one screen row, and :set wrap to wrap them at the window's edge again. That only changes the view. For real line breaks, type :set textwidth=72, then gqip in the paragraph.

The ninja folding one endless paper ribbon of text into neat, even rows against a wooden ruler marked 72, a gq keycap on the counter beside it.

The burrito shop's specials.js opens with a comment about the Friday special that runs past 80 characters, and your terminal is half the screen wide because the menu is open beside it. Vim carries the comment on to a second row at the window's edge, often in the middle of a word. That's the wrap option, on out of the box, and it changes only what you see. In the file, the comment is still one line.

:set nowrap keeps every line on a single row and cuts it off at the edge. Move the cursor along the line and the view slides sideways to follow it. :set wrap turns wrapping back on, and :set wrap! switches to whichever one is off.

Practise the keys

Wrap on and off

Type each command as you would in Vim, Enter included.

Which keys do it?
Long lines run on to extra rows. Keep each one on a single row.
scroll here, then press it
1 of 3
Long lines run on to extra rows. Keep each one on a single row.

Wrap between words, and move one row at a time

Vim wraps a line at the last character that fits, so a word can start on one row and finish on the next. :set linebreak makes it wrap at a space or a punctuation mark instead. Like wrap, it only changes the screen, and it does nothing while wrap is off.

A wrapped line is still one line, so j (down) and k (up) jump over all of its rows at once. In a README where every paragraph is one long line, j skips a whole paragraph. gj and gk move down and up one row of the screen instead.

Practise the keys

Prose that wraps

Which keys do it?
Long lines wrap in the middle of words. Make them wrap between words.
scroll here, then press it
1 of 3
Long lines wrap in the middle of words. Make them wrap between words.

Break the text into real lines

Wrapping on the screen is no help when the breaks have to be in the file. A commit message is the usual case. You've added the birria special, and git commit has opened Vim for the message. Git keeps the body exactly as you type it, and the custom is to keep its lines to 72 characters. You pasted the first paragraph in from the kitchen's group chat, where it was one long line.

:set textwidth=72 sets the width. gq rewraps text to it, breaking long lines at spaces and joining short ones. It works like d, with a motion or a text object after it, so gqip rewraps the paragraph the cursor is in (ip is the run of lines between two blank lines). While textwidth is 0, its default, gq wraps at the window's width, up to 79.

Try it here

Wrap the commit message

The cursor is at the start of the long paragraph. Git's notes at the bottom stay as they are.

Set the width with :set textwidth=72 and ↵, then rewrap the paragraph with gqipCOMMIT_EDITMSG
2Add birria tacos to the Friday menu
1
3Birria tacos go on the menu every Friday from 11:00 until the pot runs dry, three for 11.50 with a cup of the broth on the side for dipping.
1
2The kitchen screen lists it under Specials, and BIRRIA-FRIDAY goes on
3the receipt so the till can count them at close.
4
5# Please enter the commit message for your changes. Lines starting
6# with '#' will be ignored, and an empty message aborts the commit.
NORMALLoading the editor…0.0sLn 3, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Rewrap a paragraph after you edit it

A wrapped paragraph doesn't stay tidy once you edit it. You decide the kitchen screen should show a lime wedge icon too, type that into the second paragraph, and now its first line runs long. gqip would fix it and leave the cursor on the paragraph's last line. gwip rewraps it the same way and leaves the cursor where it was, so you can carry on typing. To rewrap only the line you're on, use gqq.

Try it here

Tidy the second paragraph

The cursor is on lime, where you just typed.

Rewrap the paragraph with gwip, and watch the cursor stay on limeCOMMIT_EDITMSG
5Add birria tacos to the Friday menu
4
3Birria tacos go on the menu every Friday from 11:00 until the pot runs
2dry, three for 11.50 with a cup of the broth on the side for dipping.
1
6The kitchen screen lists it under Specials with a lime wedge icon, and BIRRIA-FRIDAY goes on
1the receipt so the till can count them at close.
2
3# Please enter the commit message for your changes. Lines starting
4# with '#' will be ignored, and an empty message aborts the commit.
NORMALLoading the editor…0.0sLn 6, Col :51
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Wrap as you type, and see the limit

With textwidth set, Vim also breaks lines while you type in Insert mode. When a word would go past the width, it moves down to a new line. Neovim, and Vim with file type plugins on, set textwidth to 72 for Git commit messages by themselves, so there you get this without asking.

To see where the limit is, :set colorcolumn=80 colours column 80 all the way down the window. :set colorcolumn=+1 puts the stripe one column past textwidth, whatever that is.

Practise the keys

Widths from memory

Type each one as you would in Vim, Enter included where there is a command line.

Which keys do it?
Make Vim break lines at 72 characters as you type.
scroll here, then press it
1 of 4
Make Vim break lines at 72 characters as you type.

Keep your wrap settings

:set lasts until you close Vim. To keep a setting, put the same words in ~/.vimrc, without the colon. If you'd rather scroll sideways through long lines of code, that's set nowrap. If you mostly write prose and want it wrapped between words, it's set linebreak.

textwidth is better kept to the files that are prose, so Vim doesn't break your code as you type. autocmd FileType markdown,text setlocal textwidth=80 sets it for Markdown and plain text, as long as it comes after the filetype plugin indent on line. To have j and k move by screen row, nnoremap <expr> j v:count ? 'j' : 'gj' (and a matching line for k with gk) does it while a count such as 5j still moves by real lines.

In Neovim's init.lua the options are vim.opt.wrap = false and vim.opt.linebreak = true, and any Vim line, such as the autocmd, can go to vim.cmd as a string. Your Vim config, line by line goes through the rest of a config.

Try it here

Add it to your vimrc

Your ~/.vimrc has one line so far. Add linebreak below it.

o opens a new line below and starts typing there. Type set linebreak, then press Esc.vimrc
1" ~/.vimrc
2set number
NORMALLoading the editor…0.0sLn 2, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

TypeWhat it does
:set wrap ↵Wrap long lines at the window's edge (on by default). Changes the view, not the file.
:set nowrap ↵Keep each line on one row and cut it off at the edge. :set wrap! switches between the two.
zl / zhWith nowrap, slide the view one column right or left.
:set linebreak ↵Wrap between words instead of mid-word (lbr). Display only.
:set breakindent ↵Start each wrapped row at the line's own indent (bri).
gj / gkDown or up one row of the screen.
g0 / g$To the start or end of the row you are on.
:set textwidth=72 ↵The width for gq and for breaking lines as you type (tw). At 0, nothing breaks as you type and gq uses the window, up to 79.
gqipRewrap the paragraph. The cursor ends on its last line.
gqq / gqjRewrap this line, or this line and the next (gq takes any motion).
gwip / gwwRewrap the paragraph or the line, and leave the cursor where it was.
V, then gqRewrap the lines you selected.
:set colorcolumn=80 ↵Colour column 80 (cc). +1 means one past textwidth.