이맥스에서 dired 모드를 사용할 때! xxx 형식의 쉘 명령을 실행할 수 있지만이 명령을 실행하기 위해 키를 바인딩하는 방법은 무엇입니까? 예를 들어 파일의 O를 누르면 dired는 'cygstart'를 실행하여이 파일을 엽니 다. 감사합니다. .dired 이맥스에서 쉘 명령을 실행하기 위해 키를 바인딩하는 방법
7
A
답변
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)))
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
이 키워드이고, 다음 괄호 쌍이 필요한 경우 인수를 포함합니다. 나머지는 정규 함수 정의와 유사합니다.
참고 : 질문에 답하기 전에 이러한 기능에 대해 알지 못했습니다. 'M-! '에'C-h k'를 사용하여'shell-command'라는 이름을 얻었습니다. 먼저'dired-'함수라는 것을 추측하고 'C-h f'와 탭을 사용하여 이름을 자동 완성하는'dired-file-name-at-point'를 얻었습니다. Emacs의 함수 이름과 효과를 쉽게 알아낼 수 있습니다. ** 결국 ** 자체 문서화 ** 에디터입니다! 그것은 그것이 굉장한 많은 방법 중 하나 일뿐입니다. –