Linux下的编辑器以vim和emacs为主流,一个编辑器之神,一个是神的编辑器。
本文以主要介绍如何在linux下以vim为基础搭建一个比较顺手的代码编辑器。
- 有两种比较流行的方式:
自动安装
手动安装
- 自动安装
这种是方法是比较省事的方法,只要一个.vimrc配置文件就可以搞定所有的事情,一次配好即可。以后只要有这个配置文件,就可以走遍天下。
这种方式需要使用一个VIM插件管理工具来自动管理VIM插件的安装与卸载,笔者使用的Vundle来管理VIM插件,Vundle的全称是Vim Bundle,它是一款Vim插件管理工具。Vundle让你可以非常轻松地安装、更新、搜索和清理Vim插件。它还能管理你的运行时环境,并帮助标记。
-
- 可以在Github上下载Vundle: https://github.com/VundleVim/Vundle.vim
下载完成后,解压到用户根目录的.vim目录下即可
-
- 在终端(命令行)中使用git clone获取: git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
clone完成后, 会在用户根目录下多出来一个bundle目录,此时Vundle已经安装完成。安装好Vundle以后,就可以根据自己的需要安装插件了。Vundle提供了几种不同的插件安装方式,一般我们用到的插件在github都可以找到,因此最常用的一般就是直接找github上插件对应的仓名,添加到.vimrc中即可。然后打开vim,执行:PluginInstall命令,即可自动安装,等待Vundle执行完下载安装操作后,就可以开始享受VIM了。
笔者的.vimrc文件配置如下:
1 "=============================================================================================== 2 set nocompatible " be iMproved, required 3 filetype off " required 4 5 " set the runtime path to include Vundle and initialize 6 set rtp+=~/.vim/bundle/Vundle.vim 7 call vundle#begin() 8 " alternatively, pass a path where Vundle should install plugins 9 "call vundle#begin('~/some/path/here') 10 11 " let Vundle manage Vundle, required 12 Plugin 'VundleVim/Vundle.vim' 13 14 " The following are examples of different formats supported. 15 " Keep Plugin commands between vundle#begin/end. 16 " plugin on GitHub repo 17 Plugin 'tpope/vim-fugitive' 18 19 " plugin from http://vim-scripts.org/vim/scripts.html 20 Plugin 'L9' 21 22 " Git plugin not hosted on GitHub 23 "Plugin 'git://git.wincent.com/command-t.git' 24 25 " git repos on your local machine (i.e. when working on your own plugin) 26 "Plugin 'file:///home/gmarik/path/to/plugin' 27 28 " The sparkup vim script is in a subdirectory of this repo called vim. 29 " Pass the path to set the runtimepath properly. 30 "Plugin 'rstacruz/sparkup', {'rtp': 'vim/'} 31 32 " Install L9 and avoid a Naming conflict if you've already installed a 33 " different version somewhere else. 34 "Plugin 'ascenator/L9', {'name': 'newL9'} 35 36 " All of your Plugins must be added before the following line 37 38 "============================ my plugins start ============================= 39 Plugin 'scrooloose/nerdtree' 40 Plugin 'kien/ctrlp.vim' 41 Plugin 'vim-airline/vim-airline' 42 Plugin 'vim-airline/vim-airline-themes' 43 Plugin 'majutsushi/tagbar' 44 Plugin 'tacahiroy/ctrlp-funky' 45 Plugin 'jlanzarotta/bufexplorer' 46 Plugin 'easymotion/vim-easymotion' 47 Plugin 'haya14busa/incsearch.vim' 48 Plugin 'dkprice/vim-easygrep' 49 Plugin 'dyng/ctrlsf.vim' 50 Plugin 'Xuyuanp/nerdtree-git-plugin' 51 Plugin 'hfts/Porsche' 52 Plugin 'vim-scripts/OmniCppComplete' 53 Plugin 'vim-scripts/AutoComplPop' 54 Plugin 'scrooloose/nerdcommenter' 55 56 "============================ my plugins end =============================== 57 58 call vundle#end() " required 59 filetype plugin indent on " required 60 " To ignore plugin indent changes, instead use: 61 "filetype plugin on 62 " 63 " Brief help 64 " :PluginList - lists configured plugins 65 " :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate 66 " :PluginSearch foo - searches for foo; append `!` to refresh local cache 67 " :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal 68 " 69 " see :h vundle for more details or wiki for FAQ 70 " Put your non-Plugin stuff after this line 71 72 "========== for vim ============== 73 syntax enable " 开启语法高亮 74 set t_Co=256 " 开启256色显示 75 set scrolloff=3 " 滚动时保持边距5行 76 set number " 开启行号显示 77 set mouse=a " 开启鼠标 78 set cmdheight=1 79 set nocompatible 80 set confirm " 在处理未保存或只读文件的时候,弹出确认 81 set autoindent " 自动缩进 82 set tabstop=4 " Tab键的宽度 83 set expandtab " 展开tab为空格 84 set softtabstop=4 " 统一缩进为4 85 set shiftwidth=4 86 filetype plugin indent on "打开文件类型检测, 加了这句才可以用智能补全 87 set completeopt=longest,menu 88 set hlsearch " 高亮搜索 89 set laststatus=1 " 始终显示状态栏 90 set encoding=utf-8 " 91 set ignorecase " 搜索忽略大小写 92 set nopaste " 切换到正常模式 93 set list lcs=tab:¦ " 显示对齐线 | ¦ ┆ │ 94 95 colorscheme Porsche 96 97 set cursorline 98 "hi cursorline cterm=none term=none 99 "autocmd WinEnter * setlocal cursorline 100 "autocmd WinLeave * setlocal nocursorline 101 "highlight CursorLine guibg=#30F010 ctermbg=189 102 103 "自动补全 104 :inoremap ( ()<ESC>i 105 :inoremap ) <c-r>=ClosePair(')')<CR> 106 :inoremap { {<CR>}<ESC>O 107 :inoremap } <c-r>=ClosePair('}')<CR> 108 :inoremap [ []<ESC>i 109 :inoremap ] <c-r>=ClosePair(']')<CR> 110 :inoremap " ""<ESC>i 111 :inoremap ' ''<ESC>i 112 function! ClosePair(char) 113 if getline('.')[col('.') - 1] == a:char 114 return "<Right>" 115 else 116 return a:char 117 endif 118 endfunction" 119 120 "========== for quick key ======= 121 " F11默认时全屏 122 nmap <F2> :NERDTreeToggle<cr> 123 nmap <F3> :TagbarToggle<CR> 124 nmap <F4> :ToggleBufExplorer<cr> 125 nmap <F5> :CtrlPFunky<cr> 126 nmap <F10> :set paste<cr> " 切换到粘贴模式 127 nmap <F12> :source ~/.vimrc<cr> 128 nmap <silent> <leader>ll :colorscheme Porsche<cr> 129 nmap <silent> <leader>jj :colorscheme space-vim-dark<cr> 130 nmap <silent> <leader>nm :set nonumber<cr> 131 nmap <silent> <leader>mn :set number<cr> 132 nmap <silent> <leader>ag :Ag<cr> 133 nmap <silent> <leader>ff ::shell<cr> 134 135 "========== for NERDTree ============== 136 "let g:NERDTree_title='NERD Tree' 137 "let g:winManagerWindowLayout='NERDTree|TagList,Tarbar' 138 autocmd vimenter * NERDTree 139 nmap wm :NERDTreeToggle<cr> 140 autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") ) | q | endif 141 function! NERDTree_Start() 142 exec 'NERDTree' 143 endfunction 144 function! NERDTree_IsValid() 145 return 1 146 endfunction 147 nmap <silent> mt :if IsWinManagerVisible() <BAR> WMToggle<CR> <BAR> else <BAR> WMToggle<CR>:q<CR> endif "<CR> 148 149 "========== for CtrlP =================== 150 let g:ctrlp_map = '<c-p>' 151 let g:ctrlp_cmd = 'CtrlP' 152 let g:ctrlp_working_path_mode = 'r' 153 let g:ctrlp_match_window = 'bottom,order:ttb,min:1,max:15,results:100' 154 let g:ctrlp_tabpage_position = 'al' 155 let g:ctrlp_working_path_mode = 'r' 156 let g:ctrlp_reuse_window = 'netrw|help|quickfix' 157 let g:ctrlp_open_new_file = 't' 158 let g:ctrlp_open_multiple_files = 'tjr' 159 let g:ctrlp_arg_map = 1 160 let g:ctrlp_extensions = ['tag', 'buffertag', 'quickfix', 'dir', 'rtscript', 161 'undo', 'line', 'changes', 'mixed', 'bookmarkdir'] 162 set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*.png,*.jpg,*.jpeg,*.gif " MacOSX/Linux 163 let g:ctrlp_custom_ignore = 'v[/].(git|hg|svn)$' 164 165 if executable('ag') 166 " Use Ag over Grep 167 set grepprg=ag --nogroup --nocolor 168 " Use ag in CtrlP for listing files. 169 let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""' 170 " Ag is fast enough that CtrlP doesn't need to cache 171 let g:ctrlp_use_caching = 0 172 endif 173 174 "========== for CtrlPFunky ============== 175 nnoremap <Leader>u :execute 'CtrlPFunky ' . expand('<cword>')<Cr> 176 let g:ctrlp_extensions = ['funky'] 177 let g:ctrlp_funky_syntax_highlight = 1 178 let g:ctrlp_funky_matchtype = 'path' 179 let g:ctrlp_funky_nerdtree_include_files = 1 180 181 "========== for vim-airline ============== 182 let g:airline_theme="light" "light 183 let g:airline#extensions#tabline#enabled = 1 184 let g:airline#extensions#tabline#left_sep = ' ' 185 let g:airline#extensions#tabline#left_alt_sep = '|' 186 187 "========== for EasyMotion ============== 188 " <Leader>f{char} to move to {char} 189 map <Leader>f <Plug>(easymotion-bd-f) 190 nmap <Leader>f <Plug>(easymotion-overwin-f) 191 " s{char}{char} to move to {char}{char} 192 nmap s <Plug>(easymotion-overwin-f2) 193 " Move to line 194 map <Leader>L <Plug>(easymotion-bd-jk) 195 nmap <Leader>L <Plug>(easymotion-overwin-line) 196 " Move to word 197 map <Leader>w <Plug>(easymotion-bd-w) 198 nmap <Leader>w <Plug>(easymotion-overwin-w) 199 200 "========== for insearch ============== 201 map / <Plug>(incsearch-forward) 202 map ? <Plug>(incsearch-backward) 203 map g/ <Plug>(incsearch-stay) 204 nnoremap <Esc><Esc> :<C-u>nohlsearch<CR> 205 "set incsearch 206 set hlsearch 207 let g:incsearch#auto_nohlsearch = 0 208 map n <Plug>(incsearch-nohl-n) 209 map N <Plug>(incsearch-nohl-N) 210 map * <Plug>(incsearch-nohl-*) 211 map # <Plug>(incsearch-nohl-#) 212 map g* <Plug>(incsearch-nohl-g*) 213 map g# <Plug>(incsearch-nohl-g#) 214 215 "=========== for NERDTREE-GIT-PLUGIN ===== 216 let g:NERDTreeIndicatorMapCustom = { 217 "Modified" : "✹", 218 "Staged" : "✚", 219 "Untracked" : "✭", 220 "Renamed" : "➜", 221 "Unmerged" : "═", 222 "Deleted" : "✖", 223 "Dirty" : "✗", 224 "Clean" : "✔︎", 225 "Unknown" : "?" 226 } 227 228 "=========== cscope =============== 229 if has("cscope") 230 set csprg=/usr/local/bin/cscope 231 set csto=0 232 set cst 233 set nocsverb 234 " add any database in current directory 235 if filereadable("cscope.out") 236 cs add cscope.out 237 " else add database pointed to by environment 238 elseif $CSCOPE_DB != "" 239 cs add $CSCOPE_DB 240 endif 241 set csverb 242 endif 243 nmap <C->s :cs find s <C-R>=expand("<cword>")<CR><CR> 244 nmap <C->g :cs find g <C-R>=expand("<cword>")<CR><CR> 245 nmap <C->c :cs find c <C-R>=expand("<cword>")<CR><CR> 246 nmap <C->t :cs find t <C-R>=expand("<cword>")<CR><CR> 247 nmap <C->e :cs find e <C-R>=expand("<cword>")<CR><CR> 248 nmap <C->f :cs find f <C-R>=expand("<cfile>")<CR><CR> 249 nmap <C->i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR> 250 nmap <C->d :cs find d <C-R>=expand("<cword>")<CR><CR> 251 252 "=========== omnicppcomplete =============== 253 let OmniCpp_GlobalScopeSearch = 1 254 let OmniCpp_NamespaceSearch = 2 255 let OmniCpp_DisplayMode = 1 256 let OmniCpp_ShowScopeInAbbr = 1 257 let OmniCpp_ShowPrototypeInAbbr = 1 258 let OmniCpp_ShowAccess = 1 259 let OmniCpp_MayCompleteDot = 1 260 let OmniCpp_MayCompleteArrow = 1 261 let OmniCpp_MayCompleteScope = 1 262 let OmniCpp_MayCompleteScope = 1 263 let OmniCpp_SelectFirstItem = 1 264 let OmniCpp_DefaultNamespace=["std"] 265 let OmniCpp_SelectFirstItem = 2 266 267 "===============NERD Commenter========================= 268 let g:NERDSpaceDelims = 1 " Add spaces after comment delimiters by default 269 let g:NERDCompactSexyComs = 1 " Use compact syntax for prettified multi-line comments 270 let g:NERDDefaultAlign = 'left' " Align line-wise comment delimiters flush left instead of following code indentation 271 let g:NERDAltDelims_java = 1 " Set a language to use its alternate delimiters by default 272 let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } } " Add your own custom formats or override the defaults 273 let g:NERDCommentEmptyLines = 1 " Allow commenting and inverting empty lines (useful when commenting a region) 274 let g:NERDTrimTrailingWhitespace = 1 " Enable trimming of trailing whitespace when uncommenting 275 "===============================================================================================
- 手动安装
手动安装,当然是要自己一个一个地安装插件了。这种方式的特点是需要自己一个一下下载vim插件,然后手动进行安装。虽然比较费事,但如果之前有vim插件安装包的备份文件,那就可以不依赖于网络。所以还是推荐在自己的本地留一份常用插件的安装包备份。手动安装的过程比较简单,基本上就是“下载-解压”,先从网卡下载好插件的压缩包,一般来说直接解压到.vim目录中即可。
笔者常用的几个插件如下,一般都可以在github上找到,其实从上面的配置文件中就可以找到笔者使用了哪些vim插件。
Plugin 'scrooloose/nerdtree'
Plugin 'kien/ctrlp.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'majutsushi/tagbar'
Plugin 'tacahiroy/ctrlp-funky'
Plugin 'jlanzarotta/bufexplorer'
Plugin 'easymotion/vim-easymotion'
Plugin 'haya14busa/incsearch.vim'
Plugin 'dkprice/vim-easygrep'
Plugin 'dyng/ctrlsf.vim'
Plugin 'Xuyuanp/nerdtree-git-plugin'
Plugin 'hfts/Porsche'
Plugin 'vim-scripts/OmniCppComplete'
Plugin 'vim-scripts/AutoComplPop'
Plugin 'scrooloose/nerdcommenter'
最后,附上笔者vim的两张截图: