2011-12-06 7 views

答えて

10

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))) 
+5

注:質問に答える前に、私はこれらの機能について何も知らなかった。 'M-! 'に' C-h k'を使って 'shell-command'という名前をつけました。最初に 'dired-'関数であることを推測し、 'C-h f'とタブを使って名前を自動完成させることによって' dired-file-name-at-point'を得ました。このようなEmacsの関数名やエフェクトを簡単に理解することもできます。結局のところ、それは**自己文書化エディタです**!それは素晴らしい方法のひとつです。 –

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は通常の関数定義に類似しています。

関連する問題