9

emacsでdiredモードを使用すると、!xxxと入力してシェルコマンドを実行できますが、キーをバインドしてこのコマンドを実行するにはどうすればよいですか?たとえば、ファイルに対して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では、を使用してその時点でファイルの名前を取得できます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 に答える