解决Ubuntu22.04Desktop桌面版的vim的编辑模式下的上下左右变成ABCD
Ubuntu22.04Desktop桌面版的vim的编辑模式下的上下左右变成ABCD , 退格键也失效
解决办法1, 卸载重装vim
sudo apt remove vim; sudo apt install -y vim
解决办法2: 修改 /etc/vim/vimrc.tiny
配置文件
备份 /etc/vim/vimrc.tiny 配置文件
tempPathUri= /etc/vim/vimrc.tiny ; sudo cp -a ${tempPathUri} ${tempPathUri}.$(date +%0y%0m%0d_%0H%0M%0S_ns%0N).bak
用nano 修改 /etc/vim/vimrc.tiny 配置文件
sudo nano /etc/vim/vimrc.tiny
- 将
set compatible
改为set nocompatible
- 加上一条:
set backspace=2
或者
用 sed 修改 /etc/vim/vimrc.tiny 配置文件
sudo sed -ie 's/^set compatible$/set nocompatible\nset backspace=2/g' /etc/vim/vimrc.tiny
将原先的
set compatible
替换成
set nocompatible
set backspace=2
或者
重写 /etc/vim/vimrc.tiny 配置文件
备份 /etc/vim/vimrc.tiny 配置文件
tempPathUri= /etc/vim/vimrc.tiny ; sudo cp -a ${tempPathUri} ${tempPathUri}.$(date +%0y%0m%0d_%0H%0M%0S_ns%0N).bak
重写 /etc/vim/vimrc.tiny 配置文件的脚本
不同版本的 /etc/vim/vimrc.tiny 会有一点区别,如
/usr/share/vim/vim81 或 /usr/share/vim/vim91
echo '" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set nocompatible
set backspace=2
" vim: set ft=vim:' | sudo tee /etc/vim/vimrc.tiny
或者
去掉不必要内容版
echo '
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set nocompatible
set backspace=2
' | sudo tee /etc/vim/vimrc.tiny
原版的 /etc/vim/vimrc.tiny 配置文件的脚本
不同版本的 /etc/vim/vimrc.tiny 会有一点区别,如
/usr/share/vim/vim81 或 /usr/share/vim/vim91
Ubuntu-Desktop-24.04.2 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim91,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Ubuntu-Server-24.04.2 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim91,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Debian12.9.0 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim90,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Ubuntu-Desktop-22.04 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
ubuntu-22.04.5 的原版 /etc/vim/vimrc.tiny
配置文件,和上面一样
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Ubuntu-Desktop-20.04.5 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim81,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Ubuntu-Server-16.04.7 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
Debian10.12-Standard 的原版 /etc/vim/vimrc.tiny
配置文件
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim81,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:
修改后的 /etc/vim/vimrc.tiny
" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set nocompatible
set backspace=2
" vim: set ft=vim:
去除注释精简版
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim>
set nocompatible
set backspace=2
用命令或脚本修改
echo '" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set nocompatible
set backspace=2
" vim: set ft=vim:' | sudo tee /etc/vim/vimrc.tiny
或者
sudo sed -ie 's/^set compatible$/set nocompatible\nset backspace=2/g' /etc/vim/vimrc.tiny
去掉不必要内容版
echo '
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set nocompatible
set backspace=2
' | sudo tee /etc/vim/vimrc.tiny
直接在文件末尾追加, 会覆盖之前的设置, 优先级更高
echo -e "\n\nset nocompatible\nset backspace=2\n" >> /etc/vim/vimrc.tiny
加强版脚本
#!/bin/bash
file_path="/etc/vim/vimrc.tiny"
### grep和sed在默认不使用扩展正则时,+加号和?问好要加\反斜杠表示: 一多"\+" 零一"\?"
pattern="^set\s\+compatible$"
### 删除包含set backspace=x的行
sed -Eie "s/^\s*set\s+backspace\s*\=.*$//g" $file_path
if grep -q "$pattern" "$file_path"; then
echo "文件 包含 : $pattern , 执行替换操作"
sed -ie "s/$pattern/set nocompatible\nset backspace=2/g" $file_path
else
echo "文件 不包含 : $pattern , 执行追加操作"
echo -e "\n\nset nocompatible\nset backspace=2\n" >> /etc/vim/vimrc.tiny
fi
在这个脚本中:
file_path
变量存储了你要检查的文件路径。pattern
变量存储了你要查找的字符串的正则表达式。grep -q "$pattern" "$file_path"
命令用于在文件中查找字符串。-q
选项表示静默模式(quiet mode),即grep
不会输出任何内容,只会根据是否找到匹配项设置其退出状态。-q
选项表示静默模式(quiet mode),即grep
不会输出任何内容,只会根据是否找到匹配项设置其退出状态。if
语句检查grep
命令的退出状态。如果grep
找到了匹配的字符串,则退出状态为0
,if
语句内的代码块会执行;否则,else
语句内的代码块会执行。
可以复制命令执行,也可以将上述脚本保存为一个 .sh
文件,比如 xxx.sh
,然后通过以下命令来运行它:
给脚本文件加上可执行权限
### 给脚本文件加上可执行权限
chmod +x xxx.sh
确保 example.txt
文件存在于脚本所在的目录中,并且包含你要查找的字符串,以验证脚本的正确性。
脚本2, 删除后追加
#!/usr/bin/bash
### 删除包含set backspace=x的行
sed -ie "s/^\s*set\s\+backspace\s*\=.*$//g" $file_path
### 删除包含set xxcompatible 的行
sed -Eie "s/^\s*set\s+[a-z]*compatible.*$//g" $file_path
### 文件末尾追加
echo -e "\n\nset nocompatible\nset backspace=2\n" >> /etc/vim/vimrc.tiny
脚本3, 分别有就替换,没有就追加
#!/bin/bash
file_path="/etc/vim/vimrc.tiny"
### grep和sed在默认不使用扩展正则时,+加号和?问好要加\反斜杠表示: 一多"\+" 零一"\?"
pattern="^\s*set\s\+[a-z]*compatible.*$"
if grep -q "$pattern" "$file_path"; then
echo "文件 包含 : $pattern , 执行替换操作"
sed -ie "s/$pattern/set nocompatible/g" $file_path
else
echo "文件 不包含 : $pattern , 执行追加操作"
echo -e "\n\nset nocompatible\n" >> /etc/vim/vimrc.tiny
fi
### grep和sed在默认不使用扩展正则时,+加号和?问好要加\反斜杠表示: 一多"\+" 零一"\?"
pattern="^\s*set\s+backspace\s*\=.*$"
if grep -qE "$pattern" "$file_path"; then
echo "文件 包含 : $pattern , 执行替换操作"
sed -Eie "s/$pattern/set backspace=2/g" $file_path
else
echo "文件 不包含 : $pattern , 执行追加操作"
echo -e "\nset backspace=2\n" >> /etc/vim/vimrc.tiny
fi
还原
echo '" Vim configuration file, in effect when invoked as "vi". The aim of this
" configuration file is to provide a Vim environment as compatible with the
" original vi as possible. Note that ~/.vimrc configuration files as other
" configuration files in the runtimepath are still sourced.
" When Vim is invoked differently ("vim", "view", "evim", ...) this file is
" _not_ sourced; /etc/vim/vimrc and/or /etc/vim/gvimrc are.
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim82,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
" vim: set ft=vim:' | sudo tee /etc/vim/vimrc.tiny