Vim auto line-break
Solution 1
You need to step back a little and use gj
and gk
which go down and up inside wrapped lines.
Since gj
and gk
work exactly the same as j
and k
in non-wrapped lines you can safely map j
or <down>
to gj
and k
or <up>
to gk
making it all seamless.
-- EDIT --
Yes it doesn't adress Eddy's immediate problem but it solves his original problem (vertical movement in wrapped lines) which led him to a poor workaround that, in turn, put him in this situation.
Solution 2
You can limit the width of a line with the textwidth
option
(see :help tw
).
For example, if you want to limit the width to 80 columns, you can use:
:set tw=80
With this option, when you will type something longer than 80 columns, Vim will automatically insert a newline character.
Eddy
Updated on August 12, 2022Comments
-
Eddy over 1 year
When I'm writing a long line of text in vim (such as a paragraph in latex), it wraps my text into multiple lines which is good. However, if I then try to navigate these lines with 'j' and 'k' (or the up/down arrows) it will skip the entire paragraph. I fixed this problem by highlighting the paragraph and pressing
gq
. This inserts line breaks at the end of each line.My question is, is there a way to automate this, so I don't have to keep highlighting text and pressing
gq
? -
Dmitry Frank about 12 yearsBut there's an issue: when adding new words in an existing line, you will have to select paragraph and press
gq
to re-align it. You can automate this by typing:set fo+=a
. Read:help fo
and:help fo-table
about this. But, unfortunately, this mode (after:set fo+=a
) works not very good and has several issues too. -
Eddy about 12 yearsThanks, this is good because I use svn version control for my latex documents, so now diff won't show loads of lines that have changed due to linebreaks and word wrapping.
-
cprn about 7 yearsAlso, it doesn't break lines without white space. Just thought it's worth mentioning.