Skip to content

Vim 配置

约 815 字大约 3 分钟

LinuxVim

2025-01-16

set mouse=r

filetype plugin indent on

" 默认缩进(比如 4 空格)
set tabstop=4
set shiftwidth=4
set expandtab

" 对 JS/TS 使用 2 空格缩进
autocmd FileType javascript,typescript,json,yaml setlocal tabstop=2 shiftwidth=2 expandtab

" 对 Python 使用 4 空格缩进(可以不写,因为默认就是)
autocmd FileType python setlocal tabstop=4 shiftwidth=4 expandtab

" 对 Go 使用实际 tab 缩进
autocmd FileType go setlocal noexpandtab tabstop=4 shiftwidth=4

" ========== 基础设置 ==========
set nocompatible            " 不兼容 Vi,启用 Vim 原生功能
syntax on                   " 启用语法高亮
set number                  " 显示行号
set relativenumber          " 显示相对行号(便于跳转)
set ruler                   " 显示光标位置
set cursorline              " 高亮当前行

set autoindent              " 自动缩进
set smartindent             " 智能缩进

set wrap                    " 自动换行
set linebreak               " 中文单词换行友好

set ignorecase              " 搜索时忽略大小写
set smartcase               " 有大写时自动区分大小写
set incsearch               " 实时搜索
set hlsearch                " 搜索高亮

set backspace=indent,eol,start " 更友好的退格键
set scrolloff=5             " 上下保留 5 行
" set clipboard=unnamedplus   " 使用系统剪贴板

" ========== 文件相关 ==========
" set nobackup                " 不生成备份文件 保存时原文件不保留副本
" set nowritebackup           " 写入文件时不创建临时备份副本
" set noswapfile              " 不生成 .swp 文件
set autoread                " 文件修改后自动读取
filetype plugin indent on   " 启用插件和文件类型缩进

" ========== 记住上次位置 ==========
if has("autocmd")
  autocmd BufReadPost *
        \ if line("'\"") > 0 && line("'\"") <= line("$") |
        \   execute "normal! g`\"" |
        \ endif
endif

" ========== 状态栏 ==========
set laststatus=2            " 总是显示状态栏
set showcmd                 " 显示输入命令
set showmode                " 显示当前模式

" ========== 折叠
set foldmethod=indent
set foldlevel=99     " 启动时默认全部展开
autocmd FileType python setlocal foldmethod=indent foldlevel=99
autocmd FileType javascript setlocal foldmethod=indent foldlevel=99
autocmd FileType go setlocal foldmethod=syntax foldlevel=99


" ========== 插件管理:vim-plug ==========
call plug#begin('~/.vim/plugged')

" 示例插件(根据需要添加)
Plug 'preservim/nerdtree'       " 目录树
" Plug 'jiangmiao/auto-pairs'     " 自动括号
Plug 'tpope/vim-commentary'     " 注释快捷键 gc
Plug 'vim-airline/vim-airline'  " 美化状态栏
Plug 'rafi/awesome-vim-colorschemes'

" JSX语法高亮 + 缩进支持
Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'
" 语法检查(可选)
Plug 'dense-analysis/ale'

call plug#end()

" ========== 快捷键 ==========
nnoremap <C-t> :NERDTreeToggle<CR>   " Ctrl+t 打开目录树


" ========== 颜色方案 ==========
" set termguicolors           " 启用真彩色(需支持)
set background=dark
colorscheme molokai          " 更换为 desert 配色(你也可以改为 molokai, gruvbox 等)


" 定义主题列表(可以按需删减或替换)
let g:my_colorschemes = [
    \ '256_noir',
    \ 'abstract',
    \ 'afterglow',
    \ 'alduin',
    \ 'anderson',
    \ 'angr',
    \ 'ayu',
    \ 'apprentice',
    \ 'archery',
    \ 'atom',
    \ 'carbonized-light',
    \ 'carbonized-dark',
    \ 'challenger_deep',
    \ 'deep-space',
    \ 'deus',
    \ 'dogrun',
    \ 'flattened_dark',
    \ 'flattened_light',
    \ 'focuspoint',
    \ 'fogbell',
    \ 'github',
    \ 'gotham',
    \ 'gruvbox',
    \ 'happy_hacking',
    \ 'iceberg',
    \ 'PaperColor',
    \ 'parsec',
    \ 'scheakur',
    \ 'hybrid',
    \ 'hybrid_material',
    \ 'jellybeans',
    \ 'lightning',
    \ 'lucid',
    \ 'lucius',
    \ 'materialbox',
    \ 'meta5',
    \ 'minimalist',
    \ 'molokai',
    \ 'molokayo',
    \ 'mountaineer',
    \ 'nord',
    \ 'oceanicnext',
    \ 'oceanic-material',
    \ 'one',
    \ 'onedark',
    \ 'onehalf',
    \ 'orbital',
    \ 'paramount',
    \ 'pink-moon',
    \ 'purify',
    \ 'pyte',
    \ 'rdark-terminal2',
    \ 'seoul256',
    \ 'sierra',
    \ 'solarized8',
    \ 'sonokai',
    \ 'space-vim-dark',
    \ 'spacecamp',
    \ 'sunbather',
    \ 'tender',
    \ 'termschool',
    \ 'twilight256',
    \ 'two-firewatch',
    \ 'wombat256'
    \ ]

let g:current_colorscheme_index = -1

function! CycleColorscheme()
  let g:current_colorscheme_index = (g:current_colorscheme_index + 1) % len(g:my_colorschemes)
  let l:next = g:my_colorschemes[g:current_colorscheme_index]
  echo "Colorscheme: " . l:next
  execute 'colorscheme ' . l:next
endfunction

" <leader>ct 绑定为切换主题
nnoremap <leader>ct :call CycleColorscheme()<CR>

function! SetColorschemeByTime()
  " 获取当前小时数(24小时制)
  let l:hour = str2nr(strftime('%H'))

  " 设置主题时间段(7点~18点为白天)
  if l:hour >= 7 && l:hour < 18
    " set background=dark
    colorscheme onedark
  else
    " set background=dark
    colorscheme gruvbox  " 暗色主题(你可以改成 dracula、onedark 等)
  endif
endfunction

" 启动时自动执行
autocmd VimEnter * call SetColorschemeByTime()

" 对 .js 文件启用 JSX 支持
autocmd BufNewFile,BufRead *.js set filetype=javascript.jsx
" 启用 ale 但禁用 eslint
let g:ale_linters = {
\   'typescript': ['eslint', 'tsserver'],
\   'javascript': ['flow', 'tsserver'],
\   'javascript.jsx': ['flow', 'tsserver'],
\}
let g:ale_fixers = {
\   'typescript': ['prettier', 'biome', 'xo', 'eslint', 'tslint']
\}