Today I made a macro that involved selecting a few lines to join. The problem was that sometimes I only selected parts of one line and in this case two lines was still joined with J. Is it possible to avoid this?

So with which key combination can I do these two transformations: (< and > indicates selection)

content <of line 1
content of> line 2

Should be joined like this after pressing gJ:

content of line 1content of line2

And this

all <content on> one line

Should stay the same after pressing gJ. For now it joins the current line with the next one which is not what I want.

  • 3
    vnoremap gJ :join!<CR>
    – Matt
    May 12 '20 at 5:25
  • @Matt Thanks, if you add that as an answer I can accept it.
    – Moberg
    May 12 '20 at 12:50

As :join[!] command does not really do anything if range has equal start and end values, you can make use of it instead of original J / gJ

xnoremap <silent>J  :join<CR>
xnoremap <silent>gJ :join!<CR>

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.