AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 30988
Accepted
Kit Sunde
Kit Sunde
Asked: 2011-03-19 19:04:49 +0800 CST2011-03-19 19:04:49 +0800 CST 2011-03-19 19:04:49 +0800 CST

如何从命令行设置活动 gnome 终端的标题?[复制]

  • 772
这个问题在这里已经有了答案:
如何更改 Gnome 终端标题? (17 个回答)
8年前关闭。

有没有办法从终端本身设置 gnome-terminal 标题,而无需右键单击选项卡。就像是:

active-terminal --title "Foo"

之前有一个相关问题的答案几乎可以让您这样做:如何更改 Gnome-Terminal 标题?但这并没有将 gnome-terminal 选项卡标题设置为仅窗口标题。

command-line
  • 3 3 个回答
  • 40506 Views

3 个回答

  • Voted
  1. Best Answer
    J. Taylor
    2011-03-19T21:37:50+08:002011-03-19T21:37:50+08:00

    以下将终端的标题设置为“新终端标题”:

    echo -en "\033]0;New terminal title\a"
    

    不过,您可能还必须首先更改环境变量 PS1,否则您的更改将不会显示,因为它会在每个命令后重置标题。Ubuntu 附带的默认 .bashrc 包含以下行:

    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    

    ... "\e]0;" 代码告诉它在标题和图标名称属性中将所有内容写入“\ a”。您需要将其删除并将其设置为类似这样(即没有 \e]0; 代码):

    PS1="${debian_chroot:+($debian_chroot)}\u@\h \w\a$ "
    

    然后,您使用上述 echo 命令所做的任何更改都将更改终端标题。如果您要经常使用它,可以将它放入 ~/.bashrc 文件中的函数中:

    set_term_title(){
       echo -en "\033]0;$1\a"
    }
    

    然后,您可以通过执行以下操作从命令行将标题设置为“小猫”:

    set_term_title kittens
    

    (您必须在编辑 .bashrc 后重新启动 bash,才能使您的更改生效)

    • 47
  2. Damian Green
    2013-05-22T19:43:36+08:002013-05-22T19:43:36+08:00

    函数声明并非在所有 shell 中都有效,因此另一种方法是声明一个别名,如下所示:

    alias cd    'cd \!*; echo -en "\033]0;`pwd`\a"'
    

    该命令尤其会导致标题更改为 pwd 是什么。

    当然你会想要在启动终端时初始化标题,所以不要忘记包含 gnome-terminal --title。

    我使用 perl 脚本来帮助我首先确定所有参数值,然后它调用一个新的终端,如下所示:

    my $cmd = "gnome-terminal $window_with_profile $geometry $zoom $working_directory $temp_argv $title &";
    system($cmd);
    

    但是您要如何初始化这些值取决于您...

    如果您愿意,欢迎您使用以下代码并对其进行调整以供您个人使用:

        #!/usr/bin/perl
        #use strict;
        use Getopt::Long;
    
        my $progName = "term.pl";
    
        =pod
    
        =head1 OBJECTIVE: open a gnome-terminal with a given path and a new background color
    
         #1# In order to use this script you first need to set up 10 different terminal profiles each named "theme1" - "theme10"
            Edit... profiles... new... theme1
            Each theme should use a different color scheme...
    
         The themes are later called with --window-with-profile=theme$int
         This script then selects the next one one to open based on the number saved in the ~/.term_theme_counter file.
    
         ### The argument "." opens the terminal with the same dir as you are currently in. 
             Without it, the terminal opens to "~". Use --working-directory=<DIR> for others. 
             Also, -dir=<DIR> has been added for convenience
    
         ### You can still pass additional gnome-terminal arguments like: --tab_with_profile etc into the command 
    
         ### Also see gnome-terminal --help 
                and gconf-editor and gconftool-2  --> /apps/gnome-terminal/profiles/ 
                for editing terminal props
    
          EXAMPLES:
    
          term.pl .
          term.pl /cadtools/tech/
    
        Credits: This script was written by Damian Green over the years but first posted publicly in 2013   
    
        =cut
    
        sub usage{
            system("perldoc $progName");
        };
    
        my $opt_h = "";
        my $geometry = "";
        my $zoom = "";
        my $window_with_profile = "";
        my $working_directory = "";
        my $temp_argv = " @ARGV ";
    
        #my $counter = int(rand(10));
        ###lets keep a running counter instead
        my $counter = 0;
    
        my $home = $ENV{"HOME"};
        $home = abs_path($home);
    
        my $counter_file = "$home/.term_theme_counter";
        if (-f $counter_file){
            open (INFILE, "< $counter_file");
            my @contents = <INFILE>;
            close INFILE;
            $counter = @contents[0];
        }else{
            open (OUTFILE, "> $counter_file");
            print OUTFILE $counter; 
            close OUTFILE;
        }
    
        $counter++;
        if ($counter > 10){
            $counter = 1;
        }   
            open (OUTFILE, "> $counter_file");
            print OUTFILE "$counter\n";
            close OUTFILE;
    
        use Cwd 'abs_path';
        my $pwd = abs_path();#expands /cadtools to /data/mmc/emc/cadtools_lnx/cadtoolsmy 
        my $title_path = ""; 
    
        ### first of all pull out the "." if there is one...
        if ($temp_argv =~ m/(\s+)(\.)(\s+)/){
            my $arg = $1.$2.$3;
            my $val = $2;
            $temp_argv =~s/\Q$arg\E/ /;                     #<- remove the arg from the temp_argv
            unless ($temp_argv =~ m/--working_directory/){
                $working_directory = "--working-directory=$pwd";#<- #<- set the new working dir
            }
            $title_path = $pwd;
        #}elsif ($temp_argv =~ m/(\s+)(\S+)(\s+)/ and -d $2){
        }elsif ($temp_argv =~ m/(\s+)((?!-)\S+)(\s+)/ and -d $2){
            my $arg = $1.$2.$3;
            my $val = $2;
            $val = abs_path($val);
            $temp_argv =~s/\Q$arg\E/ /; 
            unless ($temp_argv =~ m/--working_directory/){
                $working_directory = "--working-directory=$val";
            }
            $title_path = $val;
        }elsif ($temp_argv =~ m/(\s+)(--?dir=)(\S+)(\s+)/ and -d $3){# and -d $2){
            my $arg = $1.$2.$3.$4;
            my $val = $3;
            $val = abs_path($val);
            $temp_argv =~s/\Q$arg\E/ /; 
            unless ($temp_argv =~ m/--working_directory/){
                $working_directory = "--working-directory=$val";
            }
            $title_path = $val;
        }elsif($temp_argv !~ m/--working_directory/){
            $working_directory = "--working-directory=$home";
            $title_path = "$home";
        }
    
        if($temp_argv =~ m/(\s+)(--?geometry=)(\S+)(\s+)/){
            $geometry = $3;
            my $arg = $1.$2.$3.$4;
            $temp_argv =~s/\Q$arg\E/ /; 
        }
        if($temp_argv =~ m/(\s+)(--?window-with-profile=)(\S+)(\s+)/){
            $window_with_profile = $3;
            my $arg = $1.$2.$3.$4;
            $temp_argv =~s/\Q$arg\E/ /; 
        }
        if($temp_argv =~ m/(\s+)(--?zoom=)(\S+)(\s+)/){
            $zoom = $3;
            my $arg = $1.$2.$3.$4;
            $temp_argv =~s/\Q$arg\E/ /; 
        }
        if($temp_argv =~ m/(\s+)(--?h)(elp)?(\s+)/){
            &usage(); exit;
        }
    
        if (!$geometry){
            $geometry = "--geometry=150x30+180+500";
        }else{
            $geometry = "--geometry=$geometry";
        }
        if (!$zoom){
            $zoom = "--zoom=1";
            ### some machines have a small zoom by default and so you can adjust it here for different machines if you want.
        }else{
            $zoom = "--zoom=$zoom";
        }
        if (!$window_with_profile){
            ### if gnome themes arent working on your machine, you may have to comment the following line out...
            $window_with_profile = "--window-with-profile=theme$counter";
        }else{
            $window_with_profile = "--window-with-profile=$window_with_profile";
        }
    
        my $title = "--title=$title_path";
    
        my $cmd = "gnome-terminal $window_with_profile $geometry $zoom $working_directory $temp_argv $title &"; #--sm-client-id=greend12
    
        print "$cmd\n";
        system($cmd);
    
    • 0
  3. ziulfer
    2013-06-14T06:43:56+08:002013-06-14T06:43:56+08:00

    在 Ubuntu 12.10(我不确定以前的版本)中,默认 .bashrc 中有以下行:

    If this is an xterm set the title to user@host:dir
     case "$TERM" in
     xterm*|rxvt*)
     PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
      *)
    ;;
    esac
    

    因此,要获得所需形式的标题,您只需编辑PS1此部分的值。例如,如果您想将标题作为当前目录的名称,只需 更改\u@\h: 为\w\W

    • -1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve