I write a text in vim and don't want to exceed the line length limit of 80 characters. Now I am on character 72, which is a space, start writing a word. The word ends at character 82. Is there some quick trick how to replace the last space by a newline (or somehow moving the last word to the new line) having a cursor at the end of that word, so instead of

Esc hhhhhhhhhh i Enter Esc llllllllll i blah blah blah

it would be

Esc magic i blah blah blah



There are many ways to do that.

The b key goes back one word, and B will go back a 'big' word (b will stop at dashes for example). So you could press

esc B h r enter

if you want to go back to input mode, you could do

esc B i enter

You could record a macro, if you tend do it often. To record a macro, you press q followed by any letter (you assign a macro to that letter's registry). Let's say we wanted to record the first sequence as a macro, and assign it to the z registry. you would do :

q z B h r enter q

now whenever you press @z in normal mode, it'll go back a word, and replace the character before it with a newline.

more on macros here http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/

  • Yab: Nice. With $ appended to the command sequence (so that the command finishes with cursor on line end) this would be the exact answer, but I still find setting text width more comfortable (as I use it to solve my problem). – Adam Trhon May 6 '11 at 21:46

Vim can do this automatically if you set the 'textwidth' option to 80. You can do that by putting this line in your .vimrc or _vimrc file:

set textwidth=80

That will break the line at or before column 80 when adding text. It will not reformat the entire paragraph. One way to do that while you're in insert mode is to execute


where <C-O> means to hold the Ctrl key down while typing the letter o. gqap is the command to reformat (gq) the current paragraph (ap). When that command is done, you will be back in insert mode with the cursor close to where it was before, but not necessarily at the same point in the sentence. You can read more about those commands at

:help i_CTRL-O
:help gq
:help ap

If the automatic line-breaking at 'textwidth' isn't working as you expect, you may need to change your 'formatoption'. See

:help formatoption
:help fo-table

for more on that.

If you're not sure where your .vimrc or _vimrc file is, you can open Vim and execute

  • If only I could give you another upvote for the gqap command. Thanks! – Adam Trhon May 5 '11 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.