如何绑定密钥以在dired emacs中运行shell命令

Min*_*ngo 8 emacs dired

当我在emacs中使用dired模式时,我可以按类型!xxx运行shell命令,但是如何绑定一个键来运行这个命令?例如,我想在文件上按O,然后dired将运行'cygstart'来打开这个文件.

Tik*_*vis 12

你可以使用这个shell-command功能.例如:

(defun ls ()
  "Lists the contents of the current directory."
  (interactive)
  (shell-command "ls"))

(global-set-key (kbd "C-x :") 'ls); Or whatever key you want...
Run Code Online (Sandbox Code Playgroud)

要在单个缓冲区中定义命令,可以使用local-set-key.在dired中,您可以使用获取文件的名称dired-file-name-at-point.那么,要完全按照你的要求去做:

(defun cygstart-in-dired ()
  "Uses the cygstart command to open the file at point."
  (interactive)
  (shell-command (concat "cygstart " (dired-file-name-at-point))))
(add-hook 'dired-mode-hook '(lambda () 
                              (local-set-key (kbd "O") 'cygstart-in-dired)))
Run Code Online (Sandbox Code Playgroud)

  • 注意:在回答问题之前,我不知道这些功能.我在`M-!`上使用`Ch k`得到了`shell-command`的名字; 我得到了`dired-file-name-at-point`,首先猜测它是一个`dired -`函数,然后使用`Ch f`和tab来自动填充名称.您也可以轻松找出Emacs函数名称和这样的效果 - 毕竟它是一个**自我记录的**编辑器!这只是众多令人敬畏的方式之一. (5认同)