Vim auto line-break
Asked Answered
A

3

43

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?

Aubree answered 3/2, 2012 at 15:39 Comment(1)
Are you asking for the word wrap to auto insert line breaks?Darcidarcia
M
36

You need to step back a little and use gj and gk which go down and up inside wrapped lines.

Since gjand 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.

Metopic answered 3/2, 2012 at 15:57 Comment(1)
Thanks, 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.Aubree
S
70

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.

Sequel answered 3/2, 2012 at 15:44 Comment(2)
But 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.Broadbill
Also, it doesn't break lines without white space. Just thought it's worth mentioning.Ahimsa
M
36

You need to step back a little and use gj and gk which go down and up inside wrapped lines.

Since gjand 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.

Metopic answered 3/2, 2012 at 15:57 Comment(1)
Thanks, 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.Aubree
S
0

These are the mappings I use. They handle all the cases I have encountered up to now.

" Modify the up/down keys so that they move per virtual (rather than
" physical) line if a line is displayed wrapped and no count for the
" command has been specified. Always use logical line steps for quickfix
nnoremap <expr> k ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> j ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
nnoremap <expr> <Up> ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> <Down> ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
xnoremap k gk
xnoremap j gj
vnoremap <Up> gk
vnoremap <Down> gj

" Move to start and end of virtual line (this will default to normal behaviour
" if the line isn't wrapped)
nnoremap 0 g0
nnoremap <Home> g0
nnoremap ^ g^
nnoremap $ g$
nnoremap <End> g$

" These mappings need to deal with virtual line numbers in 'insert' mode,
" but they need to do it without messing up normal 'completion menu' operation
inoremap <expr> <Up> pumvisible() ? "\<Up>" : "\<C-o>gk"
inoremap <expr> <Down> pumvisible() ? "\<Down>" : "\<C-o>gj"
Shirting answered 18/8, 2023 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.