I'm looking for an emacs function that will delete all whitespace from the cursor position (including newlines) until the first non-whitespace character.

For example, if my cursor is positioned at the end of the first line:

main(arg1,
     arg2)

The delete function would result in:

main(arg1,arg2)
up vote 7 down vote accepted

You might find the hungry-delete package useful. I personally bind C-cdelete to delete whitespace after point, and C-cbackspace to delete whitespace before point like so

(global-set-key (kbd "C-c <backspace>") 'hungry-delete-backward)
(global-set-key (kbd "C-c <deletechar>") 'hungry-delete-forward)

I use this quite often:

(defun join-line* ()
  "Join this line with the next line deleting extra white space."
  (interactive)
  (join-line t))

(global-set-key (kbd "M-j") #'join-line*) ;; just key binding I use…

Yet another solution

(defun foo () (interactive)
(delete-region (point)
(progn (while (or (equal (char-after) ? ) (eolp)) (forward-char)
)(point))))

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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