spike
spike

Reputation: 10004

"Touch" current file in emacs

Im emacs, I want to run "touch" on the file referenced by the current buffer (specifically want to change the modification time). I use guard to run some tests after files are changed, but sometimes I want to invoke it manually. I don't care about running the actual shell utility touch so long as mtime is set.

Upvotes: 18

Views: 2761

Answers (5)

RichieHH
RichieHH

Reputation: 2123

8 years later, the useful "f" library in melpa provides f-touch.

See: https://github.com/rejeep/f.el

Upvotes: 2

Greg Mattes
Greg Mattes

Reputation: 33949

Of course, this assumes that you have a command named touch on your path.

(defun touch ()
     "updates mtime on the file for the current buffer"
     (interactive)
     (shell-command (concat "touch " (shell-quote-argument (buffer-file-name))))
     (clear-visited-file-modtime))

In dired-mode there is a touch command bound by default to T. That command isn't so easy to use though because it prompts the user for a timestamp. I suppose that's very general, but it isn't a very convenient way to do what is typically intended by "touch."

Upvotes: 9

jtahlborn
jtahlborn

Reputation: 53694

Here is a pure emacs way to do it:

(defun touch-file ()
  "Force modification of current file, unless already modified."
  (interactive)
  (if (and (verify-visited-file-modtime (current-buffer))
           (not (buffer-modified-p)))
      (progn
        (set-buffer-modified-p t)
        (save-buffer 0))))

Upvotes: 6

harpo
harpo

Reputation: 43168

Maybe more keystrokes than a custom function, but you can do this out the box with C-u M-~ then C-x C-s.

M-~ is bound by default to not-modified, which clears the buffer's modification flag, unless you call it with an argument (C-u prefix), in which case it does the opposite. Then just save the buffer.

Upvotes: 7

guettli
guettli

Reputation: 27806

I do it like this: insert a space, end then I delete the space again and save. This changes the mtime.

Upvotes: 3

Related Questions