6

I like the J command for joining many lines, but don’t like having to count how many lines I want to join. I’d prefer to give a motion. This can be faked with V{motion}J; is there a way that doesn’t involve entering visual mode (hence clobbering the * register)?

5
  • Or, even better: vi.stackexchange.com – Tim Mar 10 '15 at 23:02
  • this doesn't answer your question, but I use J when I get too many line breaks inserted from some bad line wrapping. I hold shift and repeatedly tap J watching as it improves. If there are a ton of line joins then I suppose I might hold J. – barlop Mar 10 '15 at 23:39
  • you don't want to count, but say that you seem to have to.. you want a gesture, but how do you propose that the gesture count how many Js to do? – barlop Mar 10 '15 at 23:39
  • @barlop "How do you propose that the gesture count how many Js to do?" The standard vim way: by giving a motion. e.g. one can d} to delete a paragraph, or d32G to delete to line 32, etc.; and I would like to J} to join a paragraph, or J32G to join to line 32, etc. – Daniel Wagner Mar 11 '15 at 0:16
  • Wasn't really saying it didn't belong here, just suggesting it may get a better answer there. – LawrenceC Mar 11 '15 at 1:05
5

I think there is no default join operator. But you can use this code, from a old vim thread on the same subject:

nnoremap J :set operatorfunc=Joinoperator<CR>g@
nnoremap gJ :set operatorfunc=GJoinoperator<CR>g@
onoremap J j
func! Joinoperator(submode)
        '[,']join
endfunc
func! GJoinoperator(submode)
        '[,']join!
endfunc 

Notice that using this snippet it won't be possible to use J to join a pair of lines -- it will be necessary to use Jj (or JJ due to the omap J j).

1
  • Perfect, 'operatorfunc' was just what I needed to know about. – Daniel Wagner Mar 17 '15 at 19:15
3

You can do something like:

:.,/pattern/join

Essentially you're telling Vim to join a range of lines, . being the current line the cursor is on, and the last line being the first match of pattern.

1
  • 2
    You can also mark the end position and use :.,'bj to join the ranges. Will need to be at 'a or vim will ask you to about swapping ranges Or mark start and end and use ;'a,'bj Use a relative range :.,+20j etc – Steve Mar 11 '15 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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