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.
touch cannot overwrite the contents of an existing file, but the redirect will. If boot. txt does not exist, it is created.
The touch command's primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch command creates a file only if the file doesn't already exist.
Use Ctrl-x f to open a file from within Emacs. Create a new file in the same way as opening a file by specifying the new filename. The new file will not be saved unless specified. Save a file that is currently open by entering the Ctrl-x Ctrl-s command.
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."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With