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.

Advertisement

6 comments

  1. 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.

    Like

  2. 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)))

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s