Vim

安装 Vim

1
yum install -y vim-common vim-enhanced vim-minimal vim-filesystem

配置插件管理器 Vundle

Vim配置、插件和使用技巧

安装Vundle

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

vim中的杀手级插件: vundle

配置 ~/.vimrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
cat > ~/.vimrc << EOF
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()

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

" Color schemes
Plugin 'tomasr/molokai'
Plugin 'altercation/vim-colors-solarized'

" Status Color schemes
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

" 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
" Put your non-Plugin stuff after this line
" solarized start
"let g:solarized_termcolors=256
"let g:solarized_termtrans=1
"let g:solarized_contrast="normal"
"let g:solarized_visibility="normal"
"syntax enable
"set background=dark
"colorscheme solarized
" solarized end
" molokai start
let g:molokai_original = 1
let g:rehash256 = 1
colorscheme molokai
" molokai end
" Airline theme start
let g:airline_theme='molokai'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_nr_show = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#whitespace#enabled = 0
let g:airline#extensions#whitespace#symbol = '!'
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
" unicode symbols
let g:airline_left_sep = '»'
let g:airline_left_sep = ''
let g:airline_right_sep = '«'
let g:airline_right_sep = ''
let g:airline_symbols.crypt = '🔒'
let g:airline_symbols.linenr = ''
let g:airline_symbols.linenr = ''
let g:airline_symbols.linenr = '
'
let g:airline_symbols.linenr = ''
let g:airline_symbols.maxlinenr = ''
let g:airline_symbols.maxlinenr = ''
let g:airline_symbols.branch = ''
let g:airline_symbols.paste = 'ρ'
let g:airline_symbols.paste = 'Þ'
let g:airline_symbols.paste = ''
let g:airline_symbols.spell = ''
let g:airline_symbols.notexists = ''
let g:airline_symbols.whitespace = 'Ξ'
" Airline theme end
"
set laststatus=2
set t_Co=256
set encoding=utf-8
set syntax enable
set syntax on
set number
"
EOF

“显示行号
set nummber
“语法高亮度显示
syntax on
“下面两行在进行编写代码时,在格式对起上很有用;
“第一行,vim使用自动对起,也就是把当前行的对起格式应用到下一行;
“第二行,依据上面的对起格式,智能的选择对起方式,对于类似C语言编
“写上很有用
set autoindent
set smartindent
“第一行设置tab键为4个空格,第二行设置当行之间交错时使用4个空格
set tabstop=4
set shiftwidth=4
“设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
set showmatch

安装插件

1
vim +PluginInstall +qall

使用方法

1
2
3
4
5
6
" 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
1
2
3
#安装插件 Install Plugins:

Launch vim and run :PluginInstall

Vim插件

pathogen类似于Vundle

1
2
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
1
2
3
4
5
vi .vimrc

execute pathogen#infect()
syntax on
filetype plugin indent on

vim-airline

vim-airline-themes

nerdtree

nerdtree-git-plugin

Vim主题

Solarized

Solarized Colorscheme for Vim

Molokai

Molokai Color Scheme for Vim

编译安装vim

1.检查vim旧版本,若已存在,将其卸载。

1
2
3
$ vim

$ yum remove vim* -y

2.安装终端字符处理库nucrses

1
$ yum install ncurses-devel -y

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cd /usr/local/src/

$ wget https://codeload.github.com/vim/vim/tar.gz/v8.0.0134

$ tar zxf v8.0.0134

$ cd vim-8.0.0134/

$ ./configure --prefix=/usr/local/vim8

$ make && make install

$ echo $?

安装成功后,通过/usr/local/vim8/bin/vim运行vim命令。将vim命令路径添加到系统PATH环境变量,就可以直接运行vim了。本文不修改/etc/profile文件,通过添加脚本到/etc/profile.d/实现。

1
2
3
4
5
6
7
/usr/local/vim8/bin/vim /etc/profile.d/path.sh
添加以下内容:
#!/bin/bash
export PATH=$PATH:/usr/local/vim8/bin/

$ source /etc/profile.d/path.sh
$ vim

vim-plug

安装

1
2
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

配置

.vimrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cat > ~/.vimrc <<EOF
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
" Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
" Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
" Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
" Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
" Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-master branch
" Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go' ", { 'tag': '*' }

""
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

" Plugin options
" Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
" Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }

" Unmanaged plugin (manually installed and updated)
" Plug '~/my-prototype-plugin'

" Initialize plugin system
call plug#end()
EOF

安装插件

1
2
vim esc模式下
:PlugInstall

autocmd

自动命令,是在指定事件发生时自动执行的命令。利用自动命令可以将重复的手工操作自动化,以提高编辑效率并减少人为操作的差错。

1
2
查看
:autocmd

VIM学习笔记 自动命令(autocmd) - 知乎

自动加载模板文件

1
2
3
4
5
"autocmd BufNewFile *.sh 0 read ~/.vim/template/sh.tpl
autocmd BufNewFile *.sh 0 r ~/.vim/template/sh.tpl

" 自动跳转到末尾
autocmd BufNewFile * normal G
1
2
3
4
5
6
7
8
9
10
11
12
13
autocmd BufNewFile *.sh exec ":call SetTitle()"
    func SetTitle()
        call setline(1,"<?php")
        call append(line("."),"/**")
        call append(line(".")+1, "*   Copyright (C) ".strftime("%Y")." All rights reserved.")
        call append(line(".")+2, "*")
        call append(line(".")+3, "*   FileName      :".expand("%:t"))
        call append(line(".")+4, "*   Author        :xxxx")
        call append(line(".")+5, "*   Email         :xxx@xxx.com")
        call append(line(".")+6, "*   Date          :".strftime("%Y年%m月%d日"))
        call append(line(".")+7, "*   Description   :")
        call append(line(".")+8, "*/")
    endfunc

.vimrc的autocmd用法_PHP_liutingxu1的专栏-CSDN博客

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
" 使用语法高亮
syntax on

" 行前显示行号
set number

" 根据上一行决定新行的缩进
set autoindent
" 键入 <Tab> 时使用空格,使用softtabstop的空格
set expandtab
" 缩进使用的步进单位,以空格计算。下面是4个空格
set shiftwidth=4
""插入 <Tab> 时使用 'shiftwidth'
smarttab

"" 文件里的 <Tab> 代表的空格数
set tabstop=4
"" 编辑时 <Tab> 使用的空格数,使用 'tabstop' 个位置
set softtabstop=4
""set softtabstop=0 " 关闭softtabstop 永远不要将空格和tab混合输入

"" sh conf
" sh 文件缩进
autocmd FileType sh setlocal tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab
" sh 文件模板
autocmd BufNewFile *.sh 0 r ~/.vim/template/sh.tpl
" 打开新文件自动跳转到末尾
autocmd BufNewFile *.sh normal G
"" sh conf

技巧

Vim如何保存需要root权限的文件 , 以普通用户启动的Vim如何保存需要root权限的文件

1
:w !sudo tee %

替换指定行内容,命令模式下操作。

1
:10,20s/^/#/g

参考

Vim: vim_faq.txt

Vim: help.txt

VIM 中文帮助: 常见问题解答

VIM 中文帮助: 总览及快速参考

上古神器vim插件:你真的学会用NERDTree了吗?

编译安装vim-8.0 (centos)

vim 入坑指南(五)插件 Vim-Plug

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Vim
    1. 1.1. 安装 Vim
    2. 1.2. 配置插件管理器 Vundle
      1. 1.2.1. 安装Vundle
      2. 1.2.2. 配置 ~/.vimrc
      3. 1.2.3. 安装插件
      4. 1.2.4. 使用方法
    3. 1.3. Vim插件
      1. 1.3.1. pathogen类似于Vundle
      2. 1.3.2. vim-airline
      3. 1.3.3. vim-airline-themes
      4. 1.3.4. nerdtree
      5. 1.3.5. nerdtree-git-plugin
    4. 1.4. Vim主题
      1. 1.4.1. Solarized
      2. 1.4.2. Molokai
    5. 1.5. 编译安装vim
    6. 1.6. vim-plug
    7. 1.7. autocmd
    8. 1.8. 配置
    9. 1.9. 技巧
      1. 1.9.1. Vim如何保存需要root权限的文件 , 以普通用户启动的Vim如何保存需要root权限的文件
    10. 1.10. 参考
,