Skip to content

Instantly share code, notes, and snippets.

@lazydao
Last active June 26, 2024 09:17
Show Gist options
  • Save lazydao/ab00c8bef9f60c8c6063a92c7411edc4 to your computer and use it in GitHub Desktop.
Save lazydao/ab00c8bef9f60c8c6063a92c7411edc4 to your computer and use it in GitHub Desktop.
vim配置
let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
" Run PlugInstall if there are missing plugins
autocmd VimEnter * if len(filter(values(g:plugs), '!isdirectory(v:val.dir)')) | PlugInstall --sync | source $MYVIMRC | endif
call plug#begin('~/.vim/plugged')
" Make sure you use single quotes
Plug 'joshdick/onedark.vim'
Plug 'spin6lock/vim_sproto'
" Git 插件
Plug 'tpope/vim-fugitive'
" 显示 Git 差异的插件
Plug 'airblade/vim-gitgutter'
" 异步语法检查器插件
Plug 'dense-analysis/ale'
" 文件浏览器插件
Plug 'preservim/nerdtree'
" 自动管理 ctags 文件的生成和更新。
Plug 'ludovicchabant/vim-gutentags'
" 下面两个插件都是 python 的,需要编译 vim 支持 python,在 Debian 系下可以执行 `sudo apt install vim-nox` 来安装。
" 代码自动完成引擎
Plug 'ycm-core/YouCompleteMe', { 'do': './install.py' }
" 模糊查找插件
Plug 'Yggdroot/LeaderF', { 'do': ':LeaderfInstallCExtension' }
call plug#end()
" ale
" py linter: ruff, black
let g:ale_fixers = {'python': ['black'],}
let g:ale_linters = {'python': ['ruff'],}
let g:ale_python_black_executable = 'black'
let g:ale_python_ruff_executable = 'ruff'
let g:ale_fix_on_save = 1
" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') | execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
let g:NERDTreeFileLines = 1
" 设置快捷键打开 NERDTree 并聚焦
nnoremap <leader>n :NERDTreeFocus<CR>
" 显示或隐藏 NERDTree
nnoremap <leader>t :NERDTreeToggle<CR>
let g:Lf_WindowPosition = 'popup'
let g:Lf_ShortcutF = '<C-P>'
set termguicolors " 开启24位颜色
set background=dark
colorscheme onedark " 设定主题
" Turn on syntax highlighting.
syntax enable
" Disable the default Vim startup message.
set shortmess+=I
" Show line numbers.
set number
" 不换行
set nowrap
" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2
" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can't backspace before the insertion point set with 'i'.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start
" By default, Vim doesn't let you hide a buffer (i.e. have a buffer that isn't
" shown in any window) that has unsaved changes. This is to prevent you from "
" forgetting about unsaved changes and then quitting e.g. via `:qa!`. We find
" hidden buffers helpful enough to disable this protection. See `:help hidden`
" for more information on this.
set hidden
" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase
" Enable searching as you type, rather than waiting till you press enter.
set incsearch
" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " 'Q' in normal mode enters Ex mode. You almost never want this.
" Disable audible bell because it's annoying.
set noerrorbells visualbell t_vb=
" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a
set expandtab " 将 Tab 转换为空格
set tabstop=4 " 设置 Tab 的宽度为 4 个空格
set shiftwidth=4 " 设置缩进宽度为 4 个空格
set softtabstop=4 " 在插入模式下,按 Tab 键插入 4 个空格
set autoindent " 开启自动缩进
set hlsearch " search highlight
set relativenumber " 开启相对行号
" ctags 是一个代码索引生成工具,用于解析代码文件并生成一个包含各种代码符号(如函数、变量、类等)的位置索引文件。
" 这个索引文件可以用于在代码编辑器中快速导航到符号定义的位置。
" 在大多数系统中可以通过包管理器安装,比如 Ubuntu:`sudo apt install universal-ctags`
" 在你的项目根目录运行 `ctags -R .` 生成 tags 文件。
" 使用 tags 导航:
" 使用 `Ctrl-]` 快捷键跳转到光标下符号的定义。
" 使用 `Ctrl-t` 返回上一个位置。
" 使用 `:tag <symbol>` 命令直接跳转到指定符号。
" 使用 `:ts <symbol>` 命令查找所有匹配的符号。
set tags=./tags,tags;
  • u: Undo
  • Ctrl+R: Redo
  • %s/old/new/g: Replace old with new globally
  • Ctrl+O 会将光标移动到上次您离开的位置
  • zz 当前行移动到屏幕中央,zt,zb
  • di" 删除双引号内的内容,i 代表 inside
  • ci" 重写双引号内的内容,逻辑同上
  • dt" 删除到双引号为止,t 代表 till
  • <leader> 键是一个特殊的按键,用于定义用户自定义快捷键的前缀。
    • 默认情况下,<leader> 键的映射是反斜杠 \。这意味着在定义快捷键时,可以使用 <leader> 键作为前缀,以便访问特定的功能或命令。
  • 在窗口之间移动:Ctrl+w 按键后接 h(左移),j(下移),k(上移),或 l(右移)
  • 调整窗口宽度:
    • 增加宽度:Ctrl+w 然后按 >
    • 减少宽度:Ctrl+w 然后按 <
  • 调整窗口高度(如果是水平分割):
    • 增加高度:Ctrl+w 然后按 +
    • 减少高度:Ctrl+w 然后按 -
  • % 命令可以将光标移动到与当前光标位置匹配的下一个符号处。比如从左括号跳转到对应的右括号。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment