Bash PS1设置-如何获取当前文件夹作为终端标题 [英] Bash PS1 settings - how to get the current folder back as the terminal title

查看:166
本文介绍了Bash PS1设置-如何获取当前文件夹作为终端标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将这些行添加到我的〜/.bashrc文件中,以显示当前分支(如果我位于git工作文件夹中),并且很好地实现了这一点.但是,我丢失的是当前的文件夹名称曾经显示在我打开的终端的选项卡中,而现在却没有:它始终只是显示"Terminal"(终端).我可以找回并仍然保留git的东西吗?这是有问题的行-这是问题的第二行,因为仅注释掉第二行就可以解决问题.

I recently added these lines to my ~/.bashrc file to show the current branch if i'm in a git working folder, and it works nicely for that. However, what i've lost is that the current folder name used to be shown in the tab for the terminal i have open, and now it isn't: it always just says 'Terminal'. Can i get that back and still keep the git stuff? Here's the lines in question - it's the second one that's the issue, as commenting out just the second line fixes the problem.

source /etc/bash_completion.d/git
PS1='\h:\w$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ '

我一直在查看PS1选项的说明,但其中看不到任何有关终端窗口标题的信息.有人可以建议吗?谢谢,最大

I've been looking at explanations of the options for PS1 but can't see anything about the terminal window's title in there. Can anyone advise? thanks, max

编辑

我实际上已经在操纵PS1以便拥有格式为

I actually manipulate PS1 already in order to have a terminal with the format

<rvm version and gemset> <computer name> <current folder> <git branch>

,每个部分都有不同的颜色,但是我之前从未真正看过文档,因此感谢您提供的链接.我当前的PS1设置是

, with each part in a different color, but i've never actually seen the docs before, so thanks for the link to that. My current PS1 setting is

\[\033[0;31m\]$(__my_rvm_ruby_version)\[\033[0;33m\]\h\[\033[0;37m\]:\[\033[1;33m\]\W\[\033[1;32m\]$(__git_branch)\[\033[1;32m\]$(__git_dirty) \[\033[0;37m\]$

大概我可以做类似的事情

Presumably i can do something like

export "<something> $PS1"

在不丢失现有设置的情况下设置我的终端标签名称.我一直在摸索,但没有设法做到这一点.

to set my terminal tab name without losing my existing settings. I've been poking around with this though and not managed to do it.

编辑-在以下一些答案的帮助下找到了答案-谢谢大家!我把它包装在一个shell脚本中

EDIT - figured this out with the help of some of the answers below - thanks all! I wrapped it up in a shell script

#!/usr/bin/env bash
#renames the current terminal tab via the PS1 env var
source ~/.bashrc
export PS1="$PS1""\[\e]0;$1 \a\]"

它叫做"renametab",所以我现在可以用例如

it's called "renametab" so i can now call it with eg

source renametab mytabname

需要源"来将更改导出到当前的shell中:如果我只是执行renametab mytabname,则导出将进入一个子shell,该子shell在脚本完成时被杀死.

"source" is needed to export the changes into the current shell: if i just do renametab mytabname the export just goes into a subshell which is killed when the script finishes.

再次感谢您的帮助!

推荐答案

这是我在Ubuntu上默认使用的有关终端标题的内容:

That's what I have as default on my Ubuntu concerning the terminal's title:

PS1='\[\e]0;\u@\h: \w\a\]'

为此PS1加上一个就可以了

Prepend your PS1 with this one and it should be fine

这篇关于Bash PS1设置-如何获取当前文件夹作为终端标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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