【问题标题】:emacs equivalent of following vi commandemacs 等效于以下 vi 命令
【发布时间】:2011-07-09 19:36:47
【问题描述】:

我正在寻找以下 vi 命令的等效项
:! %
这在当前打开的文件上运行 nl 命令
什么是emacs检测打开文件名的方法?
M-X shell-commnad nl

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

【问题讨论】:

    标签: emacs vi


    【解决方案1】:

    我用这个:

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

    【讨论】:

      【解决方案2】:

      如果您总是希望为 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 将始终以缓冲区的文件名开头,因此您可以在命令中使用它。

      【讨论】:

        【解决方案3】:

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

        如果您只是想获取特定的行号,M-x goto-line 可以。我通过将(define-key global-map "\C-x\C-l" 'goto-line) 放入我的~/.emacs 将其绑定到C-x C-l


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

        【讨论】:

        • 虽然您的编辑确实有效,但问题是如何以编程方式提供与缓冲区对应的文件名作为 shell 命令的参数。
        • 这里是:stackoverflow.com/questions/455345。请注意,除非您事先将其指向save-buffer,否则无法保证磁盘上的文件与缓冲区中的文件匹配。 (与vi 相同。)
        猜你喜欢
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        • 2012-08-29
        • 2012-12-03
        相关资源
        最近更新 更多