文章目录
- 1. 目的
- 2. 理念: vimsolo
- 3. vimrc: 配置颜色
- 4. vimrc: 配置状态栏
- 5. 拷贝颜色主题和.vimrc: python安装脚本
1. 目的
习惯了 VSCode 默认的配色:黑色主题,蓝色状态栏。偶尔使用 Vim 时想让 vim 伪装的像 VSCode,不考虑花里花哨的插件和动态效果,静态效果上让 vim 看起来像 VSCode,怎样实现呢?
2. 理念: vimsolo
vimsolo = vim + solo,除了颜色主题可以用第三方插件, 其他配置都用 .vimrc 手工完成,不依赖库插件。
vimsolo 的理念是: vim 插件如果装多了,配置繁杂,受网络影响较大,还需要适配不同 vim/nvim 版本和 nerdfont 字体, 而是极致的简洁。
3. vimrc: 配置颜色
VSCode 颜色主题的 vim 插件有好几个, 我用的是 codedark https://github.com/tomasiser/vim-code-dark
vimrc 中的配置代码:
"--- color theme
set background=dark
tryset t_Co=256set t_ut=colorscheme codedark""colorscheme molokai
catchtrycolorscheme desertcatchcolorscheme peakseaendtry
endtry
用 vim 打开 Python 文件看看效果:
4. vimrc: 配置状态栏
状态栏的插件有很多,很容易陷入选择的困境,因为都太漂亮了;而一一尝试和配置会花费不少时间,但容易漂浮在插件开发者配置的表层。其实基于 vimL 徒手可以写一个 statuline:
- 本质是给变量
statusline
赋值 - 把 statueline 拆分为n部分,每部分是一个字符串变量,变量之间用
.
连接起来 - 每部分大都可以从 vim 自带的变量或函数获取到,例如文件编码是
let l:encoding = ' %{&fenc} '
具体配置 vimL 脚本如下:
"======================================================================
" statusline
"======================================================================
"--- https://bruhtus.github.io/posts/vim-statusline/
"--- https://jdhao.github.io/2019/11/03/vim_custom_statusline/
" component for active window
function! StatuslineActive()" if we want to add `f` items in our statuslinelet g:currentmode={\ 'n' : 'NORMAL',\ 'v' : 'VISUAL',\ 'V' : 'V·Line',\ "\<C-V>" : 'V·Block',\ 'i' : 'INSERT',\ 'R' : 'R',\ 'Rv' : 'V·Replace',\ 'c' : 'Command',\}"let l:current_mode = mode()let l:current_mode = ' ['.'%{toupper(g:currentmode[mode()])}'.'] '" if we want to add 'm' items in our statuslinelet l:cursor_position = ' Ln %l, Col %c'let l:indentation = ' %{(&expandtab=="1") ? "Spaces: ".&tabstop : "Tabs: ".&tabstop} 'let l:encoding = ' %{&fenc} 'let l:end_of_line_sequence = ' %{(&fileformat=="dos")? "CRLF" : "LF"} 'let l:percent = ' %p%% 'let l:language_mode = '%{&filetype}'" the `.` is basically to ignore whitespace before and put it right after the previous componentlet l:statusline_left = l:current_modelet l:statusline_middle = ''let l:statusline_right = l:cursor_position.l:indentation.l:encoding.l:end_of_line_sequence.l:percent.l:language_modereturn l:statusline_left.'%='.l:statusline_middle.'%='.l:statusline_right
endfunction" component for inactive window
function! StatuslineInactive()" the component goes here
endfunction" load statusline using `autocmd` event with this function
function! StatuslineLoad(mode)if a:mode ==# 'active'" to make it simple, %! is to evaluate the current changes in the window" it can be useful for evaluate current mode in statusline. For more info:" :help statusline.setlocal statusline=%!StatuslineActive()elsesetlocal statusline=%!StatuslineInactive()endif
endfunction" so that autocmd didn't stack up and slow down vim
augroup statusline_startupautocmd!" for more info :help WinEnter and :help BufWinEnterautocmd WinEnter,BufWinEnter * call StatuslineLoad('active')autocmd WinLeave * call StatuslineLoad('inactive')
augroup ENDhi StatusLine ctermbg=32 ctermfg=254 guibg=#007acc guifg=#dfe9ed
"hi StatusLineNC ctermbg=240 ctermfg=240 guibg=#d0d0d0 guifg=#444444hi StatusLineTerm ctermbg=32 ctermfg=254 guibg=#007acc guifg=#dfe9ed
"hi StatusLineTermNC ctermbg=252 ctermfg=240 guibg=#d0d0d0 guifg=#444444
"-- #007acc
"-- #dfe9ed
配置效果:
5. 拷贝颜色主题和.vimrc: python安装脚本
使用 Python 编写了安装脚本,相比于 shell 脚本,开发效率相当高, 平台兼容性更强。
import os
import shutil
import subprocess
import platformdef is_wsl():return 'microsoft-standard' in platform.uname().releasedef is_windows():return platform.system().lower() == "windows"def is_linux():return platform.system().lower() == "linux"def is_macosx():return platform.system().lower() == "darwin"class CommandRunner(object):@staticmethoddef run(cmd, verbose = True):if (verbose):print('Executing cmd:', cmd)process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)if is_windows():# encoding = 'ISO-8859-1'encoding = 'gbk'else:encoding = 'utf-8'#print('encoding:', encoding)out_bytes = process.communicate()[0]err_bytes = process.communicate()[1]out_msg = out_bytes.decode(encoding)err_msg = out_bytes.decode(encoding)if (verbose):print('Command executing stdout: {:s}'.format(out_msg))print('Command executing stderr: {:s}'.format(err_msg))return out_msg, err_msgdef is_windows_git_bash():d = 'C:\\Users'return os.path.exists(d) and os.path.isdir(d)def path_to_proper_abs_path(p):p_abs = os.path.abspath(p)if (is_windows_git_bash()):disk, remain = p_abs.split(':')res = "/{:s}/{:s}".format(disk, remain[1:])res = res.replace("\\", "/")else:res = p_absreturn resdef copy_or_link_file(src, dst):# if is_windows_git_bash():# shutil.copyfile(src, dst)# else:if (os.path.exists(dst)):os.remove(dst)elif (os.path.islink(dst)):os.remove(dst)src_abs = path_to_proper_abs_path(src) #os.path.abspath(src)dst_abs = path_to_proper_abs_path(dst) # os.path.abspath(dst)cmd = "ln -sf {:s} {:s}".format(src_abs, dst_abs)CommandRunner.run(cmd, True)def copy_colors_dir(src_color_dir, dst_color_dir):os.makedirs(dst_color_dir, exist_ok=True)for item in os.listdir('colors'):src = 'colors/' + itemdst = dst_color_dir + '/' + itemcopy_or_link_file(src, dst)def get_vim_first_default_runtimepath():if (is_windows()):# return os.path.expanduser('~/vimfiles') # not working on windows 11return os.path.expanduser('~/.vim')elif is_macosx():return os.path.expanduser('~/vimfiles')elif is_linux():return os.path.expanduser('~/.vim')def copy_vim_config():# copy .vimrcsrc = '.vimrc'dst = os.path.expanduser('~/.vimrc')copy_or_link_file(src, dst)vim_cfg_dir = os.path.expanduser('~/.vim')# copy colors dirrtp = get_vim_first_default_runtimepath()copy_colors_dir('colors', rtp + "/colors")# create undodirundodir = '{:s}/temp_dirs/undodir'.format(vim_cfg_dir)os.makedirs(undodir, exist_ok=True)if __name__ == '__main__':copy_vim_config()