根据PWD更新多项缓冲区名称 [英] Update multi-term buffer name based on PWD

查看:102
本文介绍了根据PWD更新多项缓冲区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用konsole或其他终端,终端标签名称可以根据PWD而改变。但是在多项目中,缓冲区名称为 * terminal< number> * 。这不是很好因为当我们在他们之间切换时,名字不是很丰富。所以我想根据PWD重命名它。

If I use konsole or other terminal, the terminal tag name can change based on PWD. But in multi-term, the buffer name is *terminal<number>*. This is not very nice. Because when I switch between them, the name is not very informative. So I want to rename it based on PWD.

我发现输入密钥绑定到term-send-raw,所以我写一个函数

I find that the Enter key is bind to term-send-raw, so I write a function

(defadvice term-send-raw (around rename-term-name activate)
  (progn
    (rename-buffer 
     (concat "⇒ " 
             (shell-command-to-string "pwd | xargs basename |  tr -d '\n'") 
             (format-time-string " [%M ∞ %S]")))
    ad-do-it))

但问题是 pwd 命令返回终端缓冲区的 PWD ,而不是该终端中 SHELL PWD

But the problem is pwd command return the PWD of the terminal buffer, while it is not the PWD of the SHELL in that terminal.

终端缓冲区的 PWD defcustom multi-term-default-dir 设置。 SHELL 中的 PWD 更改时不会更改。

The PWD of the terminal buffer is set by defcustom multi-term-default-dir. And it does not change when the PWD change in the SHELL.

(defcustom multi-term-default-dir "~/"
  "The default directory for terms if current directory doesn't exist."
  :type 'string
  :group 'multi-term)

如何获得PWD SHELL在终端?

How can I get the PWD of the SHELL in the terminal?

请问。

推荐答案

AFAIK没有简单的方法来检索来自正在运行的进程的信息。

AFAIK there is no easy way to retrieve information from a running process.

但是,如果要获取当前目录,可以:

But if you want to get the current directory you could:


  1. 请求shell打印

  2. 解析并跟踪cd,pushd,popd等功能的命令行。

  3. / proc / PID / cwd

第一种方法在 term.el Mx find-library RET 术语

The first method is described in the header of term.el (M-xfind-libraryRETtermRET).

现在,感谢你的问题,你给了我机会这样做:

And now, thank you for your question, you gave me the opportunity to do this:

(defadvice term-send-input (after update-current-directory)
  (let* ((pid (process-id (get-buffer-process (current-buffer))))
         (cwd (file-truename (format "/proc/%d/cwd" pid))))
    (cd cwd)))

(ad-activate 'term-send-input)

这是第三种方法的天真实现,如果用户使用 su ssh 。但是,我不知道是否可以使用第一种或第二种方法。

It's a naive implementation of the third method and it doesn't work if the user uses su or ssh. However, I don't know if it's possible withouth using the first or the second method.

在你的情况下,你可以替换 cd 命令与任何你想要的。

In your case, you can just replace the cd command with whatever you want.

这篇关于根据PWD更新多项缓冲区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆