我正在寻找等效的以下vi命令
的:! nl%
这在当前打开的文件上运行nl命令
什么是emacs检测打开文件名称的方法?
M-X shell-commnad nl
我无法找到当前打开/缓冲区和替换的确定值。 THX /马赫什
答案 0 :(得分:2)
编辑:误读您的问题,因为您希望将该更改应用于您正在处理的文件。如果您只想对缓冲区运行shell命令,可以使用shell-command-on-region
,M-|
通常绑定到M-x goto-line
。
如果您只是尝试访问特定的行号,C-x C-l
可以正常工作。我将(define-key global-map "\C-x\C-l" 'goto-line)
放入~/.emacs
。
~/.emacs
试试这个(在;;; Run a shell command on all text between the mark and the point and
;;; replace with the output.
(defun shell-command-in-region (start end command &optional flag interactive)
"Execute shell-command-on-region and replace the region with the output
of the shell command."
(interactive (list (region-beginning) (region-end)
(read-from-minibuffer "Shell command in region: "
nil nil nil 'shell-command-history)
current-prefix-arg
(prefix-numeric-value current-prefix-arg)))
(shell-command-on-region (point) (mark) command t)
)
(define-key esc-map "#" 'shell-command-in-region)
文件中):
M-#
通过选择您要操作的区域然后执行{{1}}来调用它。
答案 1 :(得分:0)
如果您总是希望为shell命令插入缓冲区的文件名,则可以使用以下建议:
(defadvice read-shell-command (before read-shell-command-with-filename activate)
"force the initial contents to contain the buffer's filename"
(if (and (null (ad-get-arg 1))
buffer-file-name)
(ad-set-arg 1 buffer-file-name)))
一旦添加了上述代码, M-x shell-command 将始终以缓冲区的文件名开头,因此您可以在命令中使用它。
答案 2 :(得分:0)
我用这个:
(defun my-shell-command-on-current-file (command &optional output-buffer error-buffer)
"Run a shell command on the current file (or marked dired files).
In the shell command, the file(s) will be substituted wherever a '%' is."
(interactive (list (read-from-minibuffer "Shell command: "
nil nil nil 'shell-command-history)
current-prefix-arg
shell-command-default-error-buffer))
(cond ((buffer-file-name)
(setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
(setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
(shell-command command output-buffer error-buffer))
(global-set-key (kbd "M-!") 'my-shell-command-on-current-file)
然后你可以做 M-! nl%