emacs 相当于以下 vi 命令

发布于 2024-10-20 06:55:18 字数 161 浏览 3 评论 0原文

我正在寻找相当于以下 vi 命令的命令
<强>:! nl%
这对当前打开的文件运行 nl 命令
emacs 检测打开文件名称的方法是什么?
MX shell-commnad nl

我无法找到当前打开/缓冲区的确定值和替代值。 谢谢/马赫什

I am looking for equivalent of following vi command

:! nl %

this runs nl command on currently open file

What is emacs way to detect name of open file ?

M-X shell-commnad nl

I am not able find determine value of current open/buffer and substitute.
Thx/Mahesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攀登最高峰 2024-10-27 06:55:18

编辑:将您的问题误读为想要将该更改应用于您正在处理的文件。如果您只想对缓冲区运行 shell 命令,可以使用 shell-command-on-region,它通常绑定到 M-|。

如果您只是想访问特定的行号,Mx goto-line 就可以了。我通过将 (define-key global-map "\Cx\Cl" 'goto-line) 放入我的 ~/.emacs< 中,将其绑定到 Cx Cl /代码>。


尝试一下(在您的 ~/.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-# 来调用它。

EDIT: Misread your question as wanting to apply that change to the file you're working on. If you just want to run a shell command against a buffer, you can use shell-command-on-region, which is usually bound to M-|.

If you're just trying to get to a particular line number, M-x goto-line works. I bind that to C-x C-l by putting (define-key global-map "\C-x\C-l" 'goto-line) in my ~/.emacs.


Try this (in your ~/.emacs file):

;;; 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)

Invoke it by selecting a region you want to operate on and then doing M-#.

_畞蕅 2024-10-27 06:55:18

如果您始终希望为 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)))

添加上述代码后,Mx shell-command 将始终以缓冲区的文件名开头,因此您可以在命令中使用它。

If you always want the buffer's file name to be inserted for the shell command, you can use this advice:

(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)))

Once you've added the above code, M-x shell-command will always start with the buffer's file name, so you can use it in the command.

椒妓 2024-10-27 06:55:18

我用这个:

(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%

I use this:

(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)

Then you can do M-! nl %

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文