【问题标题】:Emacs equivalent to VIM's `%`?Emacs 相当于 VIM 的 `%`?
【发布时间】:2012-06-29 15:40:29
【问题描述】:

在 VIM 中,调用 shell 命令时可以使用% 来指示当前文件名。任何人都可以指出我在文档的方向上显示 emacs 中的等价物吗?

【问题讨论】:

标签: shell vim emacs


【解决方案1】:

没有。但这是 Emacs!所以在这里:

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

【讨论】:

    【解决方案2】:

    你可以在 minibuffer 期望你输入一些东西时使用它(警告:不适用于 ido,但显然你总是可以用例如 C-x C-f 摆脱它)。您也可以在常规缓冲区中使用它。

    (defun insert-filename-or-buffername (&optional arg)
      "If the buffer has a file, insert the base name of that file.
      Otherwise insert the buffer name.  With prefix argument, insert the full file name."
      (interactive "P")
      (let* ((buffer (window-buffer (minibuffer-selected-window)))
             (file-path-maybe (buffer-file-name buffer)))
        (insert (if file-path-maybe
                    (if arg
                        file-path-maybe
                      (file-name-nondirectory file-path-maybe))
                  (buffer-name buffer)))))
    
    (define-key minibuffer-local-map (kbd "C-c f") 'insert-filename-or-buffername)
    

    【讨论】:

      【解决方案3】:

      在我的例子中,使用来自 homebrew 的 emacs 24 gui 版本......我将文件名视为(次要)模式栏左下角的第三个项目,就在迷你缓冲区的上方。

      要查看我在哪里,使用 ido-mode 我只需要 C-x C-f 那里不需要配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-03
        • 2010-10-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多