1

In Neovim, I used the J command to join lines, and now I'm looking for a keybinding in normal mode that can do the opposite: replace the space after a word with a line break.

I've tried a few "revJ" (reverse J) plugins for Neovim, but I couldn't get them to work correctly, likely due to my configuration.

I'm wondering if there's a good solution for this inconvenience.

My goal:

"a, bb, ccc" => "a,
                 bb,
                 ccc"

2 Answers 2

1

The :substitute command can be used to replace spaces with line breaks like this:

:s/ /\r/g

This will replace every space with a line break and repeat for every match on the line (g flag at the end)

It would be a little more robust (and a little bit more typing) to use:

:s/\s\+/\r/g

This will work on any sequence of whitespace (\s) repeated one or more times (\+). So tabs are also taken care of, and multiple spaces will still result in only one line break.

See :help :substitute for reference.

To create a reverse J mapping for regular use, you can use the following:

:nnoremap <key> <Cmd>s/\s\+/\g/g<CR>

Replace <key> with whatever key (sequence) you want for reverse J.

By the way, all of this will work both in Vim and in NeoVim.

0

The solution I found:

The keybindig consists of the following commands: f<space>s<enter><C-c>

f<space> ... jumps over a word

s<enter> ... removes the space and inserts a new line character

<C-c> ... represents the keybind Ctrl-C and for me, it exits the insert mode

This solution is meant for those who want a command that works on a line-per-line basis and doesn't just transform the whole text. This solution also works if there are multiple spaces between words.

2
  • 2
    You could also use r<CR> to replace a single character with a newline. No need to leave insert mode after that.
    – Friedrich
    Nov 3, 2023 at 7:13
  • 2
    C-c and Esc behave differently. Prefer Esc.
    – D. Ben Knoble
    Nov 3, 2023 at 13:19

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.