Apply elisp function to any file

How to apply elisp function to a specific file that is not in the open buffer?

Eg (my/apply delete-duplicate-lines "~/tmp")

+4
source share
3 answers

You can easily do this with marked Dired files if you use the Dired + ( ) library . dired+.el

  • @ in Dired, it requests a function (function name or lambda expression), and then applies this function to the marked files.

    By default, it applies a function to each marked file name. A function can do anything, but it must take a file name as an argument. (The function should not visit the file.)

    arg (C-u) ( ) .

  • M-+ @ Dired , , , . (M-+ , .)

    - , Dired , . .

    @, arg (C-u) .

    arg ARG Dired + dired-get-marked-files. , C-u C-u Dired buffer - . :

    • ARG , ARG ( - ARG, < 0).

    • ARG 16, 64 256, C-u C-u, C-u C-u C-u C-u C-u C-u C-u, all Dired buffer, :

      • 16 no ( . ..)

      • 64 . ..

      • 256 ( . ..)

    • ARG nil, .

+2

,

(with-current-buffer (find-file-noselect "~/tmp")
  (delete-duplicate-lines (point-min) (point-max))
  (save-buffer))

, ,

+4

If you want to also clear the buffer, etc., you can use

(let ((file "~/tmp"))
  (with-temp-file file
    (insert-file-contents file)
    (delete-duplicate-lines (point-min) (point-max))))
+3
source

All Articles