Skip to content

Instantly share code, notes, and snippets.

@lazydao
Last active June 26, 2024 09:17
Show Gist options
  • Save lazydao/b4e89d079538bd34966c5aab630ef273 to your computer and use it in GitHub Desktop.
Save lazydao/b4e89d079538bd34966c5aab630ef273 to your computer and use it in GitHub Desktop.
neovim
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 'folke/tokyonight.nvim'
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 tokyonight " 设定主题
" 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;

安装

参考:https://github.com/neovim/neovim/blob/master/INSTALL.md

推荐做法:

  • 下载最新版本 wget https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
  • 解压 tar -zxvf nvim-linux64.tar.gz
  • 通过 ln -s 将解压后的 nvim 可执行文件导入 PATH,例如 sudo ln -s ~/nvim-linux64/bin/nvim /usr/local/bin/nvim

配置

因为迁移的 vim 配置,所以继续使用 plug 管理插件,参见 init.vim

:checkhealth 检查配置状况

环境依赖

sudo apt install vim-nox
pip install pynvim pylama
sudo apt install exuberant-ctags

一些快捷键

主要列举配合插件使用的,自带的可以去看 vim 那边的 tips。

可以用:map查看已绑定快捷键。

默认情况下,<leader> 键的映射是反斜杠 \

快捷键 动作
[d Jump to the previous diagnostic(neovim专属)
]d Jump to the next diagnostic(neovim专属)
<C-P> * :LeaderfFile
<leader>b * :LeaderfBuffer
<leader>n :NERDTreeFocus
<leader>t * :NERDTreeToggle
[c @(GitGutterPrevHunk)
]c @(GitGutterNextHunk)
Ctrl-] 跳转到光标下符号的定义。
Ctrl-t 返回上一个位置。
:tag <symbol> 直接跳转到指定符号。
:ts <symbol> 查找所有匹配的符号。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment