Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: remove spaces between expressions

While writing Clojure code, I often end up with spaces between the last expression and the closing brackets. Something like

(defn myfunction
  [arg]
  (do
    (expr1)
    (expr2)|
  ))

where | is the position of the cursor. Is there a shortcut in Emacs to delete the spaces between (expr2) and the final brackets? The goal is to end up with

(defn myfunction
  [arg]
  (do
    (expr1)
    (expr2)))
like image 231
Filippo Diotalevi Avatar asked Sep 20 '13 19:09

Filippo Diotalevi


3 Answers

M-^ (command delete-indentation) already does what you requested, at least in the example you gave (and similar). See (elisp) User-Level Deletion.

like image 83
Drew Avatar answered Nov 03 '22 05:11

Drew


Send the prefix argument to M-^:

C-u M-^

Without the prefix, M-^ joins the current line with the previous.

With the prefix (the C-u), M-^ joins the next line with the current.

like image 3
echosa Avatar answered Nov 03 '22 05:11

echosa


Improving on @wvxvw's comment above, you can add the following to your .emacs file. Then, C-z m (or any other key combination that you select) will do what you want. In fact, it will work if you're at any point of the line containing (expr1).

(global-set-key "\C-zm" 'join-lines-removing-spaces)
(defun join-lines-removing-spaces ()
  "Join the current line with the next, removing all whitespace at this point."
  (move-end-of-line nil)
  (kill-line)
  (delete-horizontal-space))
like image 1
nickie Avatar answered Nov 03 '22 05:11

nickie