jsborjesson Yet another dev blog

Fixing J

The J motion stands out a little bit in that it acts immediately, and always joins the next line to the current one, unlike most other Vim actions that operate on a motion, and on the current line when doubled (dd, yy, cc etc.)

You can work around this problem by using it from visual mode, to join a paragraph to a single line you could do vipJ - but what we really want to do is Jip. This fits much better with the rest of Vim's interface.

Here is how you do it:

" Correct J and gJ to take a motion, and act on current line when doubled.
nnoremap <silent> J  :set operatorfunc=JoinOperator<CR>g@
nnoremap <silent> gJ :set operatorfunc=GJoinOperator<CR>g@
nnoremap JJ J
nnoremap gJJ gJ

function! JoinOperator(submode)
    '[,']join
endfunction

function! GJoinOperator(submode)
    '[,']join!
endfunction

This fixes both J and gJ to take motions, and operate on the current line when doubled.