当我在emacs中使用dired模式时,我可以按类型!xxx运行shell命令,但是如何绑定一个键来运行这个命令? 例如,我想在文件上按O,然后dired将运行'cygstart'来打开该文件。
答案 0 :(得分: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中,您可以使用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)))
答案 1 :(得分: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类似于任何常规函数定义。