Frage

Let's assume my .vimrc has the following configuration:

set textwidth=10

In a file I type the following:

This is a 
sentence
word wraps
just fine
at 10 
chars.

As I type this sentence, VIM will word-wrap in real time and prevent me from passing the marker I've set. However, a common issue I find myself in is adding more content. I forgot a word in the previous paragraph, "sentence THAT word wraps."

So I go back and add it.

This is a
a sentence that, suddenly, doesn't
word wrap
just fine
at 10
chars.

If I wanted to fix this now (have all lines below 10 character limit that I've set), I'd have to find the 10 character spot, hit RETURN (to move extra content down), then find the 10 character spot on the next line, hit RETURN, and so on.

This scenario surfaces a lot for me in my program and function prologues in code (I typically work with 73 character line limits), because I find myself having to add more explanations or content later.

Is there a quick/easy way to fix this problem?

Also:

I have this in my .vimrc to highlight text-width offenders:

" highlight anything past the 73 character limit
augroup vimrc_autocmds
    autocmd Filetype cpp autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#592929
    autocmd Filetype cpp autocmd BufEnter * match OverLength /\%73v.*/
augroup END

Perhaps there is something here I can add too?

War es hilfreich?

Lösung

You can re-format the affected lines with the gq command. Often, gqap (format a paragraph) will do, if not, use visual mode or a motion.

You can also make Vim continuously re-format while typing with :set formatoptions+=a, but that never worked out well for me.

Andere Tipps

Vim manual entry for :help fo-table reports:

l       Long lines are not broken in insert mode: When a line was longer than
        'textwidth' when the insert command started, Vim does not
        automatically format it.

Since in your comment you say that the formatoptions contains the letter l, you should turn of this option with the command:

:set formatoptions-=l

To fix existing lines, as Ingo already suggested, you can use the gq command which will honour the textwidth setting.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top