Bruce
Bruce

Reputation: 153

Running batch of emacs commands on file

to clean up some files, I need to perform a series of steps, the emacs code for these steps is

(query-replace "," " " nil 
  (if (and transient-mark-mode mark-active) 
    (region-beginning)) 
  (if (and transient-mark-mode mark-active) 
    (region-end)))

(query-replace "1 1/4" "1.25" nil 
  (if (and transient-mark-mode mark-active) 
    (region-beginning)) 
  (if (and transient-mark-mode mark-active) 
    (region-end)))

(query-replace-regexp "[0-9][0-9][0-9]V[0-9][0-9][0-9] " "" nil 
  (if (and transient-mark-mode mark-active) 
    (region-beginning)) 
  (if (and transient-mark-mode mark-active) 
    (region-end)))

(there's more, but you get the idea) Is there a way to put all these commands in a file and run them? Or perhaps assign a name to them in a lisp file and run them by name?

Upvotes: 3

Views: 1151

Answers (1)

abo-abo
abo-abo

Reputation: 20362

You can refer to this section of the manual. Put all the stuff that you want to do in a mega-function foo. Put that into ~/bar.el. Then this will do the job:

emacs --batch -l ~/bar.el -f foo

UPD: a small example

Put into ~/bar.el:

(defun foo ()
  (goto-char (point-min))
  (replace-regexp "foo" "bar")
  (goto-char (point-min))
  (replace-regexp "fred" "barney")
  (save-buffer))

Create a test file:

echo "Fred walks into a foo" > test.txt

Now test it:

emacs --batch -l ~/bar.el test.txt -f foo

Upvotes: 3

Related Questions