0x00 简介
Vim(Vi IMproved)是一款强大的文本编辑器。它支持多种编辑模式,具有丰富的编辑功能和高度可定制性。Vim 提供了代码折叠、语法高亮、自动补全等功能,适用于程序员和文本编辑爱好者。通过个性化配置和插件系统,用户可以定制快捷键、颜色方案等。Vim 还可以作为图形化编辑器,在不同操作系统上运行,并与版本控制系统集成。总之,Vim 是一款高效、灵活的编辑器,为用户提供优秀的编辑体验。
-
0x00 简介
-
0x01 Vim 配置加载过程
-
1. vim 配置说明
-
2. 确定已加载的配置
-
3. 系统级配置文件解析
-
4. 用户配置文件解析
-
5. 用户层ex编辑器配置文件
-
6. 默认配置文件
-
7. 自动加载的配置文件
-
8. vim 配置加载过程初步总结
-
-
0x02 Vim 配置加载细节问题
-
1. 系统配置与用户配置
-
2. 用户配置与默认配置文件
-
3. 用户配置与用户配置
-
4. runtimepath 与 $VIMRUNTIME
-
5. 哪些目录自动加载
-
6. 哪些文件自动加载
-
7. 自动加载好奇的知识点
-
-
0x03 vim 后门说明
-
0x04 vim 自身文件后门
-
1. 查找 vim 命令程序位置
-
2. 制作后门文件
-
-
0x05 配置文件后门
-
1. 执行系统命令
-
2. 屏蔽控制台输出
-
3. 规避 vim 阻塞
-
-
0x06 features
-
1. +libcall
-
2. +packages
-
3. +python3
-
4. +user_commands
-
5. +vim9script
-
-
0x07 总结
-
0x08 往期文章
0x01 Vim 配置加载过程
vim 配置加载过程简单描述就是先加载系统级别配置,之后加载个人用户配置,接下来以 Ubuntu 22.04 为例
可以通过 vim --version
来进行查看各种文件的位置以及一些系统包含的插件
vim --version
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
defaults file: "$VIMRUNTIME/defaults.vim"
fall-back for $VIM: "/usr/share/vim"
1. vim 配置说明
-
系统级别配置文件
$VIM/vimrc
-
用户配置文件
$HOME/.vimrc
-
次优先级用户配置文件
~/.vim/vimrc
-
用户层ex编辑器配置文件
$HOME/.exrc
-
默认配置文件
$VIMRUNTIME/defaults.vim
-
次默认配置文件
/usr/share/vim
其中 $VIM
是 vim 内置的变量而不是 Linux 的环境变量,当然 vim 也是可以使用 Linux 环境变量的
通过在vim的底线命令模式中 echo $变量名
来获取 vim 的配置文件地址
因此,在 Ubuntu Server 22.04 vim 默认配置为
-
系统级别配置文件
$VIM/vimrc
——>/usr/share/vim/vimrc -> /etc/vim/vimrc
-
用户配置文件
$HOME/.vimrc
——>~/.vimrc
-
次优先级用户配置文件
~/.vim/vimrc
-
用户层ex编辑器配置文件
$HOME/.exrc
——>~/.exrc
-
默认配置文件
$VIMRUNTIME/defaults.vim
——>/usr/share/vim/vim82/defaults.vim
-
$VIM
后备配置文件/usr/share/vim
这些配置文件默认情况下并不是都存在
2. 确定已加载的配置
但是这些配置文件中可能还会引用其他配置文件,因此想要了解本次vim 请求加载了哪些配置文件可以通过在vim的底线命令模式中输入 scriptnames
Ubuntu Server 22.04 默认情况下是没有 ~/.vim
目录的
首先加载的是系统级配置文件 /usr/share/vim/vimrc
, 之后根据系统级配置文件的内容加载了一些配置文件内容,默认没有发现用户级配置文件的加载
可以看到默认并没有用户配置
3. 系统级配置文件解析
vim 配置文件有自己的一套 vim 脚本语法,具体可以通过下面的链接进行学习
https://devhints.io/vimscript
https://www.w3cschool.cn/vim/
其中 "
是注释符号, runtime! xxx.vim
表示强制重新加载 xxx.vim
/usr/share/vim/vimrc
-> /etc/vim/vimrc
" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by
" the call to :runtime you can find below. If you wish to change any of those
" settings, you should do it in this file (/etc/vim/vimrc), since debian.vim
" will be overwritten everytime an upgrade of the vim packages is performed.
" It is recommended to make changes after sourcing debian.vim since it alters
" the value of the 'compatible' option.
runtime! debian.vim
" Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc.
" This happens after /etc/vim/vimrc(.local) are loaded, so it will override
" any settings in these files.
" If you don't want that to happen, uncomment the below line to prevent
" defaults.vim from being loaded.
" let g:skip_defaults_vim = 1
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible
" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
"au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"filetype plugin indent on
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
去掉注释后
runtime! debian.vim
if has("syntax")
syntax on
endif
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
分为三部分
runtime! debian.vim 强制重新加载 debian.vim
经过测试,这里 debian.vim
所在路径为 $VIMRUNTIME
的值(/usr/share/vim/vim82
)的目录下
经过测试,这里直接写绝对路径是不行的,必须是这个目录的相对路径
if has("syntax")
syntax on
endif
这段代码检查当前 vim 是否支持语法高亮,如果支持则打开语法高亮
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
这段配置代码的意思是,如果文件/etc/vim/vimrc.local
存在并且可读,则加载该文件
通过对系统级配置文件 /etc/vim/vimrc 进行分析,可以知道这个文件默认内容不多,主要就是开启语法高亮、重新加载配置文件 debian.vim 、引入 /etc/vim/vimrc.local
因此接下来分析 debian.vim 和 /etc/vim/vimrc.local
/usr/share/vim/vim82/debian.vim
这一看就是 debian 系操作系统特有的文件, Centos 以及 Rocky Linux 等没有这个文件,甚至系统级配置文件位置都不同
根据 /etc/vim/vimrc
中注释描述,将所有系统侧配置文件都放置在 /usr/share/vim/vim82/debian.vim
文件中,每次程序升级时,都有可能覆盖更新 /usr/share/vim/vim82/debian.vim
文件,因此一些个性化配置尽量在个人用户配置处进行
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible " Use Vim defaults instead of 100% vi compatibility
set backspace=indent,eol,start " more powerful backspacing
" Now we set some defaults for the editor
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
" modelines have historically been a source of security/resource
" vulnerabilities -- disable by default, even when 'nocompatible' is set
set nomodeline
" Suffixes that get lower priority when doing tab completion for filenames.
" These are files we are not likely to want to edit or read.
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
" We know xterm-debian is a color terminal
if &term =~ "xterm-debian" || &term =~ "xterm-xfree86"
set t_Co=16
set t_Sf=[3%dm
set t_Sb=[4%dm
endif
" Some Debian-specific things
if has('gui')
" Must define this within the :if so it does not cause problems with
" vim-tiny (which does not have +eval)
function! <SID>MapExists(name, modes)
for mode in split(a:modes, '\zs')
if !empty(maparg(a:name, mode))
return 1
endif
endfor
return 0
endfunction
" Make shift-insert work like in Xterm
autocmd GUIEnter * if !<SID>MapExists("<S-Insert>", "nvso") | execute "map <S-Insert> <MiddleMouse>" | endif
autocmd GUIEnter * if !<SID>MapExists("<S-Insert>", "ic") | execute "map! <S-Insert> <MiddleMouse>" | endif
endif
" Set paper size from /etc/papersize if available (Debian-specific)
if filereadable("/etc/papersize")
let s:papersize = matchstr(readfile('/etc/papersize', '', 1), '\p*')
if strlen(s:papersize)
exe "set printoptions+=paper:" . s:papersize
endif
endif
这个配置文件中未包含对其他配置文件的引用
/etc/vim/vimrc.local
Ubuntu Server 22.04 中默认不存在这个文件
4. 用户配置文件解析
-
~/.vimrc
-
~/.vim/vimrc
Ubuntu 默认不存在vim个人用户配置文件
5. 用户层ex编辑器配置文件
~/.exrc
Ubuntu Server 22.04 默认不存在该配置文件
6. 默认配置文件
默认配置文件在没有个人用户的配置文件时使用,Ubuntu Server 22.04 默认无个人默认配置文件,因此默认情况下会启用默认配置文件
$VIMRUNTIME/defaults.vim
——> /usr/share/vim/vim82/defaults.vim
" The default vimrc file.
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2021 Nov 17
"
" This is loaded if no vimrc file was found.
" Except when Vim is run with "-u NONE" or "-C".
" Individual settings can be reverted with ":set option&".
" Other commands can be reverted as mentioned below.
" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
finish
endif
" Bail out if something that ran earlier, e.g. a system wide vimrc, does not
" want Vim to use these default values.
if exists('skip_defaults_vim')
finish
endif
" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
" Avoid side effects when it was already reset.
if &compatible
set nocompatible
endif
" When the +eval feature is missing, the set command above will be skipped.
" Use a trick to reset compatible only when the +eval feature is missing.
silent! while 0
set nocompatible
silent! endwhile
" Allow backspacing over everything in insert mode.
set backspace=indent,eol,start
set history=200 " keep 200 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set wildmenu " display completion matches in