9

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

4

2 回答 2

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

要在单个缓冲区中定义命令,您可以使用local-set-key. 实际上,您可以使用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)))
于 2011-12-06T09:03:13.647 回答
3
;; this will output ls
(global-set-key (kbd "C-x :") (lambda () (interactive) (shell-command "ls")))

;; this is bonus and not directly related to the question
;; will insert the current date into active buffer
(global-set-key (kbd "C-x :") (lambda () (interactive) (insert (shell-command-to-string "date"))))

而是定义了lambda一个匿名函数。这样,您不必定义将在另一个步骤中绑定到键的辅助函数。

lambda是关键字,如果需要的话,下一个括号对包含您的参数。Rest 类似于任何常规函数定义。

于 2015-02-01T20:48:27.443 回答