简体   繁体   English

Gnome-Terminal,如何在不同的目录下启动?

[英]Gnome-Terminal, how to start in a different directory?

Whenever I start my console gnome-terminal in Ubuntu, it starts in the home directory.每当我在 Ubuntu 中启动控制台gnome-terminal时,它都会在主目录中启动。 How can I make it start in a different directory say ~/myfolder ?我怎样才能让它在不同的目录中启动,比如~/myfolder I tried to write cd ~/myfolder in ~/.profile but nothing happens.我试图在~/.profile写入cd ~/myfolder但没有任何反应。

如果您像gnome-terminal --working-directory=myfolder一样启动 gnome-terminal,它将从~/myfolder的工作目录开始,这样您就可以在菜单中添加一个新条目以使用该命令而不是另一个。

I did this way - with script: open 3 tabs in the same window size 170x40, each "tab" starts in a different directory.我是这样做的 - 使用脚本:在相同的窗口大小 170x40 中打开 3 个选项卡,每个“选项卡”在不同的目录中开始。

gnome-terminal --geometry=170x40 --working-directory=myfolder1 \

--tab --working-directory=myfolder2 \

--tab --working-directory=myfolder3 

将以下内容添加到您的 ~/.bashrc

cd ~/myfolder

You could use the nautilus-open-terminal extension.您可以使用nautilus-open-terminal扩展。 This allows you to right-click on a folder in nautilus and open a terminal window with that directory as its working directory.这允许您右键单击 nautilus 中的文件夹并打开一个终端窗口,将该目录作为其工作目录。

You can also run a terminal in the normal way, type "cd ", and drag a folder icon from nautilus to the window.您也可以以正常方式运行终端,键入“cd”,然后将文件夹图标从 nautilus 拖到窗口中。 This will paste the path of the folder into the command line and you then type return to change to that directory.这会将文件夹的路径粘贴到命令行中,然后键入 return 以更改到该目录。 You can do the same thing with regular files to paste their path and run commands on them.您可以对常规文件执行相同的操作以粘贴其路径并在其上运行命令。

Actually, this is how I turn it off for everyone by default.实际上,这就是我默认为每个人关闭它的方式。

gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory/ \
--set -- type=bool /apps/nautilus-open-terminal/desktop_opens_home_dir true

Directory option目录选项

There is the option --working-directory to specify the startup directory of the terminal (no short option form).有选项--working-directory来指定终端的启动目录(没有简短的选项形式)。

The basic approach to open the terminal in /some/dir is/some/dir打开终端的基本方法是

gnome-terminal --working-directory=/some/dir

but there is a trap...但是有一个陷阱...

Bad trap坏陷阱

Assuming we want to start the terminal in the directory ~/dir .假设我们要在目录~/dir启动终端。

This does not work:这不起作用:

gnome-terminal --working-directory=~/dir

The command looks perfectly fine according to the option syntax, but the terminal starts in the home directory.根据选项语法,该命令看起来非常好,但终端在主目录中启动。
It's because it does not expand the tilde ( ~ ), for confusing reasons - see below.这是因为它没有扩展波浪号 ( ~ ),出于令人困惑的原因 - 见下文。

Thesse do work:这些确实有效:

gnome-terminal --working-directory=/home/auser/dir

gnome-terminal --working-directory=$HOME/dir

gnome-terminal --working-directory ~/dir

Tilde expansion波浪号扩展

Note there is no = in the last variant.请注意,最后一个变体中没有= Because of this, the ~ is at the start of a shell word, and therefore is handeled by tilde expansion.因此, ~位于 shell 单词的开头,因此由波浪号扩展处理。

The problem is that ~ does not get expanded everywhere, but only in certain places.问题是~并没有在所有地方扩展,而只是在某些地方。 One of them is in variable assignments, like directory=~/dir .其中之一是变量赋值,例如directory=~/dir That's ok, ~ gets expanded to $HOME , but --working-directory=~/dir does not expand ~ , because that is not a variable assignment, it only looks very similar.没关系, ~被扩展为$HOME ,但--working-directory=~/dir不会扩展~ ,因为这不是变量赋值,它看起来非常相似。

If the folder has a complicated path, ie not just ~/myfolder , you could create a symlink to it in your home directory so you can get to it quickly.如果文件夹有一个复杂的路径,即不仅仅是~/myfolder ,您可以在您的主目录中创建一个符号链接,以便您可以快速访问它。 You can also set the CDPATH environment variable to tell bash to search a list of directories when you type cd myfolder .您还可以设置CDPATH环境变量以告诉 bash 在您键入cd myfolder时搜索目录列表。

I'm impressed by Neil's Mayhew comment and Volker's Siegel answer . Neil 的 Mayhew 评论Volker 的 Siegel 回答给我留下了深刻的印象。 I've tried to not only set default directory for gnome-terminal but preserve habitual behavior of desktop environment as well (I'm using Linux Mint 17.1 Cinnamon , GNOME Terminal 3.6.2 , perhaps it also can be applied for other Gnome-congenered DEs).我尝试不仅为gnome-terminal设置默认目录,而且还保留桌面环境的习惯行为(我使用的是Linux Mint 17.1 CinnamonGNOME Terminal 3.6.2 ,也许它也可以应用于其他 Gnome-congenered DE)。 So let me put my two cents in.所以让我把我的两分钱放进去。

  1. Adding cd ~/myfolder at the very end of ~/.bashrc does the job.~/.bashrc最后添加cd ~/myfolder可以完成这项工作。 But as already mentioned it will affect every interactive shell.但正如已经提到的,它会影响每个交互式 shell。 Even more, if you open some directory in a file manager ( Nemo or Nautilus or something like this) and appeal to the context menu from there (eg right click and then select Open in Terminal ) new instance of gnome-terminal will be started in ~/myfolder regardless of the folder which was loaded in the file manager.更重要的是,如果您在文件管理器( NemoNautilus或类似的东西)中打开某个目录并从那里调用上下文菜单(例如右键单击然后选择在终端中打开)新的gnome-terminal实例将在~/myfolder与文件管理器中加载的文件夹无关。 Even if you run gnome-terminal --working-directory=/some/other/folder explicitly it will still open ~/myfolder .即使您明确运行gnome-terminal --working-directory=/some/other/folder它仍然会打开~/myfolder Seems that the approach with .bashrc is unusable.似乎.bashrc的方法不可用。

  2. gnome-terminal --working-directory=myfolder works fine but only when you use custom menu entry in you DE (or custom shortcut on desktop) which runs terminal with this parameter. gnome-terminal --working-directory=myfolder工作正常,但仅当您在使用此参数运行终端的 DE(或桌面上的自定义快捷方式)中使用自定义菜单条目时。 If you would like to run gnome-terminal from command line or from mini-launcher (press Alt + F2 ), you have to type the parameter every time.如果您想从命令行或迷你启动器(按Alt + F2 )运行gnome-terminal ,则必须每次都输入参数。 Anyway this approach is more-or-less usable.无论如何,这种方法或多或少是有用的。


How gnome-terminal determines which folder to open? gnome-terminal如何确定要打开的文件夹? When --working-directory is not specified it opens current working directory (eg $PWD ) otherwise it opens directory specified explicitly.--working-directory未指定时,它会打开当前工作目录(例如$PWD ),否则它会打开明确指定的目录。

I've found the following solution.我找到了以下解决方案。

Create a file named gnome-terminal in your ~/bin folder.~/bin文件夹中创建一个名为gnome-terminal文件。 It will act as shortcut but from everywhere (start menu, mini-launcher, other terminal instance, etc) because ~/bin is already in $PATH (at least in Linux Mint...).它将作为快捷方式,但来自任何地方(开始菜单、迷你启动器、其他终端实例等),因为~/bin已经在$PATH (至少在 Linux Mint 中...)。 Make this file executable.使这个文件可执行。 Then put the following content into the script:然后将以下内容放入脚本中:

#!/bin/bash

home_directory=~

if [ "$PWD" == "$home_directory" ]; then
    # When 'gnome-terminal' was ran from either 
    #     - start menu
    #     - mini-launcher
    #     ... 
    # parent directory is set to $HOME.
    #
    # We respect original command line arguments. 
    # For example, when terminal is ran from another 
    # terminal instance and '--working-directory' is 
    # specified explicitly we should left it as it is.  
    # If there are two '--working-directory' switches 
    # in the command line 'gnome-terminal' will pick up 
    # the last one.
    #
    # Also we use full path to executable here in order
    # to prevent recursive calling of 'gnome-terminal' from 
    # '~/bin'.
    /usr/bin/gnome-terminal --working-directory=/ "$@" 
else
    # 'gnome-terminal' was from another directory. 
    # We don't change anything.
    /usr/bin/gnome-terminal "$@"
fi

If you run gnome-terminal from start menu you will see that current folder in new terminal is / (you can use any folder, for example, ~/myfolder because our custom wrapper is a Bash-script, so shell's expansion with work fine).如果您从开始菜单运行gnome-terminal ,您将看到新终端中的当前文件夹是/ (您可以使用任何文件夹,例如~/myfolder因为我们的自定义包装器是一个 Bash 脚本,所以 shell 的扩展工作正常) .

If you appeal to Open in Terminal in a file manager you will get current directory in the new terminal.如果您在文件管理器中呼吁在终端打开,您将在新终端中获得当前目录。

If you run gnome-terminal with explicit --working-directory parameter (perhaps, from existing terminal instance) new terminal instance will be opened in the directory you specified.如果您使用显式--working-directory参数(可能来自现有终端实例)运行gnome-terminal ,新终端实例将在您指定的目录中打开。

要以 root 用户身份在我想要的目录中打开,我运行了:

gnome-terminal --working-directory=/home/my-project/ -x bash -c "sudo su"

From GNOME Terminal - Getting Started :GNOME 终端 - 入门

You can also specify a command that runs automatically when you start GNOME Terminal in the profile.您还可以指定在配置文件中启动 GNOME 终端时自动运行的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM