Friday, 7 October 2011

Add a delete equivalent of kill-region and kill-line keys C-w and C-k in emacs.

A very frequent editing operation I find myself doing is, for multiple files, find something, delete it and replace it with something else. Usually the replacement is done using paste. Pretty simple. Duhrrr. Today again I am fixing up multiple files repeatedly with same or similar changes.

This can be sped up using macros. Start from emacs dir browse a macro can visit each file, jump to start of buffer, do the needful replace, save the file and go back to previous buffer (browse directory) and advance to next file ready for macro to run again.
e.g.
 start macro: "C-x ("
 select file: "Enter"
 jump to start of buffer: "C-Home"
 replace "M-x replace-regexp xxxx yyyy"      (or "M-x my-custom-repacement-func" see below)
 save: "C-x C-s"
 back to previous buffer "C-x b Enter"
 advance to next file "DownArrow"
 finish macro: "C-x )"

In emacs the behaviour of kill-ring makes pasting in the same copied line awkward.
C-w or C-k copy the killed text to kill-ring moving what you want to paste down in the ring.
Using command delete-region to CUT text without copying it into the kill-region solves the difficulty, but the lack of a key binding to delete-region makes it easy to overlook or forget about!
 C-space go to end of text M-x delete-region
is harder than
 C-space go to end of text C-w
or
 C-k



Add a delete equivalent of kill-region and kill-line keys C-w and C-k as follows.
Bound to keys C-v and C-z.


;; A keybinding to delete-region gives a good "Windows CUT Ctrl-x equivalent".
;; What keybinding to use is awkward to choose.
;; using C-v "Windows PASTE Ctrl-v" is quite a subversive option.
;;  C-v = scroll up in emacs which I have no use for.
(global-set-key (kbd "C-v") 'delete-region)

;; To have also a "Windows CUT" alternative to C-k (kill-line) this can be done:
(defun delete-line () "delete line, take it out of kill ring. bind this func to C-z"
 (interactive)
 (setq last-command 'delete-line)
 (kill-line)
 (setq kill-ring (cdr kill-ring))
 (setq kill-ring-yank-pointer kill-ring)
 (setq last-command 'delete-line)
)
(global-set-key (kbd "C-z") 'delete-line)
;; without setting of last-command 2+ C-zs mess up kill-ring
============================================================
other recently used quick replace elisp funcs:
(defun replace-ucolc () "replace old olc suc and ssuc ids with valid ones"
 (interactive)
 (replace-regexp "OLC.UC.29.?\\b" "SYS.UC.29")
 (replace-regexp "SYS.UC.151\\b" "SUB.UC.DP.1") 
)
(defun replace-olctest-timeouts2 () "replace hardcoded numbers in func calls in about 20 olc tests with variable"
 (interactive)
 (replace-regexp "\\(olc::checkForOLCState .*\"WAIT.*COMPLETED\" +\\)240" "\\1 $olc::icWaitForDCGAReady")
 (replace-regexp "\\(olc::checkForOLCState .*\"COMPLETED\" +\\)240" "\\1 $olc::icWaitForOLCCompletedAfterNodesReady")
 (replace-regexp "\\(olc::checkOLCControlState .*->completed} +\\)60" "\\1 $olc::icWaitForOLCCompletedAfterNodesReady")
 (replace-regexp "\\(olc::checkOLCControlState .*->completed} +\\)6" "\\1 $olc::icWaitForOLCCompletedAfterCompleted")
 (replace-regexp "\\(olc::checkDSSEState .* +\\)420" "\\1 $olc::icWaitForDatapathReadyAfterOLCCompleted")
 (replace-regexp "\\(olc::checkForNodeState .*\"OLC.*DC_GA.*READY\" +\\)180" "\\1 $olc::icWaitForDCGAReady")
)

No comments: