Openstack 实现技术分解 (3) 开发工具 — VIM & dotfiles
跟我一起学习VIM - vim插件合集
很全面的vimrc配置技巧
VIM set 指令
VIM is the God of editors, EMACS is God’s editor, 这是一句非常经典的话, 可以看出 VIM 在 editors 圈的地位. 首先需要声明的是, 本人不参与任何 IDE 战争, IDE 的本质追求是提高开发效率, 能够称心如意撸代码就是你最好的选择. 但就 Openstack 开发而言, 我仍会极力推荐使用 VIM, 因为绝大多数的 Openstack 线上生产环境是极其严酷的, 不会纵容你安装和使用重量级 IDE.
那么如何能够快速搭建或者说同步自己的 VIM 编程环境到其他机器上呢?
VIM + dotfiles 就是最佳的组合.
在正文之前先放张 VIM 的快捷键一览图, 大家不妨打印出来贴在自己工位上 : )
Vundle is short for Vim bundle and is a Vim plugin manager.
现在所统计的 VIM 扩展插件多达 4900 多种, 基本上能够很好的满足开发者们各种各样奇葩的要求. 同时, 如何友好的将这些插件应用到自己的开发环境中成为了刚需求. Vundle 就是为此而生的一个 VIM 插件管理工具. 在介绍如何使用 Vundle 之前, 还需要了解一个文件 .vimrc .
.vimrc 是 VIM 的配置文件, 绝对路径为 ~/.vimrc, 是整个 VIM 的灵魂, 拥有非常强大的自定义能力. Vundle 首先会读取 .vimrc 中以关键字 Plugin 开始的语句, 这条语句的值实际上就是插件项目在 Github 上的名称, 然后再实现对插件的 安装/卸载/更新 .
Set up Vundle by manual:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
NOTE: 当然你也可以通过修改 .vimrc 来实现自动安装 Vundle
Configure Plugins: 这里给出 Vundle 的官方配置样例
" 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 安装 Vundle 插件 Plugin VundleVim/Vundle.vim " 下列列出了你所希望管理的 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 youve 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() " 必须 filetype plugin indent on " 开启插件, 必须 " 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
NOTE: 可以看出在语句 call vundle#begin() 和 call vundle#end() 之间就定义了需要安装的插件列表.
Install Plugins:
在定义好需要安装的插件列表之后, 只需要执行下面的指令就可以自动的完成所有插件的安装.
vim +PluginInstall +qall
当然了, 在安装这些插件之前, 我们首先需要知道那些插件是做什么用的, 是否适合自己. 下面继续推荐几个常用的 VIM 插件, 不妨在之后再进行安装.
Solarized 具有阴阳(light/dark)两种风格鲜明的主题和灵活的自定义配色能力, 是最受欢迎的主题插件之一. 安装它只需要对 .vimrc 进行如下编辑:
NOTE 1: 上文已经提到了, 表示插件的 Plugin altercation/vim-colors-solarized 配置语句必须放在call vundle#begin() 和 call vundle#end() 之间, 下面所有的插件同理, 所以不在赘述.
NOTE 2: 这里使用了阴主题 dark, 阳主题的值为 light.
Nerdtree 提供的项目目录结构浏览功能, 极大的加强了开发者对整个项目目录结构的辨识和把控.
" Automatically open a NERDTree if no files where specified autocmd vimenter * if !argc() | NERDTree | endif " Close vim if the only window left open is a NERDTree autocmd bufenter * if (winnr("$") == 1 exists("b:NERDTreeType") b:NERDTreeType == "primary") | q | endif " Open a NERDTree nmap F2 :NERDTreeToggle CR
NOTE 1: 当关闭最后一个文件界面时会同时退出 Nerd tree, 避免多输入一个 :q
NOTE 2: 设置了快捷键 F2 来 Open/Close Nerd tree
Symbol 窗口列出了当前文件中的 宏/全局变量/函数/类 的信息, 使用光标选择就能够跳转相应源代码的位置, 非常便捷.
" Ctrlp ================================================= set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*.png,*.jpg,*.jpeg,*.gif " Ignore for MacOSX/Linux let g:ctrlp_custom_ignore = { \ dir: \v[\/]\.(git|hg|svn|rvm)$, \ file: \v\.(exe|so|dll|zip|tar|tar.gz|pyc)$, \ } let g:ctrlp_match_window = bottom,order:btt,min:1,max:10,results:20 let g:ctrlp_max_height = 30 "let g:ctrlp_user_command = [ " \ .git, cd %s git ls-files . -co --exclude-standard, " \ find %s -type f " \ ] if executable(ag) " Use Ag over Grep set grepprg=ag\ --nogroup\ --nocolor " Use ag in CtrlP for listing files. let g:ctrlp_user_command = ag %s -l --nocolor -g "" " Ag is fast enough that CtrlP doesnt need to cache let g:ctrlp_use_caching = 0 endif let g:ctrlp_working_path_mode=0 let g:ctrlp_match_window_bottom=1 let g:ctrlp_max_height=15 let g:ctrlp_match_window_reversed=0 let g:ctrlp_mruf_max=500 let g:ctrlp_follow_symlinks=1 let g:ctrlp_map = leader p let g:ctrlp_cmd = CtrlP nmap leader f :CtrlPMRU CR
NOTE 1: 这里使用了 ag 搜索来代替 find 指令搜索, 更加高效.
NOTE 2: 设置了 leader+f 快捷键来 Open/Close CtrlP
NOTE 3: leader 键类似于 Home 键, 是组合快捷键的基础, 一般设置为 , 号, 后文会给出该键的设置方法.
" YouCompleteMe ================================================= let g:ycm_autoclose_preview_window_after_completion=1
NOTE 1: 完成补全之后自动关闭预览窗口
" Syntastic ================================================= " configure syntastic syntax checking to check on open as well as save let g:syntastic_check_on_open=1 let g:syntastic_html_tidy_ignore_errors=[" proprietary attribute \"ng-"] let g:syntastic_always_populate_loc_list = 1 let g:syntastic_auto_loc_list = 1 let g:syntastic_check_on_wq = 0 set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%*
" General Config ================================================= set nocompatible " be iMproved, required filetype off " required set number " 显示行号 set ruler " 打开状态栏标尺 set backspace=indent,eol,start " Allow backspace in insert mode set fileencodings=utf-8,gbk " Set encoding of files set history=1000 " Number of things to remember in history set showcmd " Show incomplete cmds down the bottom set showmode " Show current mode down the bottom set showmatch " 输入 )/} 时,光标会暂时的回到相匹配的 (/{ set gcr=a:blinkon0 " Disable cursor blink set novisualbell " No sounds set noerrorbells " No noise set autoread " Reload files changed outside vim set laststatus=2 " 显示状态栏 set statusline+=%{fugitive#statusline()} " Git Hotness set list listchars=tab: .,trail:. " Display tabs and trailing spaces visually set linebreak " Wrap lines at convenient points set nobackup set nowb set tabstop=4 set shiftwidth=4 set textwidth=80 " Make it obvious where 80 characters is highlight ColorColumn ctermbg=gray set colorcolumn=80 set numberwidth=4 set fileformat=unix set expandtab set t_Co=256 set list "set ignorecase set incsearch " 输入搜索内容时就显示搜索结果 au WinLeave * set nocursorline nocursorcolumn " Highlight current line au WinEnter * set cursorline cursorcolumn set cursorline cursorcolumn " 突出当前行和列 " Persistent Undo set undodir=~/.vim/backups set undofile " Search Options set incsearch " Find the next match as we type the search set hlsearch " 搜索时高亮显示被找到的文本 set viminfo=100,f1 " Save up to 100 marks, enable capital marks " Indentation set autoindent set smartindent " 开启新行时使用智能自动缩进 set smarttab set shiftwidth=4 " 设定 和 命令移动时的宽度为 4 set softtabstop=4 " 使得按退格键时可以一次删掉 4 个空格 set tabstop=4 " 设定 tab 长度为 4 set expandtab " Folds set foldmethod=indent " Fold based on indent set foldnestmax=3 " Deepest fold is 3 levels set nofoldenable " Dont fold by default " Leader setting let mapleader = "," " Rebind Leader key " Syntax Highlight syntax on " Run commands that require an interactive shell nnoremap Leader r :RunInInteractiveShell space
最终效果
NOTE: 完整的 .vimrc 文件非常长, 感兴趣的小伙伴请移步到 JMilkFan’s Github
dotfilesdotfiles(点文件) 顾名思义就是文件名前缀带 . 的文件, 因为这类文件在 Linux 中一般为与系统环境相关的隐藏文件(EG. .vimrc/.bashrc/.profile/.bash_profile), 所以在一定程度上 ditfiles 代表了 Linux 系统环境的个性化配置. 简而言之就是, 如果在另外一台计算机中同步了这些 dotfils 就能拥有与你自己的计算机一致的环境设置. 而且 dotfiles + Github 就能够实现只要有网络, 那么所有的计算机都能够变成自己熟悉且习惯的样子.
工作原理:
将这些 “dotfiles” 都放置到同一个目录 dotfiles 中 将 dotfiles 目录上传到 Github 或者任意网络存储设备上 在另外一台计算机上拉下 dotfiles 目录, 并以软链接的方式将 dotfiles 目录中对应的 “dotfiles”文件链接到系统中相应路径中
EXAMPLE:
jmilkfan@JmilkFan-Devstack:~$ git clone https://github.com/JmilkFan/dotfiles.git
NOTE: 这里只是一个仅含有 .vimrc 文件的 dotfiles, 实际上会含有更多的文件, 那么就需要使用到 Bash 来为我们快速的建立软链接了.