真實有效的Vim配置記錄(macOS)

真實有效的Vim配置記錄(macOS)

以前在Ubuntu里配置了兩次Vim,macOS中配置了一次,都沒有配置成,一度放棄了Vim,安心地用Xcode,最近要開始準備省賽了,就抓緊重新配置了一下Vim。
參考了一些不錯的博客:
從零搭建和配置OSX開發環境
Vim中的殺手級插件: YouCompleteMe
Vim配置、插件和使用技巧

Homebrew

在macOS中,有兩款大家常用的管理工具:Homebrew或者MacPorts。這兩款工具都是為了解決同 樣的問題——為OSX安裝常用庫和工具。Homebrew與MacPorts的主要區別,是Homebrew不會破壞OSX 原生的環境,這也是推薦Homebrew的主要原因。同時它安裝的所有文件都是在用戶獨立空間內的,這讓你安裝的所有軟件對于該用戶來說都是可以訪問的,不需要使用sudo命令。
在安裝前,需要去App Store中下載并且安裝Xcode。

安裝方式

管理一下自帶的Ruby庫,直接安裝即可。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

常用命令

brew list                 # 查看已經安裝的包
brew update               # 更新Homebrew自身
brew doctor               # 診斷關于Homebrew的問題(Homebrew 有問題時請用它)
brew cleanup              # 清理老版本軟件包或者無用的文件
brew show ${formula}      # 查看包信息
brew search ${formula}    # 按名稱搜索
brew upgrade ${formula}   # 升級軟件包
brew install ${formula}   # 按名稱安裝
brew uninstall ${formula} # 按名稱卸載
brew pin/unpin ${formula} # 鎖定或者解鎖軟件包版本,防止誤升級

zsh

shell程序是Linux/UNIX系統中的一層外殼,幾乎所有的應用程序都可以運行在shell環境 下,常用的有bashzsh等。
下面命令在/etc/shells文件中查看系統中的各種shell。
cat /etc/shells
zshmacOS系統原生的shell之一,其功能強大,語法相對于bash更加友好和強大,所以推薦 使用zsh作為默認的shell
下面命令切換zsh為默認shell
chsh -s $(which zsh)
可以用Homebrew安裝最新的zsh,同時也會保留原生的zsh

brew install --disable-etcdir zsh
# 將 /usr/local/bin/zsh 添加到下面文件中
sudo vim /etc/shells
# 更換默認shell
chsh -s /usr/local/bin/zsh

參考配置

# modify the prompt to contain git branch name if applicable
git_prompt_info() {
  ref=$(git symbolic-ref HEAD 2> /dev/null)
  if [[ -n $ref ]]; then
    echo " %{$fg_bold[green]%}${ref#refs/heads/}%{$reset_color%}"
  fi
}
setopt promptsubst
export PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[blue]%}%c%{$reset_color%}$(git_prompt_info) %# '

# load our own completion functions
fpath=(~/.zsh/completion $fpath)

# completion
autoload -U compinit
compinit

# load custom executable functions
for function in ~/.zsh/functions/*; do
  source $function
done

# makes color constants available
autoload -U colors
colors

# enable colored output from ls, etc
export CLICOLOR=1

# history settings
setopt hist_ignore_all_dups inc_append_history
HISTFILE=~/.zhistory
HISTSIZE=4096
SAVEHIST=4096

# awesome cd movements from zshkit
setopt autocd autopushd pushdminus pushdsilent pushdtohome cdablevars
DIRSTACKSIZE=5

# Enable extended globbing
setopt extendedglob

# Allow [ or ] whereever you want
unsetopt nomatch

# vi mode
bindkey -v
bindkey "^F" vi-cmd-mode
bindkey jj vi-cmd-mode

# handy keybindings
bindkey "^A" beginning-of-line
bindkey "^E" end-of-line
bindkey "^R" history-incremental-search-backward
bindkey "^P" history-search-backward
bindkey "^Y" accept-and-hold
bindkey "^N" insert-last-word
bindkey -s "^T" "^[Isudo ^[A" # "t" for "toughguy"

# use vim as the visual editor
export VISUAL=vim
export EDITOR=$VISUAL

# load rbenv if available
if which rbenv &>/dev/null ; then
  eval "$(rbenv init - --no-rehash)"
fi

# load thoughtbot/dotfiles scripts
export PATH="$HOME/.bin:$PATH"

# mkdir .git/safe in the root of repositories you trust
export PATH=".git/safe/../../bin:$PATH"

# aliases
[[ -f ~/.aliases ]] && source ~/.aliases

# Local config
[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local

iTerm2

參考 iTerm:讓你的命令行也能豐富多彩
之所以用它完全是因為美觀起見。
iTerm2可以改變配色,在Preferrence/Profile/Colors中可以獲取網上的許多配色方案。而且字體也可以自己改變。
當然,最吸引我的是Hotkey
Preferrence/Keys中可以設置Hotkey,可以在桌面上調出,為半透明。感覺很好看。

Vim

終于到重頭戲了。

基礎配置

在Home目錄創建~/.vim目錄和.vimrc文件。

mkdir ~/.vim
vim .vimrc

.vimrc就是Vim的配置腳本文件了,如果要自己看懂還是不太容易的,需要參考Vim的文檔。
下面放上我的配置文件。

" vundle
if filereadable(expand("~/.vimrc.bundles"))
  source ~/.vimrc.bundles
endif

set nu
set showcmd
set laststatus=1
set magic
set cursorline
set mouse=a
set go=
set showmatch
set matchtime=1
set nobackup
set ruler
set autoindent
set confirm

set tabstop=4
set shiftwidth=4
set smarttab
set nocompatible
set expandtab
set backspace=indent,eol,start

set wildmenu
set fo=cqrt
set laststatus=2
set textwidth=78
set colorcolumn=+1
set ww=<,>,h,l
set noeb visualbell
let mapleader = ","

filetype on
filetype plugin on
filetype indent on
syntax on
syntax enable

" encoding
set fencs=utf-8,usc-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8

" Molokai
colorscheme molokai
highlight NonText guibg=#060606
highlight Folded  guibg=#0A0A0A guifg=#9090D0

" some map
map <F5> :call CR()<CR>
func! CR()
    exec "w"
    exec "!g++ % -o %<"
    exec "! ./%<"
endfunc

map <F10> :call RG()<CR>
func! RG()
    exec "w"
    exec "!g++ % -g -o % <"
    exec "! ./%<"
endfunc

map <F2> :call SetTitle()<CR>
func SetTitle()
let l = 0
let l = l + 1 | call setline(l,'/******************************')
let l = l + 1 | call setline(l,' *File name: '.expand("%"))
let l = l + 1 | call setline(l,' *Author: wzhzzmzzy')
let l = l + 1 | call setline(l,' *Created Time: '.strftime("%c"))
let l = l + 1 | call setline(l,' *TODO:')
let l = l + 1 | call setline(l,'******************************/')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <cstdio>')
let l = l + 1 | call setline(l,'#include <cstring>')
let l = l + 1 | call setline(l,'#include <cstdlib>')
let l = l + 1 | call setline(l,'#include <iostream>')
let l = l + 1 | call setline(l,'#include <string>')
let l = l + 1 | call setline(l,'#include <algorithm>')
let l = l + 1 | call setline(l,'#include <vector>')
let l = l + 1 | call setline(l,'#include <queue>')
let l = l + 1 | call setline(l,'#include <set>')
let l = l + 1 | call setline(l,'#include <map>')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
endfunc

map <F3> :call SetTitle2()<CR>
func SetTitle2()
let l = 0
let l = l + 1 | call setline(l,'/******************************')
let l = l + 1 | call setline(l,' *File name: '.expand("%"))
let l = l + 1 | call setline(l,' *Author: wzhzzmzzy')
let l = l + 1 | call setline(l,' *Created Time: '.strftime("%c"))
let l = l + 1 | call setline(l,' *TODO:')
let l = l + 1 | call setline(l,'******************************/')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <bits/stdc++.h>')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
endfunc

" Nerd Tree
let NERDChristmasTree=0
let NERDTreeWinSize=40
let NERDTreeChDirMode=2
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
let NERDTreeShowBookmarks=1
let NERDTreeWinPos="left"
autocmd vimenter * if !argc() | NERDTree | endif 
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
nmap <F6> :NERDTreeToggle<CR>

" Tagbar
let g:tagbar_width=35
let g:tagbar_autofocus=1
nmap <F7> :TagbarToggle<CR>

" YouCompleteMe
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
nnoremap <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>

Vundle

Vim最麻煩的地方在于插件的配置,所幸有一個叫做Vundle的插件管理工具,用它就能夠簡便非常多。它能夠搜索、安裝、更新和移除vim插件,再也不需要手動管理vim插件。

  • 安裝Vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
  • .vimrc中添加vundle支持
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

而我實際是將Vundle的安裝插件文檔放在了另一個文本.vimrc.bundles中,這需要在.vimrc中加入一段:

" vundle
if filereadable(expand("~/.vimrc.bundles"))
  source ~/.vimrc.bundles
endif

在新的配置腳本中添加上面的vundle支持。
下面的是我的.vimrc.bundles,包含了我需要安裝的插件。

if &compatible
  set nocompatible
end
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" Let Vundle manage Vundle
Bundle 'gmarik/vundle'
" Define bundles via Github repos
Bundle 'christoomey/vim-run-interactive'
Bundle 'Valloric/YouCompleteMe'
Bundle 'croaky/vim-colors-github'
Bundle 'danro/rename.vim'
Bundle 'majutsushi/tagbar'
Bundle 'kchmck/vim-coffee-script'
Bundle 'kien/ctrlp.vim'
Bundle 'pbrisbin/vim-mkdir'
Bundle 'scrooloose/syntastic'
Bundle 'slim-template/vim-slim'
Bundle 'thoughtbot/vim-rspec'
Bundle 'tpope/vim-bundler'
Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-rails'
Bundle 'tpope/vim-surround'
Bundle 'vim-ruby/vim-ruby'
Bundle 'vim-scripts/ctags.vim'
Bundle 'vim-scripts/matchit.zip'
Bundle 'vim-scripts/tComment'
Bundle "mattn/emmet-vim"
Bundle "scrooloose/nerdtree"
Bundle "Lokaltog/vim-powerline"
Bundle "godlygeek/tabular"
Bundle "msanders/snipmate.vim"
Bundle "jelera/vim-javascript-syntax"
Bundle "altercation/vim-colors-solarized"
Bundle "othree/html5.vim"
Bundle "xsbeats/vim-blade"
Bundle "Raimondi/delimitMate"
Bundle "groenewege/vim-less"
Bundle "evanmiller/nginx-vim-syntax"
Bundle "Lokaltog/vim-easymotion"
Bundle "tomasr/molokai"
Bundle "klen/python-mode"
if filereadable(expand("~/.vimrc.bundles.local"))
  source ~/.vimrc.bundles.local
endif
filetype on
  • 安裝插件
    bundle分為三類,比較常用就是第二種:
  1. 在Github vim-scripts 用戶下的repos,只需要寫出repos名稱。
  2. 在Github其他用戶下的repos, 需要寫出”用戶名/repos名”。
  3. 不在Github上的插件,需要寫出git全路徑。
    打開Vim,運行:BundleInstall,或在shell中直接運行vim +BundleInstall +qall
  • 安裝完插件,還有可能會有一個問題,就是Vim版本不夠高
    可以使用以下命令更新。
brew install macvim --override-system-vim

然后用以下命令符號連接到/Application

brew linkapps macvim

最后在.zshrc配置文件中使用別名來使用更新后的vim

Vim常用插件

Vim有許許多多好用的插件,配合起來甚至可以與IDE相比。

NERD Tree

NERD Tree是一個樹形目錄插件,方便瀏覽當前目錄有哪些目錄和文件。
我在.vimrc文件中配置NERD Tree,設置一個啟用或禁用NERD Tree的鍵映射。

nmap <F5> :NERDTreeToggle<cr>

配置之后只需按F5鍵就能啟用或禁用NERD TreeNERD Tree提供一些常用快捷鍵來操作目錄。

通過hjkl來移動光標
o打開關閉文件或目錄,如果想打開文件,必須光標移動到文件名
t在標簽頁中打開
s和i可以水平或縱向分割窗口打開文件
p到上層目錄

YouCompleteMe & syntastic

YouCompleteMe是一個快速、支持模糊匹配的vim代碼補全引擎。它是基于Clang引擎為C/C++/Objective-C提供代碼提示,也支持其他語言代碼提示的引擎,例如基于Jedi的Python代碼補全,基于OmniSharp的C#代碼補全,基于Gocode的Go代碼補全。
只需敲入代碼,就自動提示想輸入的代碼列表,你可以選擇其中一個,然后tab鍵就可以補全代碼。

  • 安裝及編譯
    Vundle中安裝,然后編譯之。
    老版本的YCM是編譯install.sh,但是現在換成了install.py
let g:ycm_global_ycm_extra_conf = 'your path to .ycm_extra_conf.py'
  • 配置
    不同于很多vim插件,YCM首先需要編譯,另外還需要有配置。在vim啟動后,YCM會找尋當前路徑以及上層路徑的.ycm_extra_conf.py。在~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py中提供了默認的模板。
    下面是一個修改過的模板,優化了標準庫找不到的問題。
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
 
import os
import ycm_core
 
# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
#'-Werror',
#'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-stdlib=libc++',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-I',
'.',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',
]
 
# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''
 
if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None
 
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
 
def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )
 
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag
 
    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )
 
    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break
 
      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break
 
    if new_flag:
      new_flags.append( new_flag )
  return new_flags
 
def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
 
def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )
 
def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None
 
    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )
 
    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
    #try:
    #  final_flags.remove( '-stdlib=libc++' )
    #except ValueError:
    #  pass
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
 
  return {
    'flags': final_flags,
    'do_cache': True
  }

這樣,差不多就配置完成了YCM。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 工欲善其事,必先利其器,第一次從頭開始打造自己的vim,特記錄下流程~安裝插件有:bundle, YouCompl...
    tianxinheihei閱讀 1,995評論 0 4
  • 網上已經有很多很好的vim使用和配置教程。但每個人都有自己的習慣,因此教程僅作參考,只有在使用過程中才能體會到具體...
    yiekue閱讀 96,848評論 51 199
  • 因為旅游和小恙,有進半個月沒有做有氧健身操了。看著,肌肉逐漸松散,心里著急啊。 今天,終于恢復了。一身汗,額角的汗...
    Elaine匹閱讀 351評論 0 0
  • 越學越覺得自己的渺小,覺得自己的無知。想想自己走過的三十幾個春秋,未學習之前,學校里學的是知識,強調的是分數...
    伏羲媽媽俞洪華閱讀 360評論 0 0