# Wrapping Text In Vim

## Terminology: Hard Wrap vs Soft Wrap

A “soft” wrap is when your text editor just makes the text look like it has been wrapped at the edge of the screen, when in reality it’s just one very long line. This is pretty obvious when line numbers are turned on because there are multiple lines of text but the line number to the left doesn’t increase.

A “hard” wrap is when your text editor is configured to actually insert a newline character \n at a predefined width. You wouldn’t want it at the edge of the window because that is pretty random.

## Enabling Soft Wrap

I’ve got the following in my ~/.vimrc

set wrap
" enable soft wrapping at the edge of the screen
set linebreak
" make it not wrap in the middle of a "word"


:set wrap causes vim to soft-wrap on the edge of the window.

:set linebreak causes it to not break in the middle of a word, but this will only work if the list setting is not enabled. Normally this isn’t a problem but if it’s not working you can :set nolist

So, if you want your Vim to soft-wrap at the edge of the screen, but not break in the middle of a word you can say.

:set wrap linebreak nolist

I’ve never needed to specify the nolist.

## Enabling Hard Wrap

Hard and soft aren’t exclusive. My Vim wraps at the edge of the window and puts a hard wrap at 80 characters.

Enabling hard wrap is pretty simple. You just tell Vim where to make the cut-off.

:set texwidth=80

To disable it you just set it to 0

:set textwidth=0

I can never remember if it’s textWIDTH or textWRAP so I just use the tw abbreviation which works for both. ;)

#### The Quirk

In pretty much every other editor, having your cursor at the end of a line and then using the right arrow (assuming a left-to-right language) will result in the cursor moving to the start of the next line. Not in Vim. At least, not by default.

set whichwrap+=<,>,h,l

whichwrap allow specified keys that move the cursor left/right to move to the previous/next line when the cursor is on the first/last character in the line. This line says “Hey Vim, add < (left arrow), > (right arrow), h and l to the list of keys that are allowed to move to the previous / next line.”

See :help wrichwrap for more details.

### Bonus Points

I like having a visual indicator of where that 80 character border is. Vim can give you one.

    set colorcolumn=80
" make a vertical column in the background at 80 characters

highlight ColorColumn guibg=Black
" make it black in Graphical Vims (my vim background is dark gray)
" see help gui-colors for a list of suggseted color names
" see help guibg for how to specific specific rgb / hex colors

highlight ColorColumn ctermbg=0
" make it black in terminal vims (my terminal vim looks the same as my GUI vim)
" see :help ctermbg for a list of colors that can be used in the terminal


## Reflowing Text

Let’s assume you’ve got a document with crazy-long lines or maybe a paragraph that look like this:

This is a line line that is very long, despite us wanting it to be very short. Why do people write lines like this.
I'm a short line.
I'm a medium length line.


But you want it to look like this:

This is a line line that is very long, despite us
wanting it to be very short. Why do people write
lines like this.  I'm a short line.  I'm a medium
length line.


I just made a visual selection of the paragraph and invoked gq.

I have no idea what gq stands for, but I like to think of the men’s fashion magazine of the same name. It’s all about making things look good, and that’s what gq does for me.