Vim line wrapper: how to insert backslash '\' automatically when wrapping a line?

Suppose I :set tw=5when the following long line ends:

a = b + c

This will:

a = b
+ c

But I want it to be:

a = b \
+ c

Or even better, if an indent is inserted before the next line, for example:

a = b \
  + c

How to do it?

+4
source share
1 answer

What you are looking for is this :h formatexpr.

You need to define an expression that checks what mode you are in mode() ==# 'i', and then make the changes you want to make. Returning a nonzero value will use the expression expr.

eg.

set formatexpr=FormatFoo()
function! FormatFoo()
  if mode() ==# 'i'
    echom "insertmode line wrap"
    return 1
  else
    echom "normalmode line wrap"
    return 1
  endif
endfunction
+5
source

All Articles