使用 zsh 后HOME/END 键以及小键盘失效

#1 问题描述

zsh是一款超赞的shell工具,但是配置复杂,有个闲着没事的程序员,弄了一个开源项目 robbyrussell/oh-my-zsh 截止目前,58.8k+ star就知道有多火了。比如 Spring boot在github才 15.7K+ spring framework 也16.1k+,最近火到炸天的tensorflow 69.4k

同样很优秀的有 fisherman/fisherman

但是近期发现问题, 设置了 xshell 远程连接时, host将zsh设置成默认shell时, HOME/END 以及小键盘区诸键无效. 敲击没有任何反应.

#2 问题解决

##2.1 设置 xshell 连接终端的类型

第一种方法也是最简单的办法是,修改xshell连接此host的终端类型,改成linux

修改终端属性

但是注意,改用 Linux 可能会导致shell的颜色比较奇怪.

##2.2 重新映射键值

在 ~/.zshrc 中将 HOME/END 等键值重新 MAP.

# key bindings
bindkey "\e[1~" beginning-of-line
bindkey "\e[4~" end-of-line
bindkey "\e[5~" beginning-of-history
bindkey "\e[6~" end-of-history

# for rxvt
bindkey "\e[8~" end-of-line
bindkey "\e[7~" beginning-of-line
# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
bindkey "\eOH" beginning-of-line
bindkey "\eOF" end-of-line
# for freebsd console
bindkey "\e[H" beginning-of-line
bindkey "\e[F" end-of-line
# completion in the middle of a line
bindkey '^i' expand-or-complete-prefix

# Fix numeric keypad  
# 0 . Enter  
bindkey -s "^[Op" "0"
bindkey -s "^[On" "."
bindkey -s "^[OM" "^M"
# 1 2 3  
bindkey -s "^[Oq" "1"
bindkey -s "^[Or" "2"
bindkey -s "^[Os" "3"
# 4 5 6  
bindkey -s "^[Ot" "4"
bindkey -s "^[Ou" "5"
bindkey -s "^[Ov" "6"
# 7 8 9  
bindkey -s "^[Ow" "7"
bindkey -s "^[Ox" "8"
bindkey -s "^[Oy" "9"
# + - * /  
bindkey -s "^[Ol" "+"
bindkey -s "^[Om" "-"
bindkey -s "^[Oj" "*"
bindkey -s "^[Oo" "/"

#3 参考资料

Home and End keys not working on CentOS

Problem with home, end and del keys under zsh

ins-end-delete-keys-are-mischevious-297024

### 解决 ZSH 历史记录文件损坏的方法 当遇到 `zsh: corrupt history file` 错误时,通常是因为 `.zsh_history` 文件的内容被破坏或格式不正确。以下是修复该问题的具体方法: #### 方法一:重建历史记录文件 可以通过以下命令清理并重新生成历史记录文件: ```bash cd ~ mv .zsh_history .zsh_history_bad strings .zsh_history_bad > .zsh_history fc -R ~/.zsh_history ``` 上述脚本的作用如下: - 将原始的历史记录文件重命名为 `.zsh_history_bad`,以便备份[^3]。 - 使用 `strings` 提取可读字符串到新的 `.zsh_history` 文件中,从而去除可能存在的非法字符或数据。 - 执行 `fc -R` 刷新当前会话中的历史记录缓存。 #### 方法二:手动删除损坏文件 如果确认不需要保留旧的历史记录,则可以直接移除损坏的文件并创建一个新的空白历史记录文件: ```bash rm ~/.zsh_history touch ~/.zsh_history chmod 600 ~/.zsh_history ``` 此操作完全清除了之前的命令历史记录,并设置适当的权限以防止潜在的安全风险[^2]。 #### 方法三:调整 ZSH 配置避免未来发生类似问题 为了减少再次出现此类错误的可能性,可以在用户的 ZSH 配置文件(通常是 `~/.zshrc`)中加入以下配置项: ```bash setopt INC_APPEND_HISTORY_TIME HISTSIZE=10000 SAVEHIST=10000 ``` 这些选项可以优化历史记录管理机制,例如增加存储容量以及按时间戳追加新条目,有助于提高稳定性[^1]。 通过以上三种方案之一即可有效解决 `zsh: corrupt history file` 的报错情况。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值