在emacs中"触摸"当前文件

spi*_*ike 15 emacs

我是emacs,我想对当前缓冲区引用的文件运行"触摸"(特别是想要更改修改时间).我使用guard在文件更改后运行一些测试,但有时我想手动调用它.只要设置了mtime,我就不关心运行实际的shell实用程序触摸了.

Gre*_*tes 8

当然,这假设您touch在路径上命名了一个命令.

(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))
Run Code Online (Sandbox Code Playgroud)

在直接模式下,默认绑定的触摸命令T.该命令不是那么容易使用,因为它会提示用户输入时间戳.我认为这是非常普遍的,但这并不是一种非常方便的方式来做"触摸"通常所要求的.

  • 如果缓冲区名称中有空格怎么办?或以"; rm -rf /"结尾?我建议使用`(shell-quote-argument(buffer-file-name))` (2认同)

har*_*rpo 6

也许更比按键自定义功能,但你可以做到这一点的开箱C-u M-~然后C-x C-s.

M-~默认情况下绑定not-modified,清除缓冲区的修改标志,除非你用参数(C-u前缀)调用它,在这种情况下它会相反.然后只需保存缓冲区.


jta*_*orn 5

这是一种纯emacs方式:

(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))))
Run Code Online (Sandbox Code Playgroud)