Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Touch" current file in emacs

Tags:

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.

like image 931
spike Avatar asked Jan 24 '12 15:01

spike


People also ask

Will touch overwrite a file?

touch cannot overwrite the contents of an existing file, but the redirect will. If boot. txt does not exist, it is created.

What is touch command in terminal?

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.

What is the command to open a file within Emacs?

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.


2 Answers

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."

like image 121
Greg Mattes Avatar answered Oct 25 '22 16:10

Greg Mattes


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.

like image 41
harpo Avatar answered Oct 25 '22 15:10

harpo