Fork me on GitHub
Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

1/09/2014

[Vim] Map Ctrl+S to save file

Beacuse pressing Ctrl-S in terminal will performs an 'XOFF' which stops commands from being receiv

ed. (If Ctrl-S freezes your terminal, type Ctrl-Q to get it going again.)

So, we need to disable that first, put below command in .bashrc

stty stop undef

Then, in the vimrc, add below lines

noremap <silent> <C-S>          :update<CR>
vnoremap <silent> <C-S>         <C-C>:update<CR>
inoremap <silent> <C-S>         <C-O>:update<CR>

It's done. The Ctrl+S has been mapped to :w

References:

  1. In vim how to map “save” to ctrl-s
  2. How to map Ctrl+S in vim on gnome-terminal?

-- EOF --

5/06/2013

Personal wiki based on vimwiki

Recently, I am looking for a simple solution for personal wiki. My basic requirement is no need of database, easy deploy and lightweight.

Finally, I found the vimwiki is a perfect solution with some other services.

vimwiki: Simple syntax, perfectly integrated with vim. Export each single wiki to separate html file.
google-code-prettyfy: Used to make code snippets well formated.
Github pages: Used to host/publish the wiki.
Bootswatch: There are many beautiful and basic templates to beautify your wiki

Below is a brief description about how to setup the personal wiki which utilize above tools. Enjoy them :)


1. Vimwiki installation

https://code.google.com/p/vimwiki/wiki/Installation

  1. Download and unpack vimwiki_N_N.zip. (N_N is version number ie 0_8)
  2. Place unpacked files into (create directory if it isn't exists):
    Linux: ~/.vim/
    Windows: ~/vimfiles/, where ~ is a home directory, usually C:\Documents and Settings\USERNAME\
  3. To install help, open Vim and run :helptags ~/vimfiles/doc command.
  4. Modify vimrc as below, <F4> is mapped to generate html
" // --- vimwiki --- //
let g:vimwiki_list = [{'path': '~/Dropbox/vimwiki/',
			\'template_path': '~/Dropbox/vimwiki/template/',
			\'template_default': 'default',
			\'template_ext': '.html',
			\'path_html': '~/Dropbox/github/vimwiki/'}]

map <F4> :VimwikiAll2HTML<cr>


2. Use google-code-prettify for code highlight

https://code.google.com/p/google-code-prettify/

1. Download code-prettify (ex: prettify-small-4-Mar-2013.tar.bz2)
https://code.google.com/p/google-code-prettify/downloads/list

2. Modify the default vimwiki template
Add below code block before </head> and change <body> to <body onload='prettyPrint()'>

Use your prettify.js and prettify.css instead of http://XXX/prettify.css and ttp://XXX/prettify.js

<link href='http://XXX/prettify.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://XXX/prettify.js' type='text/javascript'/>
<script type='text/javascript'>
document.addEventListener(&#39;DOMContentLoaded&#39;,function() { prettyPrint();});
</script>

The result default.html of vimwiki should looks like as below

<html>
<head>
<link rel="Stylesheet" type="text/css" href="%root_path%%css%">
<title>%title%</title>
<meta http-equiv="Content-Type" content="text/html; charset=%encoding%">
<!-- Bootstrap: Bootswatch template goes here -->
<link href="http://XXX/bootstrap.min.css" rel="stylesheet"
media="screen" type='text/css'>

<!-- google-code-prettify -->
<link href='http://XXX/prettify.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://XXX/prettify.js' type='text/javascript'/>
<script type='text/javascript'>
document.addEventListener(&#39;DOMContentLoaded&#39;,function() { prettyPrint();});
</script>

</head>
<body onload='prettyPrint()'>
%content%
</body>
</html>


3. Use Github pages to host/publish you wiki

http://pages.github.com/

  1. Build a project page for the vimwiki
    https://help.github.com/articles/creating-project-pages-manually

1/14/2012

tmux - A powerful terminal multiplexer

tmux 顧名思義 terminal multiplexer 是一個終端多工器,什麼時候會需要呢?

  1. 當你需要開啟多個終端視窗,但是又不希望底下的視窗列被擠得滿滿
  2. 階層的概念讓你可以將終端依照任務的需求群組起來 (Session -> Window -> Pane)
  3. 使用 ssh 連入主機時,可以方便 user 不需要開啟多個 ssh 連線也可以開啟多個終端視窗

以下就紀錄一下這幾天下來的一些使用心得和筆記

Installation

sudo apt-get install tmux

Hierachy

Session 可以把他想像成 task 的概念,要完成一個 task 可能會需要開啟很多的終端。例如: 開啟 vim 的終端、開啟 git 的終端、開啟 grep 的終端, 等等...。這一個個的終端在 tmux 當中就是一個一個的 Window。所以,Window 算是一個終端基本單元,而 Session 就是 Window 的容器。

tmux 的功能鍵有一個前綴,預設是 Ctrl + b,但是改成 Ctrl + a 應該會比較方便, 下面用 Prefix 表示

tmux new -s <session-name>          // 創建一個 session, 然後進入該 session
tmux kill-session -t <session-name> // 刪除 session

當你建立了一個 session,你可以在任意時間離開它,然後在任意時間回到這個 session 而不遺失任何你離開前開啟的 window。

<Prefix> + d                    // 離開 session, d: detach
<Prefix> + s                    // 列出所有 session, 並可以跳轉
tmux attach -t <session-name>   // 回到 session 

在學會 attach/detach 以後,接著就是如何在 session 裡頭建立 Window

tmux neww -n <window-name>      // 建立新 window
<Prefix> + ,                    // 更改 window 名稱
<Prefix> + 數字鍵                // 跳轉 window
<Prefix> + x                    // 刪除 window

tmux.conf

以上就是一些 tmux 的簡單操作,此外 tmux 有一個 tmux.conf 可以讓使用者將 tmux.conf 客製化成更稱手的兵器。

// remap prefix to Control + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

// set window start from 1
set -g base-index 1

// scrollback buffer n lines
set -g history-limit 5000

// C-a C-a for the last active window
bind-key C-a last-window

// Highlight active window
set-window-option -g window-status-current-bg red

// Default colors
set -g status-bg black
set -g status-fg white

// Left side of status bar
set -g status-left-length 20
set -g status-left '#[fg=green][#[bg=black,fg=cyan]#S#[bg=black,fg=blue,dim]#[fg=green]]'

// <Prefix> - 水平分割視窗
bind - split-window -v
// <Prefix> - 垂直分割視窗
bind | split-window -h

Reference

  1. Practical Tmux
  2. How do I scroll in tmux
  3. Tmux試用感想

-- EOF --

1/08/2012

My Vim Configuration

Title: 我的 vim 進化史 Tags: vim

工具這種東西就是需要用到的時候就去查,然後記錄下來每次要用就看一下,用久了就熟了。分享也順便記錄一下目前 vim 所用到的一些筆記和設定。

vim note

  • Edit

      u           // Undo
      <Cr-R>      // Redo
    
      "dy                     // put this copy into buffer "d"
      Insert mode: <Cr-R>d    // paste from buffer "d"
    
      "ayw        // 複製一個字到 buffer a
      "ap         // 貼上 buffer a 當中的字
      /<Cr-R>a    // 搜尋 buffer a 當中的字
    
      :e          // 在 vim 當中開啟同資料夾下的其他檔案
      :bufdo e!   // refresh all files in buffers
    
      yaw         // yank around word
      ci"         // change inside quote
      ctX         // change till X
    
  • Search

      n           // Find next
      N           // Find previous
    
      *           // Go to next occurrence of word under cursor
      \#          // Go to previous occurrence of word under cursor
    
  • Move

      16G         // 跳到第16行
      1G          // 跳到第1行
      G           // 跳到最後一行
    
      <Cr> E      // 往下捲動一行
      <Cr> Y      // 往上捲動一行
      <Cr> F      // 往下捲動半頁
      <Cr> B      // 往上捲動半頁
    
      <Cr> O      // previous cursor location
      <Cr> I      // next cursor location
    
      gf          // 在當前視窗打開游標下的檔案
      <Cr-W> gf   // 在新的Tab打開游標下的檔案
      gt          // next tab
    
  • Window

      vsp <file.path>     // vertical split window
      :set scrollbind     // set current window scroll bind
    
  • Ctags

      <Cr> ]      // go to definition
      <Cr> t      // previous step
    
  • Cscope

      // 建立 Cscope 資料庫
      find . -name "*.h" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.hpp"  > cscope.files
      cscope -Rbkq
    
      :cs find <parameter> <search-item>
    

vimrc

  • General (一般設定)

      syntax on
      filetype on
    
      set nu
      set confirm
      set modeline
      set showcmd
      set nobackup    
      set hlsearch
      set autoindent
      set noswapfile
      set smartindent
      set nocompatible
    
      set ls=2
      set tabstop=4
      set backspace=2
      set shiftwidth=4
      set encoding=utf-8
      set fileencodings=utf-8,cp950
    
  • Appearance (外觀設定)

      "set textwidth=90
      "set expandtab
      colorscheme desert
    
      " status line appearance (狀態列的設定)
      set statusline=
      set statusline +=\ %n\             "buffer number
      set statusline +=%{&ff}            "file format
      set statusline +=%y%*              "file type
      set statusline +=\ %<%F            "full path
      set statusline +=%m                "modified flag
      set statusline +=%=%5l             "current line
      set statusline +=/%L               "total lines
      set statusline +=%4c\              "column number
      set statusline +=0x%04B\           "character under cursor
    
  • Keys Mapping (快捷鍵設定)

    • Split window (分割視窗)
      用法:按 vv 垂直分割,ss 水平分割

        nnoremap <silent> vv <C-w>v
        nnoremap <silent> ss <C-w>s
      
    • Toggle cursorline/cursorcolumn or center line
      功能:trace code 的時候如果有開輔助線,比較容易跟上游標
      用法:按 F12 一次顯示底線,兩次顯示垂直線,三次兩者都顯示

          nmap <F12> zz
          if version >= 700 " NONE turns off underlining
              highlight CursorLine NONE ctermbg=Yellow
              highlight CursorColumn NONE ctermbg=Yellow
              let s:lico=0
              function LiCo()
                  let s:lico=s:lico>2 ?0 :s:lico+1
                  let &cursorline=s:lico % 2
                  let &cursorcolumn=s:lico / 2
              endfun
              nmap <silent> <F12> :call LiCo()<cr>
          endif
          imap <F12> <c-o><F12>
          vmap <F12> <c-c><F12>gv
      
    • Insert blank line without into insert mode
      功能:每次要新增一個空行,要按 o 再按 Esc 還蠻麻煩的
      用法:按 Enter 在下方新增一個空行, Shift+Enter 則在上方

        map <S-Enter> O<Esc>
        map <CR> o<ESc>k
      
    • Copy/Paste cross session
      功能:在多個開啟的 vim 視窗間複製貼上
      用法:Ctrl+V 選取欲複製之區段, Shift+Y 複製, 跳到另外一個 session 按 Shift+P貼上

        "custom copy'n'paste
        "copy the current visual selection to ~/.vbuf
        vmap <S-y> :w! ~/.vbuf<CR>
        "copy the current line to the buffer file if no visual selection
        nmap <S-y> :.w! ~/.vbuf<CR>
        "paste the contents of the buffer file
        nmap <S-p> :r ~/.vbuf<CR>
      
    • Mark redundant spaces
      功能:移除多餘的 trailing space
      用法:按 F3 標示出多餘空白, 持續按 N 向下搜尋, 按 X 刪除

        function ShowSpaces(...)
            let @/='\v(\s+$)|( +\ze\t)'
            let oldhlsearch=&hlsearch
            if !a:0
            let &hlsearch=!&hlsearch
            else
            let &hlsearch=a:1
            end
            return oldhlsearch
        endfunction
      
        function TrimSpaces() range
            let oldhlsearch=ShowSpaces(1)
            execute a:firstline.",".a:lastline."substitute ///gec"
            let &hlsearch=oldhlsearch
        endfunction
      
        command -bar -nargs=? ShowSpaces call ShowSpaces(<args>)
        command -bar -nargs=0 -range=% TrimSpaces <line1>,<line2>call TrimSpaces()
        nnoremap <F3>     :ShowSpaces 1<CR>
      
    • Show function name
      功能:函數 body 有時候太過龐大,可以不用按 [+{ 就看到函數名稱
      用法:按 , + f 顯示游標所在函數名稱

        fun! ShowFuncName()
          let lnum = line(".")
          let col = col(".")
          echohl ModeMsg
          echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
          echohl None
          call search("\\%" . lnum . "l" . "\\%" . col . "c")
        endfun
        map f :call ShowFuncName() <CR>
      

Plugin

  • Pathogen: 管理套件的套件

    如果你的工作環境會切換不同的電腦的話,那麼實現 vim 套件同步就是一個大問題了,因為總不能每換一次電腦就要重新安裝所有套件吧,那實在太累了。

    概念很簡單,想到同步,那麼會想到利用 git 來管理 .vim 目錄,那麼只要把你的 .vim 放到 github,在不同電腦上就可以 sync 同樣的設定。

    一般安裝許多 plugin 後,檔案會散落在各處,造成 .vim 目錄看起來一團混亂,這時候就是 Pathogen 派上用場的時刻了。

    利用 git submodule 搭配 Pathogen 套件,將每一個套件當做 submodule 管理,在 vim 啟動時透過 Pathogen 將套件載入,乾淨俐落。更詳細的作法可以參閱 Synchronizing plugins with git submodules and pathogen。

  • SuperTab: 不需要什麼設定,也不用產生 tags 檔案,裝好即用的自動補完套件

    SuperTab 補齊所使用的關鍵字是藉由搜尋所有被開啟檔案的內容,所以不需先建立 tags 檔。當你打字打到一半需要它幫你補齊時就按下 Tab 鍵,它就幫你把剩下的字補齊。

    因為,一般需要補齊的東西蠻多都是 local 變數的,所以這個套件相當好用。如果你的意圖是真的要一個 indent 而不是自動補齊,那麼就用 Ctrl+V 再按 Tab。

    另外,如果需要更強大的補齊套件,C/C++ 語言的話,可以參考 OmniCppComplete。

  • MinibufExplorer: 簡單好用的 vim 分頁套件

    如果每用 vim 開一個檔案就要占一個 terminal,那實在太浪費空間啦。所以,有沒有類似瀏覽器分頁效果的套件呢!有的,MinibufExplorer 就是你要的!

    在開啟的 vim 檔案中,使用方式:

      :e <要開啟的檔案路徑>   // 開啟檔案於新分頁
      :b <number>             // 跳到第 <num> 個分頁
      :bd                         // 關閉現在所在的分頁
    
  • Align: 對齊愛好者必裝套件

    有時候利用 tab 來對齊其實還蠻繁瑣的,如果有這個套件輔助,對齊會變的相當簡單俐落。

    使用前:

      int a = 12;
      int blogNum = 3;
      int day = 5;
    

    使用後:

      int a       = 12;
      int blogNum = 3;
      int day     = 5;
    

    安裝方式:

      vim Align.vba.gz    // 用 vim 開啟 Align.vba.gz
      :so %
      :q
    

    使用方式

      :10,20Align =   // 從第10行到第20行,以 = 符號進行對齊
    
  • Ctags, Cscope: C/C++ 開發必裝套件

    這兩套就不用多說了,要實現類似 IDE 強大的 go to definition 的功能,這兩個套件是不可或缺的!

    Ubuntu 下直接安裝方式:

      sudo apt-get install ctags cscope global
    

References

  1. Learn to speak vim – verbs, nouns, and modifiers!: 了解 vim 指令的文法,可以更快速的舉一反三喔!

  2. Writing Vim Plugins

  3. Learn Vimscript the Hard Way

1/01/2012

使用 Markdown 搭配 Vim 寫 Blog

Markdown 是一種非常好用的輕量級標記語言,其易讀易寫並且方便發佈在網路上的特性,使得他越來越被廣泛的使用。所以,記錄一下使用 markdown 和 vim 來寫 blog 所使用到的工具。

  1. 下載 markdown

    Markdown 解譯器是一個 perl 寫成的 script,所以要將寫好的 markdown file 轉成 html,就需要下載這個 script。當然,你要先確定你的系統有 perl 環境才行。

  2. 幫 vim 裝上 markdown 語法高亮度

    參照 Markdown syntax highlighting and conversion in vim 一文,設定好以後,以後開啟 .mkd 檔,vim 就會高亮度標記 markdown syntax 啦!

  3. 將 markdwon 轉換成 html

    只要在 vim 裡面下這行指令,就可以將 markdown 轉換成 html 格式。我把 Markdown.pl 放在 dropbox 當中以便雲端作業。

    :%!~/Dropbox/Markdown.pl --html4tags
    
  4. 學習 markdown 語法 和 一些所見即所得工具

    markdown 的語法其實不多,也相當好記,最重要的應該就是熟練吧!也有一些所見即所得的工具可以讓你即時看到 markdown 轉換成 html 的效果,有助於學習和記憶!

    1. 線上轉換成 html 和 preview 工具

    2. Markdown 語法說名

    3. 所見即所得的 markdown editor

小結

使用 markdown 來撰寫 blog 這種不需要太華麗編排的文章格式已經是綽綽有餘,只是有時後一些眉角還是要注意一下

  1. 像是如果需要在 list 裡面放 code block 那麼就得要 indent 8 spaces 才行

  2. Markdown 的連結有分 行內 和 參考 兩種形式,參考的形式還蠻適合用來寫學術論文或是一些大量引用的文章,可以把一些 reference 集中在一起管理

-- EOF --

10/22/2011

利用 git submodule 和 pathogen 同步 vim 套件

最近因為工作的關係,開始需要頻繁的使用 vim 和 git,果然也驗證了一個道理,若你想要快速的學會某項工具,就把自己丟到一個不能不用那個工具的環境當中,強迫自己必須用最快最有效率的速度上手那套工具,然後再慢慢進階。

每次建置開發環境都得要重新設定 vimrc 檔,以及一些慣用的 plugin,不同主機之間的同步也是個大問題。但是,自從開始使用 git submmodule 和 pathogen 一切都不再是問題,架設 vim 環境和同步不同主機之間的設定都變得輕鬆愉快。

簡單來說,這個 solution 是利用將 .vim 目錄以 git 的方式上傳到 github 上面管理,pathogen 這個 vim 套件則可以 runtime 決定套件的路徑,因此,搭配上 git submodule,套件的管理變得更加井然有序。

有興趣的朋友,我強力推薦這個 教學影片,講解相當的清晰易懂,如果你也為了在多台主機之間同步 vim 設定而煩惱,這絕對是一個一勞永逸的解法!

10/01/2011

[Vim] Vim installation on Windows and Mac

Windows (Use portable GVim)

1. Extract GVim_Portable.exe to Program Files
2. Rename GVimPortable.exe to vi.exe
3. Add path to environment variable PATH
4. Copy the pre-built cscope binary and related dll to folder where vi.exe exists
5. Put wombat.vim to C:\Program Files\GVimPortable\App\vim\vim71\colors
6. Add below at the top of vimrc

colorscheme wombat
set guifont=Monaco:h12

Note:
1. vimrc in C:\Program Files\GVimPortable\App\DefaultData\settings
2. put autoload_cscope.vim and cscope_maps.vim to C:\Program Files\GVimPortable\App\vim\vim71\plugin
3. Don't put the project root folder in path with spaces

MacOS (10.7.1)

1. Download macvim source from github: https://github.com/b4winckler/macvim
2. Download cscope from sourceforge: http://sourceforge.net/projects/cscope/files/
3. Refer the readme to build cscope, maybe you will get some errors like below:

if gcc -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT build.o -MD -MP -MF ".deps/build.Tpo" -c -o build.o build.c; \
    then mv -f ".deps/build.Tpo" ".deps/build.Po"; else rm -f ".deps/build.Tpo"; exit 1; fi
In file included from build.c:50:
/usr/include/ncurses.h:550:40: error: macro "cbreak" passed 1 arguments, but takes just 0
/usr/include/ncurses.h:575:44: error: macro "erasechar" passed 1 arguments, but takes just 0
/usr/include/ncurses.h:611:43: error: macro "killchar" passed 1 arguments, but takes just 0
make[2]: *** [build.o] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

4. How to fix it: Add the #undef and #define lines in the middle of this chunk of code in constants.h

#if (BSD || V9) && !__NetBSD__ && !__FreeBSD__
# define TERMINFO 0 /* no terminfo curses */
#else
# define TERMINFO 1
#endif

#undef TERMINFO
#define TERMINFO 1

#if !TERMINFO
# ifndef KEY_BREAK
# define KEY_BREAK 0400 /* easier to define than to add #if around the use */
# endif
# ifndef KEY_ENTER
# define KEY_ENTER 0401
# endif
# ifndef KEY_BACKSPACE
# define KEY_BACKSPACE 0402
# endif

# if !sun
# define cbreak() crmode() /* name change */
# endif

# if UNIXPC
# define erasechar() (_tty.c_cc[VERASE]) /* equivalent */
# define killchar() (_tty.c_cc[VKILL]) /* equivalent */
# else
# define erasechar() (_tty.sg_erase) /* equivalent */
# define killchar() (_tty.sg_kill) /* equivalent */
# endif /* if UNIXPC */
#endif /* if !TERMINFO */

5. Put the built cscope to usr/local/bin

...

11/21/2010

讓 vim 的 class 和 function 也有語法高亮度 (syntax highlight)

1. 建立檔案 ~/.vim/syntax/c.vim

2. 在 c.vim 當中加入

" Highlight Class and Function names
syn match    cCustomParen    "(" contains=cParen
syn match    cCustomFunc     "\w\+\s*(" contains=cCustomParen
syn match    cCustomScope    "::"
syn match    cCustomClass    "\w\+\s*::" contains=cCustomScope


hi cCustomFunc guifg=yellowgreen
hi cCustomClass guifg=#00FF00

3. 重新啟動 vim

參考資料:
1. class & function names highlighting in Vim - Stack Overflow

...

幫 vim 加上 NERDtree 檔案系統瀏覽功能

一般 IDE 都會有一個類似檔案總館的樹狀資料結構,vim 也可以透過 NERDTree 這個 plugin 達成!

1. 下載 NERDTree

2. 解壓縮至 ~/.vim 資料夾當中,將對應的檔案放到對應的資料夾底下,沒有該資料夾則自行創建

3. 重新啟動 vim

一些使用的技巧如下:

1. 使用 :NERDTreeToggle 開啟/關閉 NERDTree 視窗,也可以設定對應的快速鍵來達成
map :NERDTreeToggle

2. 用 Ctrl + ww 來切換 NERDTree 和原始碼視窗

3. 在 NERDTree 當中用 t 使用新分頁開啟檔案並跳轉過去,使用 T 新分頁開啟檔案但不跳轉過去

其他:

Press i to open the file in a new horizontal split.
Press s to open the file in a new vertical split.
Press p to go to parent directory.
Press r to refresh the current directory.

參考資料:
Command Center: Vim Plugins You Should Know About: NERDTree

...

11/19/2010

幫 vim 加上 C++ code completion 功能

IDE 的自動補完功能是非常方便的,如何讓 vim 也有類似 IDE 那般方便的 auto-completion 呢。以下記錄實作的方式。

1. 在 ~/.vim 下建立資料夾 tags

2. 建立 stdc++ tags, 下載並解壓縮 modified libstdc++ headers 到 ~/.vim/tags/cpp_src

3. 建立 ctags 吧
--c++-kinds=+pl : 為C++文件增加函數原型的標籤, 添加 l 讓局部變數也能自動補完
--fields=+iaS : 在標籤文件中加入繼承信息(i)、類成員的訪問控制信息(a)、以及函數的指紋(S)
--extra=+q : 為標籤增加類修飾符。注意,如果沒有此選項,將不能對類成員補全

$ cd ~/.vim/tags
$ ctags -R --sort=1 --c++-kinds=+pl --fields=+iaS --extra=+q --language-force=C++ -f cpp cpp_src 

如果你想要針對特定使用之函式庫也能有自動補完功能,那麼也需要針對該函式庫建立相對應的 tag 檔案,舉 Qt4 為例 (只針對 QtCore 的部份建立 tags):

$ cd ~/.vim/tags
$ ctags -R --sort=1 --c++-kinds=+pl --fields=+iaS --extra=+q --language-force=C++ -f qt4Core /Library/Frameworks/QtCore.framework/Headers


4. 編輯你的 ~/.vimrc 檔案

" configure tags - add additional tags here or comment out not-used ones
set tags+=~/.vim/tags/cpp
set tags+=~/.vim/tags/qt4Core



" build tags of your own project with Ctrl-L
map <C-L> :!ctags -R --sort=yes --c  -kinds= p --fields= iaS --extra= q .<CR>


" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters
let OmniCpp_MayCompleteDot = 1 " autocomplete after .
let OmniCpp_MayCompleteArrow = 1 " autocomplete after ->
let OmniCpp_MayCompleteScope = 1 " autocomplete after ::
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
set completeopt=menuone,menu,longest,preview

5. 然後就可以開始使用啦,新增局部變數的時候記得 更新一下 tags 檔案,就可以 選擇要自動補完的函數或變數囉!

參考資料:
1. C++ code completion - Vim Tips Wiki
2. Vim and Ctags tips and tricks - Stack Overflow

3.  The Only Winning Move » Code Completion for Qt4 in Vim - Mac OS X Version

...

11/18/2010

為 MacVim 裝上 taglist 套件

vim 是一個強大的文字編輯器,使用 taglist 套件,我們可以概覽程式碼的結構並在各個原始碼文件中跳轉瀏覽,非常方便!

Tag List 插件需要 Ctags 這個程序的支持,然而,系統自帶的 Ctags 功能比較簡單,支持的語言也少(如不支持 C++、Java 等語言),所以推薦安裝更強大的 Exuberant Ctags。

首先到官方網站下載 Exuberant Ctags 的源代碼,最新版本是5.8。

接著解壓到本地並進入文件夾,確保你的 Mac 上面已經安裝了 XCode 之後,就可以開始編譯安裝 Exuberant Ctags:

./configure
make
sudo make install


Exuberant Ctags 就會被安裝在 /usr/local/ 目錄下了,但是由於系統中原先已經內置了Ctags,如果沒有將 /usr/local/bin 添加到系統路徑中的話,默認執行的是 /usr/bin/ctags,可以在命令行中執行:

which ctags

如果返回的不是 /usr/local/bin/ctags,那麼需要修改一下環境變數

echo $PATH 可以印出現在的 PATH 設定。

$ sudo vi /etc/paths 則可以編輯 PATH 設定,將 usr/local/bin 移至 usr/bin 前方,儲存後重開 Terminal。

接著再檢查一次 which ctags 是不是返回 /usr/local/bin/ctags,是的話就正確了。

Exuberant Ctags 安裝完畢之後就可以安裝 Tag List 插件了,在官方網站上下載 Tag List 然後解壓,將 plugin 文件夾中的 taglist.vim 復制到 ~/.vim/plugin/ 中,將 doc 文件夾中的 taglist.txt 復制到 ~/.vim/doc/ 中,如果~目錄下沒有對應的文件夾請自行創建。

最後,處理 Tag List 的幫助文件,先命令行進入 ~/.vim/doc 文件夾,然後啟動 Vim,在 Vim 中執行(注意命令中的 .)

:helptags .

注意:

1. This plugin relies on the Vim "filetype" detection mechanism to determine the type of the current file. You have to turn on the Vim filetype detection by adding the following line to your .vimrc file:

filetype on

2. You can now use the ":TlistToggle" command (previously ":Tlist") to open/close the taglist window. You can use the ":help taglist" command to get more information about using the taglist plugin.

參考資料:

1. Vim Taglist plugin installation: http://vim-taglist.sourceforge.net/installation.html
2. 安裝Exuberant Ctags及Tag List插件: http://aug6th.com/blog/?p=12/

使用方法:

1. Terminal 到你要瀏覽的專案資料夾目錄下,輸入

ctags -R 建立 tag 標籤檔

2. 用 MacVim 開啟一原始檔,並且下

:TlistToggle 開啟 taglist