Ubuntu + Fcitx + 搜狗拼音:无法输入中文 --- 可复现操作手册
适用:Ubuntu 20.04(GNOME、X11) 、Fcitx 4.x 、搜狗 .deb 安装版 (可执行文件在 /opt/sogoupinyin/files/bin/)。
本文 dpkg 安装包写死为 :sogoupinyin_4.2.1.145_amd64.deb,路径写死为 ~/Downloads/sogoupinyin_4.2.1.145_amd64.deb (~ 为当前用户主目录)。
按章节顺序执行;每步命令可直接复制到终端(除非注明为图形界面)。
0. 现象(与本次排障一致时可照做)
- 已安装搜狗,Fcitx 列表里能看到「搜狗拼音」,但 切不过去 / 配置灰 / gedit 打不出中文。
- 同机 谷歌拼音可能正常。
fcitx-diagnose曾出现:第一个输入法不是键盘 ;或sogouimebs-diag里只有sogouimebs-session,没有sogoupinyin-service。
1. 安装与依赖(若尚未安装)
bash
sudo apt-get update
sudo apt-get install -y fcitx fcitx-bin fcitx-config-gtk fcitx-frontend-all \
fcitx-module-dbus fcitx-googlepinyin im-config
安装搜狗(包名与路径已写死):
bash
sudo dpkg -i ~/Downloads/sogoupinyin_4.2.1.145_amd64.deb
sudo apt-get -f install -y
确认可执行文件存在:
bash
test -x /opt/sogoupinyin/files/bin/sogoupinyin-service && echo OK
2. 系统输入法框架选 Fcitx(必做)
图形界面(推荐):
bash
im-config
在弹出窗口中选择 fcitx ,确定;按提示 注销并重新登录(至少执行一次)。
3. 图形会话环境变量(必做,避免仅终端里能用)
若文件不存在则创建;若已存在则先打开检查是否已有相同三行,避免重复。
bash
XPROFILE="$HOME/.xprofile"
grep -q 'GTK_IM_MODULE=fcitx' "$XPROFILE" 2>/dev/null || cat >> "$XPROFILE" << 'EOF'
# Fcitx(搜狗 / 拼音)
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx
EOF
修改 ~/.xprofile 后需 注销重新登录 或 重启 后对整个桌面生效。
4. 修正 Fcitx:输入法顺序 + 热键
4.1 备份
bash
mkdir -p "$HOME/.config/fcitx"
cp -a "$HOME/.config/fcitx/profile" "$HOME/.config/fcitx/profile.bak.$(date +%Y%m%d%H%M)" 2>/dev/null || true
cp -a "$HOME/.config/fcitx/config" "$HOME/.config/fcitx/config.bak.$(date +%Y%m%d%H%M)" 2>/dev/null || true
4.2 输入法列表:EnabledIMList 必须以 键盘 为第一项
做法 A --- 图形界面(最直观)
bash
fcitx-config-gtk3
- 在 输入法 列表中,确保有 Keyboard - English (US) (或你用的
fcitx-keyboard-...)。 - 用 上移 ,把 键盘 移到 列表最顶端。
- 确认 搜狗拼音 、(可选)谷歌拼音 已添加且启用。
- 关闭窗口。
做法 B --- 命令行:一键重排前三项为「US 键盘 → 搜狗 → 谷歌」
下面脚本会解析 ~/.config/fcitx/profile 中的 EnabledIMList=,把 fcitx-keyboard-us、sogoupinyin、googlepinyin 排到最前并设为 True,其余项顺序不变。
若你的键盘不是 fcitx-keyboard-us,请先改脚本里 PRIORITY 第一项,或改用上面的图形界面。
bash
python3 << 'PY'
from pathlib import Path
profile = Path.home() / ".config/fcitx/profile"
if not profile.is_file():
print("ERROR: missing ~/.config/fcitx/profile --- open fcitx-config-gtk3 once and save, then re-run.")
raise SystemExit(1)
PRIORITY = ["fcitx-keyboard-us", "sogoupinyin", "googlepinyin"]
text = profile.read_text(encoding="utf-8", errors="replace")
lines_out = []
done = False
for line in text.splitlines(keepends=True):
if not line.startswith("EnabledIMList="):
lines_out.append(line)
continue
nl = "\n" if line.endswith("\n") else ""
raw = line.rstrip("\n\r")
_, rest = raw.split("=", 1)
parts = rest.split(",")
parsed = []
for p in parts:
if ":" in p:
n, _, v = p.partition(":")
parsed.append((n, v))
first_names = {n for n, _ in parsed}
missing = [x for x in PRIORITY if x not in first_names]
if missing:
print("WARN: not in list:", missing, "--- use fcitx-config-gtk3 to add them, then re-run this script.")
new_head = []
used = set()
for name in PRIORITY:
if name not in first_names:
continue
new_head.append(f"{name}:True")
used.add(name)
tail = [f"{n}:{v}" for n, v in parsed if n not in used]
new_line = "EnabledIMList=" + ",".join(new_head + tail) + nl
lines_out.append(new_line)
done = True
if not done:
print("ERROR: no EnabledIMList= line in profile")
raise SystemExit(1)
profile.write_text("".join(lines_out), encoding="utf-8")
print("profile: EnabledIMList head reordered OK")
PY
做法 C --- 仅当文件头正好是「搜狗, US, 谷歌」时的快速替换(可选)
bash
sed -i \
's/^EnabledIMList=sogoupinyin:True,fcitx-keyboard-us:True,googlepinyin:True,/EnabledIMList=fcitx-keyboard-us:True,sogoupinyin:True,googlepinyin:True,/' \
"$HOME/.config/fcitx/profile"
若 sed 后无变化,说明你的 EnabledIMList 开头与上述不一致,请用 做法 A 或 B。
4.3 热键:~/.config/fcitx/config
目标:
TriggerKey=CTRL_SPACE(激活/关闭输入法,可按习惯在fcitx-config-gtk3里改)。IMSwitchKey=True、IMSwitchHotkey=CTRL_SHIFT(用 Ctrl+Shift 在已启用输入法之间轮换)。
执行:
bash
CFG="$HOME/.config/fcitx/config"
test -f "$CFG" || { echo "ERROR: $CFG missing"; exit 1; }
sed -i 's/^TriggerKey=.*/TriggerKey=CTRL_SPACE/' "$CFG"
sed -i 's/^IMSwitchKey=False$/IMSwitchKey=True/' "$CFG"
sed -i 's/^#IMSwitchKey=True$/IMSwitchKey=True/' "$CFG"
sed -i 's/^#IMSwitchHotkey=CTRL_SHIFT$/IMSwitchHotkey=CTRL_SHIFT/' "$CFG"
若执行完上面的 sed 后仍没有 以 IMSwitchKey= 开头的行(可用 grep '^IMSwitchKey=' "$CFG" 检查),请用编辑器打开 "$CFG",在 [Hotkey] 小节中手动 增加以下两行(不要带 #):
IMSwitchKey=True
IMSwitchHotkey=CTRL_SHIFT
保存即可。
4.4 让 Fcitx 重新读配置
bash
fcitx -r
5. 搜狗后台服务:用户级自启动(本次关键)
现象:pgrep -a sogoupinyin 看不到 sogoupinyin-service,只有登录会话外层的 sogouimebs-session 时,需在用户目录写入自启动项(比仅依赖 /etc/xdg/autostart/ 更稳)。
整段复制执行:
bash
mkdir -p "$HOME/.config/autostart"
cat > "$HOME/.config/autostart/sogoupinyin-service.desktop" << 'EOF'
[Desktop Entry]
Name=sogoupinyin-service
Name[zh_CN]=搜狗输入法个人版
GenericName=sogoupinyin-service
GenericName[zh_CN]=搜狗输入法个人版
Comment=sogoupinyin-service
Comment[zh_CN]=搜狗输入法个人版
Keywords=ime;imf;input;
Exec=/opt/sogoupinyin/files/bin/sogoupinyin-service
Terminal=false
Type=Application
NoDisplay=true
Categories=System;Utility;
StartupNotify=false
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Phase=Applications
X-GNOME-Autostart-Notify=false
X-GNOME-Autostart-Delay=2
X-KDE-autostart-phase=1
X-KDE-autostart-after=panel
X-MATE-Autostart-Phase=Applications
X-MATE-Autostart-Delay=2
X-MATE-Autostart-Notify=false
EOF
cat > "$HOME/.config/autostart/sogoupinyin-watchdog.desktop" << 'EOF'
[Desktop Entry]
Name=sogoupinyin-watchdog
Name[zh_CN]=搜狗输入法个人版 守护进程
GenericName=sogoupinyin-watchdog
GenericName[zh_CN]=搜狗输入法个人版 守护进程
Comment=sogoupinyin-watchdog
Comment[zh_CN]=搜狗输入法个人版 守护进程
Keywords=ime;imf;input;
Exec=/opt/sogoupinyin/files/bin/sogoupinyin-watchdog
Terminal=false
Type=Application
NoDisplay=true
Categories=System;Utility;
StartupNotify=false
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Phase=Applications
X-GNOME-Autostart-Notify=false
X-GNOME-Autostart-Delay=3
X-KDE-autostart-phase=1
X-KDE-autostart-after=panel
X-MATE-Autostart-Phase=Applications
X-MATE-Autostart-Delay=3
X-MATE-Autostart-Notify=false
EOF
当前会话立即拉起(不等下次登录):
bash
/opt/sogoupinyin/files/bin/sogoupinyin-service &
/opt/sogoupinyin/files/bin/sogoupinyin-watchdog &
sleep 2
pgrep -a sogoupinyin || echo "WARN: no sogoupinyin processes --- check path and permissions"
fcitx-remote -s sogoupinyin
然后 注销并重新登录 (或 重启 ),确认每次开机都会出现 sogoupinyin-service。
6. 验证清单(复制执行)
bash
echo "=== sogoupinyin processes ==="
pgrep -a sogoupinyin || true
echo "=== fcitx ==="
pgrep -a fcitx || true
echo "=== IM env (current shell; GUI apps need .xprofile + re-login) ==="
echo "GTK_IM_MODULE=$GTK_IM_MODULE QT_IM_MODULE=$QT_IM_MODULE XMODIFIERS=$XMODIFIERS"
echo "=== fcitx diagnose (excerpt) ==="
fcitx-diagnose 2>&1 | grep -A6 '## Input Methods' || true
预期:
pgrep能看到.../sogoupinyin-service。fcitx-diagnose中 Default input methods 为 keyboard 在第一位(不应再提示 sogoupinyin 作默认第一项)。
在 gedit 中:Ctrl+Space 激活输入法,Ctrl+Shift 轮换到搜狗,输入拼音应上屏。
可选:搜狗官方收集日志
bash
/opt/sogoupinyin/files/bin/sogouimebs-diag
7. 常用命令速查
| 目的 | 命令 |
|---|---|
| Fcitx 配置 | fcitx-config-gtk3 |
| 切到搜狗 | fcitx-remote -s sogoupinyin |
| 重启 Fcitx | fcitx -r |
| 搜狗图形配置 | /opt/sogoupinyin/files/bin/sogoupinyin-configtool |
8. 仍存在问题时需要收集的信息
bash
journalctl --user -b | grep -iE 'sogou|fcitx' | tail -50
fcitx-diagnose 2>&1 | tee /tmp/fcitx-diagnose.txt
将 fcitx-diagnose 里 Configuration → Input Methods 、Frontends setup 和 journalctl 中与报错相关的几行一并反馈。
9. 修改汇总(给他人核对用)
| 路径 | 修改要点 |
|---|---|
~/.xprofile |
追加 GTK_IM_MODULE、QT_IM_MODULE、XMODIFIERS 三行指向 fcitx |
~/.config/fcitx/profile |
EnabledIMList 以 fcitx-keyboard-us:True(或你的键盘)开头,再接 sogoupinyin:True、googlepinyin:True |
~/.config/fcitx/config |
TriggerKey=CTRL_SPACE;IMSwitchKey=True;IMSwitchHotkey=CTRL_SHIFT |
~/.config/autostart/sogoupinyin-service.desktop |
Exec=/opt/sogoupinyin/files/bin/sogoupinyin-service,含 X-GNOME-Autostart-enabled=true |
~/.config/autostart/sogoupinyin-watchdog.desktop |
Exec=/opt/sogoupinyin/files/bin/sogoupinyin-watchdog,含 X-GNOME-Autostart-enabled=true |
im-config |
选 fcitx |
文档版本:可逐步复制执行;若发行版/搜狗大版本不同,仅路径与包名可能略有差异。