Wednesday, April 15, 2009

Running an elisp function on each marked file in a dired buffer

Chain Tool by Florian 

If you have some function, say some custom refactoring operation, you can call it from a dired buffer, and have it run on each marked file, using the code below. 

Go into a dired buffer, mark some files, and then do M-x and run your command. In this case it would be test-for-each-dired-marked-file

;;; iterating over files in dired

;;; usage example - for-each-dired-marked-file returns a filename and path
;;; for each marked file, so this is what a function using it looks like
(defun view-stuff(filename)
"opens up the file and gets the length of it, then messages the result"
(let (fpath fname mybuffer len)
(setq fpath filename)
(setq fname (file-name-nondirectory fpath))
(setq mybuffer (find-file fpath))
(setq len (buffer-size))
(kill-buffer mybuffer)
(message "Buffer length %d %s" len (buffer-file-name mybuffer))))

; Usage example
(defun test-for-each-dired-marked-file()
(for-each-dired-marked-file 'view-stuff))

(defun for-each-dired-marked-file(fn)
"Do stuff for each marked file, only works in dired window"
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files)))
(mapcar fn filenames))
(error (format "Not a Dired buffer \(%s\)" major-mode))))

Thursday, April 2, 2009

Securely copying files to another machine

Fast post about copying files between computers over a secure connection.

Linux and Cygwin comes with scp, a secure version of cp.

Usage... in this case I'm copying a file from my cygwin (putty) terminal to a remote machine.

scp /cygdrive/c/Documents and Settings/myname/My Documents/interestingfile.txt usernameonmachine@

You should get a password prompt and see the transfer complete as follows.

interestingfile.txt 100% 34KB 34.3KB/s 00:00