Vim 插键及配置

6d9475f6ly1fxspjxxb2pj20hs0cimym.jpg

如果你觉得这个页面广告太多,欢迎移步博客阅读:Vim 插键及配置

编辑器之神 —— Vim

平日使用vim经常编辑文件,想想使用时的痛点,决定研究一下插件的使用。

Vim的扩展通常也被成为bundle或插件。

软件版本:

  • Mac OS X 10.14.1
  • vim 8.1

插件安装-Vundle

众多文章中都提到Vundle,那我就选用它好了!

有一个 Vim 的插键网站,专门有相关插键的配置介绍:VimAwesome

1.将Vundle下载到本地,后面下载的插件也将会下载到~/.vim/bundle路径下。

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

2.插件配置,将如下的内容粘贴到~/.vimrc的顶部(前提是,你本身.vimrc里一开始没有什么其他内容)。

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
" Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
" Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

3.安装插件

将需要安装的插键放入.vimrc文件中即可,例如:

Plugin 'scrooloose/nerdtree' 
"Plugin 'scrooloose/nerdtree'  " 如果暂时不启用,就将该插件注释掉

打开vim,然后输入:

vim # 打开 vim
:PluginInstall
:PluginList # 查看已安装插键列表

4.升级与卸载插件

# 注释下面这段话
# Plugin 'Valloric/YouCompleteMe'

然后

:PluginUpdate # 这个命令本身可以一键升级所有插件
:PlugginClean

参考:

5.帮助文档

:h vundle

NERDTree

Plugin 'scrooloose/nerdtree'

"F2开启和关闭树"
map <F2> :NERDTreeToggle<CR>
let NERDTreeChDirMode=1
"显示书签"
let NERDTreeShowBookmarks=1
"设置忽略文件类型"
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
"窗口大小"
let NERDTreeWinSize=25

python-mode

Plugin 'klen/python-mode' " https://vimawesome.com/plugin/python-mode

编写 Python 文件保存时,就会进行语法检查了:

let g:pymode_rope = 1
let g:pymode_rope_completion = 1
let g:pymode_rope_completion_bind = '<C-p>'  "为了自动补全有效,需要将 set paste 设置不启用
let g:pymode_rope_goto_definition_bind = '<C-c>g'
let g:pymode_python = 'python' " 默认检查 Python2
"Autofix PEP8 errors
nnoremap <leader>f :PymodeLintAuto<CR>

快捷键:

K 显示内置函数文档
<leader>r 运行 python 文件 # let mapleader=", " " 设置 leader 为空格,那么`,+r`就可以运行 python 文件了

参考:

vim-airline

一个状态栏美化工具,颜控必备。附带功能可以一目了然的区分各种编辑状态。

Plugin 'vim-airline/vim-airline' "https://github.com/vim-airlin    e/vim-airline
Plugin 'vim-airline/vim-airline-themes' "  https://github.com/v    im-airline/vim-airline-themes https://github.com/vim-airline/vi    m-airline/wiki/Screenshots

主题预览

To use solarized dark, set :AirlineTheme solarized and add the following to your .vimrc: let g:airline_solarized_bg='dark'

配置:

let g:airline_powerline_fonts = 1
let g:airline_theme='deus'
let g:Powerline_symbols='fancy'
let Powerline_symbols='fancy'
set t_Co=256 " 状态栏就有颜色了

Yggdroot/indentLine

安装:

Plugin 'Yggdroot/indentLine'

配置:

let g:indentLine_enabled = 1
let g:indentLine_color_term = 239
map <leader>m :IndentLinesToggle<CR> # 解决vim复制代码时,缩进线也被复制的问题 https://github.com/Yggdroot/indentLine/issues/261

为了这个插件能够有效果,也是折腾了半天。在 CentOS 平台是正常的,但是在 Mac 上的缩进线显示不正确,为何会这样呢?SOF-Why is apple vim compiled WITHOUT conceal feature?,原来 Mac 上自带的 Vim 版本虽然是8.0版本,但是没有concel这个 Feature ,而indentLine插件要显示对齐线依赖这个,坚线和星号在使用 conceal 功能

那么该怎么添加这个特性呢?搜了一圈,可以重新安装 Vim,可以参考这篇文章安装 Vim。

经过安装设置之后,可以通过vim --version|grep con或者:echo has("conceal")查看是否已经具有 conceal 特性:
006tNbRwly1fxrioguwosj30f902mdg7.jpg

rking/ag.vim

ag 的语法:

ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]

ag 这个 vim 插键主要是基于这个项目 ggreer/the_silver_searcher

ag --list-file-types # 查看支持自定义哪些文件类型

安装了这个插键后,在 vim 的命令模式下,可以使用:Ag [options] {pattern} [{directory}]搜索了。

majutsushi/tagbar

安装 vim 插键之前,机器本身需要ctags

# ubuntu
sudo apt-get install ctags
# centos
sudo yum install ctags
# mac
brew install ctags

在这时使用 vim-tagbar 插件可以帮你快速了解当前文件中的类、方法等。

Plugin 'majutsushi/tagbar' " https://github.com/majutsushi/tagbar

配置:

nmap <F8> :TagbarToggle<CR>

关于 tagbar 的使用,看查看这篇文章 wklken-大纲式导航

YouCompleteMe

目前主要涉及的是 Python 开发,所以,YCM 目前没有配置,如下仅供参考。

Plugin 'Valloric/YouCompleteMe'
sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev
cd ~/.vim/bundle/YouCompleteMe
./install.py --all

cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
"YouCompleteMe配置相关
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

主题

安装主题的方法比较简单,例如需要安装molokai主题,手动安装则按照如下步骤操作:

因为我在 VSCode 和 Iterm2 中都采用了 Dracula 的主题,因此,vim 主题我也同样偏爱它,可以采用插键的方式安装:

Plugin 'dracula/vim'
:PluginInstall
  • 然后在~/.vimrc文件中加入行colorscheme dracula即可。

主题相关的命令:

:colorscheme   "查看当前主题
:colorscheme space tab "列出所有主题
:colorscheme your-theme "切换主题

设置vim

为了让vim使用起来更加得心应手,先做一些简单的配置。

编辑VIM配置文件,可能一开始没有这个文件,不过没关系,直接vi ~/.vimrc保存这个文件即可。

今天学习到<leader> 这个概念,很强大,快捷键很方便!

  • nnoremap 将一个组合快捷键映射为另一个快捷键。

关于leader以及其他map知识,可以查看如下文章:

个人vim配置

参考多人的配置,打造属于自己的Vim配置,这个配置不涉及插件的设置,因为常常生产环境是网络不通的,要迅速配置能用:

let mapleader="," " 设置 leader
let g:mapleader = ','

" 分屏窗口移动, Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" Go to home and end using capitalized directions
" H和L跳转到行首行末, 实在不想按0和$, 太远
noremap H ^
noremap L $

" 命令行模式增强,ctrl - a到行首, -e 到行尾
cnoremap <C-a> <Home>
cnoremap <C-e> <End>

" 去掉搜索高亮
noremap <silent><leader>/ :nohls<CR>

" 快速保存和退出
" Quickly close the current window
nnoremap <leader>q :q<CR>
" Quickly save the current file
nnoremap <leader>w :w<CR>


syntax on " 自动语法高亮
set cursorline " 突出显示当前行
set encoding=utf-8
set fileencoding=utf-8
set fileformat=unix "从Win上复制文件时,避免换行符错误
set hlsearch " 搜索时高亮显示被找到的文本
set ignorecase smartcase " 搜索时忽略大小写,但在有一个或以上大写字母时仍保持对大小写敏感
set incsearch " 输入搜索内容时就显示搜索结果
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set magic " 设置魔术
set nocompatible " 关闭 vi 兼容模式
set number " 显示行号
set paste  " 解决拷贝的时遇到注释会自动注释后续所有行的问题
set ruler " 打开状态栏标尺
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
set softtabstop=4 " 使得按退格键时可以一次删掉 4 个空格
set smartindent " 开启新行时使用智能自动缩进
set tabstop=4 " 设定 tab 长度为 4
set ambiwidth=double " 设置为双字宽显示,否则无法完整显示如:☆

vim配置文件中的注释,末尾用"隔开即可。保留注释,对于了解配置内容有利。

vim配置参考

FAQ

1. github访问速度慢,下载插件失败

2. tmux里面鼠标复制, 无法选中一行内容

mac上iterm2中,你会发现你想复制terminal上的东西的时候,死活复制不了,这时按住 Option (Alt)键就行了。
如果是在Windows平台上利用mobaxterm等工具,tmux也无法复制内容,按住shift键即可。

3. vim 中的内容,mobaxterm无法鼠标复制

注释掉这句话set mouse=a

参考

转载于:https://www.cnblogs.com/michael-xiang/p/10467220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值