如何通过命令行为 Gnome 终端创建新配置文件?

ale*_*dam 19 command-line gnome-terminal

众所周知,您可以通过菜单创建一个新的配置文件,在该菜单中您会被询问哪个现有配置文件应该是您的新配置文件的父级等等。但是我应该如何通过命令行创建一个新的配置文件?

我应该通过 gconftool 读取默认配置文件中的每个现有值并以新名称重新设置它们还是有更好的解决方案?如果答案是肯定的:我需要注意新的个人资料名称吗?新的总是叫Profile0Profile1Profile2等。

Bra*_*iam 10

对于 Gnome 终端 < 3.8

您不能创建新的配置文件,但可以转储当前配置,使用gconftool-2、修改它并加载它。

gconftool-2 --dump '/apps/gnome-terminal' > gnome-terminal-conf.xml
## Modify the file here.
gconftool-2 --load gnome-terminal-conf.xml
Run Code Online (Sandbox Code Playgroud)

请记住,它只返回非默认值(或 gconf 检测为非默认值的值),因此生成的文件可能不完整。


小智 10

对于GNOME 终端 >= 3.8,要通过 cli 创建/编辑/读取配置文件,您可以使用dconf-cligsettings。我的选择是dconf-cli

GNOME 终端的 dconf 目录是 /org/gnome/terminal/legacy/profiles:. 所有操作都发生在这个目录中。我将它存储在$dconfdir下面的脚本中。

创建一个新的配置文件

最小步骤是

  1. 通过运行命令为配置文件生成 UUID uuidgen
  2. 将其附加到listdconf write "$dconfdir/list" "[..., 'UUID']"
  3. 设置其visible-namedconf write "$dconfdir/:UUID"/visible-name "'NAME'"

之后,即使许多设置没有设置,终端的 GUI 设置中也会显示一个新的配置文件,以便您可以通过 GUI 编辑设置。

一个工作脚本:

#!/bin/bash
dconfdir=/org/gnome/terminal/legacy/profiles:

create_new_profile() {
    local profile_ids=($(dconf list $dconfdir/ | grep ^: |\
                        sed 's/\///g' | sed 's/://g'))
    local profile_name="$1"
    local profile_ids_old="$(dconf read "$dconfdir"/list | tr -d "]")"
    local profile_id="$(uuidgen)"

    [ -z "$profile_ids_old" ] && local profile_ids_old="["  # if there's no `list` key
    [ ${#profile_ids[@]} -gt 0 ] && local delimiter=,  # if the list is empty
    dconf write $dconfdir/list \
        "${profile_ids_old}${delimiter} '$profile_id']"
    dconf write "$dconfdir/:$profile_id"/visible-name "'$profile_name'"
    echo $profile_id
}

# Create profile
id=$(create_new_profile TEST)
Run Code Online (Sandbox Code Playgroud)

小心你写的值周围的引号。正如手册中所说,

设置密钥时,还需要指定一个VALUE. 该值的格式是序列化 GVariant 的格式,因此例如字符串必须包含显式引号:"'foo'". 打印值时也使用此格式。

如果需要,您可以通过 cli 设置配置文件的更多选项。跑

dconf write /org/gnome/terminal/legacy/profiles:/:UUID/KEY "'NAME'"
Run Code Online (Sandbox Code Playgroud)

设置。您可以使用dconf-editor来检查可用的选项。导航到类似的路径 /org/gnome/terminal/legacy/profiles:/:9ca4ab84-42f2-4acf-8aa9-50e6351b209a/。最好检查一个设置了许多选项的旧配置文件。

复制个人资料

您可以dconf dump将旧的配置文件和load现有的配置文件。因此,要复制配置文件,您需要使用上述步骤创建一个新配置文件,并复制旧配置文件以覆盖它。记住在覆盖后重命名它。

一个工作脚本:

dconf write /org/gnome/terminal/legacy/profiles:/:UUID/KEY "'NAME'"
Run Code Online (Sandbox Code Playgroud)

要按名称获取配置文件的 UUID:

# ... codes from last script

in_array() {
  local e
  for e in "${@:2}"; do [[ $e == $1 ]] && return 0; done
  return 1
}

duplicate_profile() {
    local from_profile_id="$1"; shift
    local to_profile_name="$1"; shift
    local profile_ids=($(dconf list $dconfdir/ | grep ^: |\
                        sed 's/\///g' | sed 's/://g'))

    # If UUID doesn't exist, abort
    in_array "$from_profile_id" "${profile_ids[@]}" || return 1
    # Create a new profile
    local id=$(create_new_profile "$to_profile_name")
    # Copy an old profile and write it to the new
    dconf dump "$dconfdir/:$from_profile_id/" \
        | dconf load "$dconfdir/:$id/"
    # Rename
    dconf write "$dconfdir/:$id"/visible-name "'$to_profile_name'"
}

# Create a profile from an existing one
duplicate_profile $id TEST1
Run Code Online (Sandbox Code Playgroud)

将配置文件设置为默认值

只需将配置文件的 UUID 写入 key default

dconf write $dconfdir/default "'$UUID'"
Run Code Online (Sandbox Code Playgroud)

参考

  • 我们绝对必须赞成这个答案,因为这是最正确和最新的答案!我自己搜索了一段时间,设置了`dconf watch /`,终于知道发生了什么,答案与@joegnis 所写的完全一样。只需创建一个 UUID,将其写入数据库并设置 `visible-name` 和 `/list`。 (2认同)

小智 3

# 找出有多少个 pofile - 一开始只有 1 个 - 默认
profile_list=$(gconftool-2 --get "/apps/gnome-terminal/global/profile_list" | sed "s|\[||;s|\]||;")
echo "1 个配置文件列表:" ${profiles_list}
last_profile=$(echo "${profiles_list}" | sed "s/^.*,//" | sed 's/Profile//')
echo "最后的个人资料名称/编号: " ${last_profile}

# 如果只有默认值或最后一个加 1,则将“ProfileX”X 数字设置为 0
if [ ${last_profile} == "默认"]; 然后
    下一个个人资料编号=0;
echo "1 个新的个人资料编号:" ${next_profile_number}
别的
    next_profile_number=$(( ${last_profile} + 1 ));
echo "2 新的个人资料编号:" ${next_profile_number}
菲
echo "新个人资料编号:" ${next_profile_number}

# 使用额外的配置文件“编号”构建配置文件列表
profile_list=$(echo "[${profiles_list},个人资料${next_profile_number}]")
echo "1 个配置文件列表:" ${profiles_list}

# 获取默认配置文件的转储,将全局名称更改为新的配置文件名称
profileName=我的新个人资料
gconftool-2 --dump "/apps/gnome-terminal/profiles/Default" > /tmp/${USER}_gnome-terminal_profiles_${profileName}.xml
sed -i "s|默认|配置文件${next_profile_number}|g" /tmp/${USER}_gnome-terminal_profiles_${profileName}.xml

# 加载新的配置文件
gconftool-2 --load /tmp/${USER}_gnome-terminal_profiles_${profileName}.xml

# 告诉 gnome-terminal 它有另一个配置文件
gconftool-2 --set --type list --list-type string "/apps/gnome-terminal/global/profile_list" "${profiles_list}"

# 设置属性
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/visible_name "${profileName}"
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/exit_action "hold"
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/font "Monospace 14"
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/background_color "#000000000000"
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/foreground_color "#0000FFFF0000"
gconftool-2 --set --type string /apps/gnome-terminal/profiles/Profile${next_profile_number}/scrollbar_position "隐藏"
gconftool-2 --set --type boolean /apps/gnome-terminal/profiles/Profile${next_profile_number}/use_system_font "false"
gconftool-2 --set --type boolean /apps/gnome-terminal/profiles/Profile${next_profile_number}/use_theme_colors "false"
gconftool-2 --set --type boolean /apps/gnome-terminal/profiles/Profile${next_profile_number}/login_shell "true"
gconftool-2 --set --type boolean /apps/gnome-terminal/profiles/Profile${next_profile_number}/scrollback_unlimited "true"

# 创建一个终端
gnome-terminal --geometry=80x24+0+0 --profile=${profileName} 标题 "${profileName}" --zoom 0.8 -e "/bin/sh"