2

I'm trying to configure line splitting which I want to make available only if the current symbol is space. I currently configured it as so:

r<ENTER>k$

But this does the following:

line1 line2
     ^
   cursor

to

line1
line2

and

line1line2
    ^
  cursor

to

line          <---------- 1 is missing
line2

which is not desirable. So I want to allow to split the line only if the current symbol is space. Is it possible to do?

1 Answer 1

3

You can use an expression mapping:

nnoremap <expr> <Leader>s getline('.')[col('.')-1] =~ "\\s" ? "r<Enter>k$" : ""

The <expr> will make Vim evaluate a Vimscript expression, instead of just a "static" list of keys.

Here, we use getline('.')[col('.')-1] to get the character under the cursor, we check if this is whitespace with =~ "\\s" (this will also match tabs etc.) and run r<Enter>k$ if it is.

If it doesn't match, we do nothing.


Perhaps a more useful mapping is:

nnoremap <expr> <Leader>q getline('.')[col('.')-1] =~ "\\s" ? "r<Enter>k$" : "i<Enter><Esc>k$"

Which will split the line without deleting whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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