1
votes

Often while coding I need to align comments etc. to specific columns. It is useful to have a key combination/command that will delete everything from cursor to the next non-white character.

  1. M-\ runs the command delete-horizontal-space, which however deletes white space to left of cursor as well, which is not the desired outcome! Is there any existing command that will do this?

  2. With some trial and error cooked up the following function, which seems to do what I want.

    (defun del-ws-to-right ()
      "Delete all spaces and tabs from point to next non-white char."
      (interactive)
      (save-excursion 
        (let* ((orig-pos (point))
               (numchrs (skip-chars-forward " \t"))
               (end-pos (+ orig-pos numchrs)))
          ;(message "orig-pos : end-pos = %d : %d" orig-pos end-pos) 
          (delete-region orig-pos end-pos)
          )
        )
      )

(global-set-key (kbd "C-.") 'del-ws-to-right)

The elisp code for delete-horizontal-space etc. look more complicated! Just wonder if there is any gotchas here?!

2
I believe that this question: stackoverflow.com/questions/17958397/… is asking the same thing.user797257

2 Answers

3
votes

I don't think there is a built-in command for this.

Your solution is reasonable, though a bit wordy. You don't need to look at the result of skip-chars-forward, since it moves point. I'd probably write something more like:

(delete-region (point) (save-excursion (skip-chars-forward "\\s-") (point)))
1
votes

You can also use hungry-delete. If you install it and put

 (require 'hungry-delete)
 (global-hungry-delete-mode)

in your init file, C-d becomes bound to hungry-delete-forward which is defined to "Delete the following character or all following whitespace up to the next non-whitespace character."