linux文本操作vi显示颜色
安装虚拟机部署centos系统,使用vi编辑代码时,发现没有颜色区分,而自己其他机器是可以显示颜色的。通过查看其他主机的信息,发现vi 其实是vim。当我使用vim时无法找到该命令,查询安装信息如下
[root@linuxcentos7 ~]$ [root@linuxcentos7 ~]$ rpm -qa |grep vim vim-minimal-7.4.629-7.el7.x86_64 [root@linuxcentos7 ~]$ [root@linuxcentos7 ~]$ alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@linuxcentos7 ~]$
原因:vi是没有颜色的,其他机器上,在linux的控制台下用vi编辑文件却能看到彩色这是因为安装了vim-enhanced的包,所以我们运行vi实际上运行的是vi的增强版本vim;目前本虚拟机需要安装 vim-enhanced,安装后默认在控制台就是彩色显示了。
安装vim
[root@linuxcentos7 ~]# rpm -qa |grep vim vim-minimal-7.4.629-7.el7.x86_64 [root@linuxcentos7 ~]# [root@linuxcentos7 ~]# [root@linuxcentos7 ~]# yum install vim Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: ftp.sjtu.edu.cn * updates: ftp.sjtu.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/2): updates/7/x86_64/primary_db | 17 MB 00:00:03 (2/2): extras/7/x86_64/primary_db | 250 kB 00:00:13 Resolving Dependencies --> Running transaction check ---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed --> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64 --> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64 --> Running transaction check ---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed ---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed --> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64 --> Running transaction check ---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================== Package Arch Version Repository Size ======================================================================================================================== Installing: vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M Installing for dependencies: gpm-libs x86_64 1.20.7-6.el7 base 32 k vim-common x86_64 2:7.4.629-8.el7_9 updates 5.9 M vim-filesystem x86_64 2:7.4.629-8.el7_9 updates 11 k Transaction Summary ======================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 7.0 M Installed size: 23 M Is this ok [y/d/N]: y Downloading packages: (1/4): gpm-libs-1.20.7-6.el7.x86_64.rpm | 32 kB 00:00:00 (2/4): vim-enhanced-7.4.629-8.el7_9.x86_64.rpm | 1.1 MB 00:00:00 (3/4): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm | 11 kB 00:00:00 (4/4): vim-common-7.4.629-8.el7_9.x86_64.rpm | 5.9 MB 00:00:01 ------------------------------------------------------------------------------------------------------------------------ Total 4.0 MB/s | 7.0 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 1/4 Installing : 2:vim-common-7.4.629-8.el7_9.x86_64 2/4 Installing : gpm-libs-1.20.7-6.el7.x86_64 3/4 Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 4/4 Verifying : gpm-libs-1.20.7-6.el7.x86_64 1/4 Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 2/4 Verifying : 2:vim-common-7.4.629-8.el7_9.x86_64 3/4 Verifying : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 4/4 Installed: vim-enhanced.x86_64 2:7.4.629-8.el7_9 Dependency Installed: gpm-libs.x86_64 0:1.20.7-6.el7 vim-common.x86_64 2:7.4.629-8.el7_9 vim-filesystem.x86_64 2:7.4.629-8.el7_9 Complete! [root@linuxcentos7 ~]# [root@linuxcentos7 ~]# [root@linuxcentos7 ~]# rpm -qa |grep vim vim-minimal-7.4.629-7.el7.x86_64 vim-common-7.4.629-8.el7_9.x86_64 vim-filesystem-7.4.629-8.el7_9.x86_64 vim-enhanced-7.4.629-8.el7_9.x86_64
效果
添加alias,转义:
vim ~/.bashrc alias vi='vim'
[root@linuxcentos7 essential]# vim ~/.bashrc [root@linuxcentos7 essential]# source ~/.bashrc [root@linuxcentos7 essential]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias ss='cd /mnt/hgfs/liugh/studyspace' alias vi='vim' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
再用vi时,实际用的是vim。
按照上述操作,有可能仍不能显示颜色,见附录处理。
附录:
1. 安装好vim,设置环境变量
echo "export TERM=xterm" >> ~/.bashrc source ~/.bashrc
此时vim编辑仍无颜色,查看/etc/vimrc文件。
2. /etc/vimrc编辑
文件为空
[root@test1 ~]# ll /etc/vi* -rw-r--r--. 1 root root 15 Oct 18 15:08 /etc/vimrc -rw-r--r-- 1 root root 1982 Jun 22 2018 /etc/vimrc.rpmnew -rw-r--r--. 1 root root 1982 Jun 22 2018 /etc/virc [root@test1 ~]# date Mon Jan 30 13:43:57 CST 2023 [root@test1 ~]# vi /etc/vimrc.rpmnew [root@test1 ~]# cat /etc/vimrc set nomodeline
把 /etc/vimrc.rpmnew 或 /etc/virc 内容拷贝到 /etc/vimrc 即可。(vimrc.rpmnew 和 virc 内容相同)
cp /etc/virc /etc/vimrc diff /etc/vimrc /etc/vimrc.rpmnew
此时vim编辑显示颜色了。
原因:系统安装时默认vim 最小化安装,已经生成了/etc/vimrc文件,再次安装vim相关组件时,该文件未被更新,而是生成了vimrc.rpmnew文件。
解决:安装vim前,把/etc/vimrc删除,安装时则会重新生成/etc/vimrc。
3. 普通用户权限赋予
当root用户使用vi显示颜色,但是使用普通用户时,仍然不显示颜色,这时需要确认,普通用户有没有访问/etc/vimrc的权限,如果没有,则赋予可读权限。
如下
[root@test1 ~]# ll /etc/vi* -rw-r----- 1 root root 1982 Apr 23 14:02 /etc/vimrc -rw-r--r-- 1 root root 1982 Jun 22 2018 /etc/vimrc.rpmnew -rw-r--r--. 1 root root 1982 Jun 22 2018 /etc/virc
普通用户无法读取vimrc,则在该普通用户下,无法显示颜色。修改权限如下
[root@test1 ~]# chmod 644 /etc/vimrc [root@test1 ~]# ll /etc/vi* -rw-r--r-- 1 root root 1982 Apr 23 14:02 /etc/vimrc