如何打开多个终端标签,并在Vim中打开文件并更改目录?

我问这个问题是因为我尝试了很多我找到的解决方案,但它们都没有起作用。
  • gnome-terminal --save-config=... 的结果是一个错误,这个选项已经不再可用

  • gnome-terminal --tab -- 'vim ~/Files/file.txt' 用于创建一个shell脚本会导致一个错误,我的系统是德语的,但似乎是这样的:

    创建此终端的子进程时出错
    无法执行子进程 "vim ~/Files/file.txt"(没有该文件或目录)
    

    而且什么都不做,只使用vim而不打开任何文件是可以工作的

  • 使用会话管理器只会自动打开终端,但没有任何标签页

  • gnome-terminal --tab -- 'cd ~/Files/' 也不起作用(与上面的错误类似)

版本是# GNOME Terminal 3.28.2 使用 VTE 0.52.2 +GNUTLS -PCRE2。 也许我在解决问题的方法上有错误,或者你知道另一种(更好的)替代方案吗?
谢谢你的帮助!

1谢谢你提供的链接。我希望我理解得没错,但是在.bashrc中设置变量并输入gnome-terminal --tab -- 'bash -c "export BASH_POST_RC=\"vim todo.txt\"; exec bash"'也不起作用。 - TravelTrader
不,我认为你应该在你的~/.bashrc文件的末尾添加一行eval "$BASH_POST_RC"(字面上!),然后运行例如:gnome-terminal --tab -e 'bash -c "export BASH_POST_RC=\"vim todo.txt\"; exec bash"' --working-directory="$HOME/Files" --tab -e 'exec bash' 对于最后一个 -e 'exec bash'我不太确定,可以尝试带和不带它。 - dessert
只有最后一句话,它会打开两个标签页,一个显示工作目录,一个显示之前的错误。如果没有最后一句话,则只会打开一个显示目录的标签页。 - TravelTrader
谢谢。我会去了解一下《终结者》。好提示。gnome-terminal的另一个版本不是解决我的愿望的可能性吗? - TravelTrader
2个回答

最接近我能找到的方法是创建多个脚本。gnome-terminal在多年来发生了变化,-x和--command选项已经被大量弃用。你最好为每个要运行的标签创建配置文件,并使用:
gnome-terminal --tab-with-profile=Something1 --tab-with-profile=Something2

如果你在命令中使用像~*这样的内容,很可能这些内容将不会被评估,因为这实际上是shell(bash)的工作,而不是gnome-terminal的工作。所以在某个地方创建一个实际的脚本并告诉gnome-terminal使用它可能会有所帮助。


使用 /home/username/ 替代 ~/ ,它会起作用。谢谢。也许有一个更灵活的解决方案,因为需要为不同的终端布局使用大量的配置文件。 - TravelTrader

这个脚本可以打开新的标签页:

  1. 将路径列表(foo-test)保存在pathtabs.list中;
  2. 创建一个名为autotab.sh的新脚本,用于使用controlshiftt快捷键和cd命令来格式化任何路径列表;
  3. autotab.sh对于xdotool格式化<any-list-of-paths>非常有用;
  4. 用法:autotab.sh <any-list-of-paths>
  5. pathtabs.list创建foo_*测试目录;
  6. 运行./autotab.sh pathtabs.list并将输出保存到新脚本maketabs.sh
  7. maketabs.sh对于打开与原始pathtabs.list中相同的一组标签页/路径非常有用;

...并执行命令:

8. 保存在`pathncommtabs.list`中的(foo-test)路径和要在新终端选项卡中运行的命令列表; 9. 创建一个名为`autopctab.sh`的新脚本,用于将任何路径和命令列表以`xdotool`-FORMAT格式添加controlshiftt快捷键、`;`和`cd`到命令中; 10. `autopctab.sh`对于`xdotool`格式化``(参见`pathncommtabs.list`)很有用; 11. 用法:`autopctab.sh `;(请参阅`pathncommtabs.list`) 12. 运行`./autopctab.sh pathncommtabs.list`并将命令列表保存到新脚本`makepctabs.sh` 13. `makepctabs.sh`对于从原始的`pathncommtabs.list`打开相同的选项卡/路径/命令集很有用;
#open-several-tabs-with-xdotool-slowly-and-easily
#!/bin/bash
#pathtabs.list
if [ ! -f ~/pathtabs.list ]; then
echo ~/foo_00 > pathtabs.list
echo ~/foo_00/foo_00_1 >> pathtabs.list
echo ~/foo_00/foo_00_2 >> pathtabs.list
echo ~/foo_00/foo_00_2/foo_00_2_1 >> pathtabs.list
fi
echo 'pathtabs.list: has a list of paths for the new tabs'

#mkdirs
if [ ! -d ~/foo_00/ ]; then
mkdir foo_00
mkdir foo_00/foo_00_1
mkdir foo_00/foo_00_2
mkdir foo_00/foo_00_2/foo_00_2_1
fi
echo 'mkdirs: testing foo_*' 

##commtabs.list
if [ ! -f ~/commtabs.list ]; then
echo nano newfile1.txt > commtabs.list
echo nano newfile2.txt >> commtabs.list
echo nano newfile3.txt >> commtabs.list
echo nano newfile4.txt >> commtabs.list
fi
echo 'commtabs.list: has a list of commands to run on the new tabs'

#pathtabs.list+commtabs.list+options
if [ ! -f ~/pathncommtabs.list ]; then
cat pathtabs.list |sed 's/.*/cd &;/'|sed '1s/^/c+s+t;/'|\
paste - commtabs.list|tr -d '\t' |sed '$!s/$/;c+s+t/' > pathncommtabs.list
fi
echo 'pathncommtabs.list: integrates pathtabs.list+commtabs.list+cd+;+(new-tab-tokens)'

#autopctab.sh <anypathncommtabs.list>
if [ ! -f ~/autopctab.sh ]; then
echo '#!/bin/bash' > autopctab.sh
echo 'cat $1| sed '\''s/ /#/g'\''| sed '\''s/\(.\)/\1 /g'\''|sed '\''s/#/space/g'\''|\
    sed '\''s/~/asciitilde/g'\''|sed '\''s/_/underscore/g'\''|sed '\''s/c + s + t/control+shift+t/g'\''|\
    sed '\''s/\//slash/g'\''|sed '\''s/\./period/g'\''|sed '\''s/;/Return/g'\''|\
    sed '\''s/.*/xdotool key --delay 18  & Return/'\''' >> autopctab.sh
chmod u+x autopctab.sh
fi
echo 'autopctab.sh: xdotool-format paths+commands+(xdotool-commands)'

#run autopctab.sh <pathncommtabs.list>
#makepctabs.sh
if [ ! -f ~/makepctabs.sh ]; then
echo '#!/bin/bash' > makepctabs.sh
./autopctab.sh pathncommtabs.list >> makepctabs.sh
chmod u+x makepctabs.sh
fi
echo 'makepctabs.sh: run xdotool command lines to OPEN new tabs AND RUN commands with pathncommtabs.list'

#autotab.sh <anytabs.list>
if [ ! -f ~/autotab.sh ]; then
echo '#!/bin/bash' > autotab.sh
echo 'cat $1|sed '\''s/.*/cd &/'\''|sed '\''s/ /#/g'\''|\
    sed '\''s/\(.\)/\1 /g'\''|sed '\''s/#/space/g'\''|\
    sed '\''s/~/asciitilde/g'\''|sed '\''s/_/underscore/g'\''|\
    sed '\''s/\//slash/g'\''|sed '\''s/\./period/g'\''|\
    sed '\''s/.*/xdotool key --delay 18 control+shift+t space c d space asciitilde Return & Return c l e a r Return/'\'''  >> autotab.sh
chmod u+x autotab.sh
fi
echo 'autotab.sh: xdotool-format ONLY paths+(xdotool-commands)'

#run autotab.sh <pathtabs.list>
#maketabs.sh
if [ ! -f ~/maketabs.sh ]; then
echo '#!/bin/bash' > maketabs.sh
./autotab.sh pathtabs.list >> maketabs.sh
chmod u+x maketabs.sh
fi
echo 'maketabs.sh: run xdotool command lines ONLY to open new tabs with pathtabs.list'

#run-maketabs?
#./maketabs.sh

#remove-maketabs.sh?

#remove-autotab.sh?
#rm autotab.sh

#remove-pathtabs.list?
#rm pathtabs.list

#run-makepctabs?
#./makepctabs.sh

#remove-makepctabs.sh?
#rm makepctabs.sh

#remove-autopctab.sh?
#rm autopctab.sh

#remove-pathncommtabs.list?
#rm pathncommtabs.list

#remove-dirs?
#rm -r foo_0?

#echo See also 'https://askubuntu.com/a/380320'
#echo '        https://www.semicomplete.com/projects/xdotool/'
#echo '        https://github.com/jordansissel/xdotool'