最近在使用vim时,如果账号为root时,颜色的显示是一样的;而账号为kavin(uid=500),会根据文件内容来显示不同的颜色的;因为自己比较喜欢vim的这个根据内容显示不同颜色的功能,而且对眼睛也有好处。所以自己去查找了下原因。

1、确认了下vim的配置文档,看看是否对root有特殊设置


  
  1. [root@localhost etc]# diff vimrc virc  --vi有两个配置文档,先比对下连个文件是否有区别
  2. [root@localhost etc]# cat vimrc   
  3. if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"  
  4.    set fileencodings=utf-8,latin1  
  5. endif  
  6.   
  7. set nocompatible        " Use Vim defaults (much better!)  
  8. set bs=indent,eol,start         " allow backspacing over everything in insert mode  
  9. "set ai                 " always set autoindenting on  
  10. "set backup             " keep a backup file  
  11. set viminfo='20,\"50    " read/write a .viminfo file, don't store more  
  12.                         " than 50 lines of registers  
  13. set history=50          " keep 50 lines of command line history  
  14. set ruler               " show the cursor position all the time  
  15.   
  16. Only do this part when compiled with support for autocommands  
  17. if has("autocmd")  
  18.   augroup redhat  
  19.     " In text files, always limit the width of text to 78 characters  
  20.     autocmd BufRead *.txt set tw=78  
  21.     " When editing a file, always jump to the last cursor position  
  22.     autocmd BufReadPost *  
  23.     \ if line("'\"") > 0 && line ("'\"") <= line("$") |  
  24.     \   exe "normal! g'\"" |  
  25.     \ endif  
  26.   augroup END  
  27. endif  
  28.   
  29. if has("cscope") && filereadable("/usr/bin/cscope")  
  30.    set csprg=/usr/bin/cscope  
  31.    set csto=0  
  32.    set cst  
  33.    set nocsverb  
  34.    " add any database in current directory  
  35.    if filereadable("cscope.out")  
  36.       cs add cscope.out  
  37.    " else add database pointed to by environment  
  38.    elseif $CSCOPE_DB != ""  
  39.       cs add $CSCOPE_DB  
  40.    endif  
  41.    set csverb  
  42. endif  
  43.   
  44. " Switch syntax highlighting onwhen the terminal has colors  
  45. " Also switch on highlighting the last used search pattern.  
  46. if &t_Co > 2 || has("gui_running")  
  47.   syntax on  
  48.   set hlsearch  
  49. endif  
  50.   
  51. if &term=="xterm"  
  52.      set t_Co=8  
  53.      set t_Sb=dm  
  54.      set t_Sf=dm  
  55. endif  
  56. [root@localhost etc]#   

 从上面看发现还是没有区别,应该不是vim配置文档的问题。

2、根据linux的引导顺序逐层查找原因,终于在/etc/profile.d/vim.sh文件里面被我找到了原因,以下为该文档未修改的内容:


  
  1. [root@localhost etc]# vi /etc/profile.d/vim.sh   
  2. if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n "$ZSH_VERSION" ]; then 
  3.   [ -x /usr/bin/id ] || return 
  4.   tmpid=$(/usr/bin/id -u) 
  5.   [ "$tmpid" = "" ] && tmpid=0 
  6.   [ $tmpid -le 100 ] && return 
  7.   # for bash and zsh, only if no alias is already set 
  8.   alias vi >/dev/null 2>&1 || alias vi=vim 
  9. fi 

从vim.sh文档中可以看出,如果uid小于100的账号使用vim是都是没有根据内容显示不同颜色的功能,解决办法有两种:

①修改该账号自己的.bashrc,添加内容 alias vi='vim'即可。

②直接修改/etc/vim.sh,将[ $tmpid -le 100 ] && return注释掉即可

第一种方法只能修改本账号,而第二种方法针对所有账号。

附上root更改前后的图片比对:

修改前 修改前