0

im looking for a way/function to kill/delete alll whitespace/empty area in a region to start of line. for example if i have this snippet

(defhydra hydra-copy-paste  (:color blue  :columns 4 :hints nil)
  "copy paste operations"
     ("0" z/copy-buffer-file-path  "copy current buffer to path" )
("a" align-regexp "align-regexp" )
  ("c" copy-to-register  "copy to register") 
("e" hydra-edit-extra/body  "Extra editing commands" :face 'hydra-face-purple)

    ("d" downcase-region  "downcase " )
("p" insert-register  "paste from register") 
("v" view-register  "view register") 
    ("r" z/open-this-file-as-other-user  "edit as root" :face 'hydra-face-red )
    ("u" upcase-region  "upcase " )
("q" nil "cancel" nil)
)

i want it to run and format the section by killing all whitespace till start of line so it looks like this

(defhydra hydra-copy-paste  (:color blue  :columns 4 :hints nil)
"copy paste operations"
("0" z/copy-buffer-file-path  "copy current buffer to path" )
("a" align-regexp "align-regexp" )
("c" copy-to-register  "copy to register") 
("e" hydra-edit-extra/body  "Extra editing commands" :face 'hydra-face-purple)
("d" downcase-region  "downcase " )
("p" insert-register  "paste from register") 
("v" view-register  "view register") 
("r" z/open-this-file-as-other-user  "edit as root" :face 'hydra-face-red )
("u" upcase-region  "upcase " )
("q" nil "cancel" nil)
)
3

One possibility would be to look at the implementation of (delete-trailing-whitespace), but certainly, the easiest way would be to select the region and hit:

M-x replace-regexp RET ^[[:space:]]+ RET RET

You can bundle that into an interactive function if need be.

  • thx thats perfect! – zeltak Dec 25 '16 at 11:34
  • actually, i created this small little function (defun zxx () "delete all numeric characters" (interactive) (goto-char (point-min)) (replace-regexp "^[[:space:]]+" "") ). this works well. any idea how to make the above funtion work only on marked region? – zeltak Dec 25 '16 at 11:40
  • 1
    replace-regexp accepts args for the start and end positions of its application. You can pick up the region limits with (interactive "r"), and test whether the region is active and non-empty with use-region-p. – Drew Dec 25 '16 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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