Join line to following line

I wrote a while ago about using M-^ to join the current line to the previous line. In fact, when editing I find that I most often want to join a line to the following line. I used to do this by using C-e to move to the end of the line and then C-d to delete until I got the next line joined to the current one. It is easier to do this with a single key using a simple function, which I set to C-j. Add the following to your emacs config file:

;; join line to next line
(global-set-key (kbd "C-j")
            (lambda ()
                  (interactive)
                  (join-line -1)))

You can keep hitting C-j to keep joining the next line.

Update

I originally used M-j as the keybinding for this, but Kaushal Modi pointed out in the comments that C-j is a better choice.

  • Clément Pit–Claudel

    Isn’t that what C-u M-^ does?

    • You are quite right – I hadn’t spotted that! It is handy to have on a single key so you can bash it out repeatedly.

  • Kaushal Modi

    The default bindings for M-j is priceless for me. Let’s say you are on a comment line “;; abc| def” where | is the cursor. If you hit M-j there, that single comment line converts to 2 lines: “;; abc” and “;; def”. But I also feel that need to frequently do (join-line -1). So I have a “pull-up” funciton that does that plus something special when call within a comment code.

    • That is a good point. I should switch to C-j so I can keep the functionality of M-j.

  • Pingback: 2015-11-09 Emacs News - sacha chua :: living an awesome life()

  • Juanma Barranquero

    I use the reverse (C-u) meaning of delete-indentation (AKA join-line) more than the normal one, so I defined an advice to switch the meaning of C-u, but only when used interactive.

    (define-advice delete-indentation (:around (indent-fun &optional arg) “reverse”)
    (funcall indent-fun (if (called-interactively-p ‘any) (not arg) arg)))