2020年6月23日 星期二

設定 tabs 寬度與縮排

設定tabs 寬度是: :set tabstop=4 如果要設定縮排,是: :set shiftwidth=4 設完這兩個,縮排就會是 4

2019年4月1日 星期一

Copy Select to System Clipboard

用 V--- Y 選擇和 copy 的內容是在 vi 內部的 clipbord.
所以沒辦法 貼到另一個 app (例如 firefox)

實際上 vi 有提供 copy 到 system clipboard 的方法。
就是.. 原來 y 做 copy 動作
改用 "+y 三個字元來代替。

要 support 這個功能,vim compile option 要包含 xterm_clipboard
:~$ vim --version | grep xterm
+balloon_eval    +folding         +mouse_xterm     +termguicolors
+eval            +mouse_dec       +statusline      +xterm_clipboard
+ex_extra        +mouse_gpm       -sun_workshop    -xterm_save
ref:

2016年8月8日 星期一

find file and open with vi

http://askubuntu.com/questions/586047/how-to-open-a-found-file-with-vi-piping-find-output-to-vi
find /home/user -type f -name 'important'
 -exec vi {} \;
其中有說明..
-exec 後面接的是 要 exec 的命令,一直到 \; 符號為止。
而 {} 代表的是 前面 find 的結果。

2015年7月14日 星期二

bookmarks, go and back

設定 bookmark, 這樣以後可以回到這裡。
設定: ma
(a,b, c....是 bookmark id)
回到... `a
(a, b, c.. 回到 bookmark a, b, c..)

2014年1月15日 星期三

重新設定 indent ..

有時候改人家的 code,為了讓 tab, space, mixing 的 ident 風格 (還有人用 2 space 的 ident..)
可以整齊,就要調整 vi 的 ident 格式..

" a combination of spaces and tabs are " used to simulate tab stops at a width " other than the (hard)tabstop set softtabstop=4

ref: http://stackoverflow.com/questions/1878974/vim-how-to-redefine-tab-as-4-spaces

2013年1月14日 星期一

顯示 java class list

開啟 java source 時,在左邊列出這個 java file 所有的 class, field, method.
plugin 的名子叫 JavaBrowser.

http://www.vim.org/scripts/script.php?script_id=588

Instal:

Download最新版,unzip 解開,找到 vimfile/plugin 下的 JavaBrowser.vim。
copy 到 ~/.vim/plugin

要注意系統要有 ctags。

測試:

開啟一個 java file,在command 輸入: :JavaBrowser

就可以看到左邊被開了一排,顯示 java file 裡所有的 field, method, class..

用 ctrl-w, w 移動到左邊,在對應的宣告名稱按下 enter,
右邊的 source 就會移動到 source line。

同樣的,在 source code 中移動,稍微停頓 1,2 sec,
左邊的 list 會update,停在 source code 的 function name 中。

要關掉的話,一樣下 JavaBrowser 命令,那是一個 toggle 命令

2013年1月4日 星期五

show dos line ending mark ^M

vi 在開啟檔案時,會自動判斷他是 dos 還是 unix 格式。
避免把 dos ending 的 ^M 顯示出來,

所以在檢查一個 檔案 是不是 有 ending mark 時,會看不出來。

可以在 自己 home 的 .vimrc 加上: :set ffs=unix
強制以 unix format 開啟檔案,這樣就可以顯示出 ^M 了。



實際測試,開啟檔案後再下 command 沒效,要加在 .vimrc 裡才行。

2012年5月7日 星期一

gvim : remember the color scheme

color scheme 要寫在 .vimrc 中。
先挑選你要的 color scheme,然後在 .vimrc 加入一行 (以 pablo 為例): colorscheme pablo

ref:

gvim. single instance & open in tab

gvim 是圖形介面的 vim。
但是從 command line 開啟 (就是 follow 以前 vim 的 invoke 方法),每次都會開一個新的 gvim。
要作到像 gedit 一樣,每次都開在同一個 gvim,但是是不同 tab。

要先確認 這個 gvim compile 時,有沒有 +clientserver。

--- 這在, Help --- Version 裡可以看到。一般的 distribution 應該是都有 enable。

然後在 .bashrc 加入: gvim () { command gvim --remote-tab-silent $@ || command gvim $@; } 這樣以後每次在 command line 用 gvim 開啟檔案時,都會自動加上 --remote-tab-silent 選項。

  • 記得修改完 .bashrc 要重新 login 才會生效


ref:
  1. http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instance
  2. http://superuser.com/questions/334794/having-files-opening-in-tabs-in-gvim

2012年1月10日 星期二

ctags

跟 cscope 有點類似。
  1. 先產生 database : ctags -R
  2. 開檔,編輯
  3. 使用 ctag command
好像可以跟 cscope 一起用 (command 不衝突)
command:

到 定義: Ctrl-]
回去,跟cscope 一樣: Ctrl-T
在函数中移动光标的快捷键: [{ 转到上一个位于第一列的"{" }] 转到下一个位于第一列的"{" { 转到上一个空行 } 转到下一个空行 gd 转到当前光标所指的局部变量的定义 * 转到当前光标所指的单词下一次出现的地方 # 转到当前光标所指的单词上一次出现的地方



ref : http://blog.csdn.net/wangyunqian6/article/details/6608238

Show current file name in status line

用 cscope 時需要知道現在是在哪一個 file。
用這個 command 可以把現在編輯的 file show 在 status line: :set ls=2 可以把這個寫在 vimrc 裡。


ref : http://www.unix.com/shell-programming-scripting/132512-continuously-display-file-name-vim.html

2011年10月6日 星期四

Session : remember the state

想要退出,但是待會回來時,要回 vi 時,要回到原來的狀態。
用 :mksession 命令 :mksession 會把目前得的狀態存在目前目錄下的 Session.vim
等一下開啟 vi 時,加上: vi -S session.vim 就會回到剛剛的狀態。

要是目前目錄已經有Session.vim,會提醒你用 ! 確認 overwrite
:mksession!
或是自己指定 session 檔名: :mksession ~/mysession1.vim

cscope : browsing codes with cscope

cscope 要先建 database,
加上 cscope for vim 的 key mapping 才能動作。

  1. 建立要cscope 處理的檔案列表
  2. 要 cscope 根據列表產生 database

cscope 會依照 cscope..files 的內容 (檔案量列表),
parsing context 產生data base。
如果是 linux kernel,
可以用 find command 找出要的 source file。

以下就是以 ARM 為例子的 shell script: #!/bin/sh find . \ -path "arch/arm/*" -prune \ -o -path "include/asm-*" \ ! -path "include/asm-i386*" -prune \ -o -path "tmp*" -prune \ -o -path "Documentation*" -prune \ -o -path "scripts*" -prune \ -o -path "drivers*" -prune \ -o -name "*.[chxsS]" -print > cscope.files 在 kernel source folder 執行完 script ,會在目錄下產生 cscope.files。


接著產生 database..

在目錄下作 cscope -b -q -k
  1. -b : 產生 database 就好,不用 launch gui
  2. -q : 產生 reverse index,方便 search
  3. -k : kernel mode , 不要對 ./usr/include 作 index


做完以上動作,cscope 的 資料就 OK 了。

下載 cscope for vim 的 keymapping 檔。
cscope_maps.vim

download 到 .vim/plugin/ 下
--- 沒有這個目錄就自己 create 一個。


之後就可以像平常一樣開啟任何一個 source file。
然後.. 在任何變數 , function 按 Ctrl \ + g 到 定義的地方。

Ctrl \ + d 列出這個 function 所有呼叫到的 function,讓你挑選。

Ctrl \ + c 到 call 這個 function 的 code 去。


要回到上一個地方用: Ctrl t
可以用 :cs help列出command.... find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name) c: Find functions calling this function d: Find functions called by this function e: Find this egrep pattern f: Find this file g: Find this definition i: Find files #including this file s: Find this C symbol t: Find assignments to
ref:
  1. http://cscope.sourceforge.net/large_projects.html
  2. http://http://forteallan.pixnet.net/blog/post/24394720-cscope-%5B%E7%80%8F%E8%A6%BD%E7%A8%8B%E5%BC%8F%E7%A2%BC%E5%B7%A5%E5%85%B7-built-with-vim%5D"
  3. http://softsmith.blogspot.com/2009/01/vim-cscope-trace.html
  4. http://adrianhuang.blogspot.com/2007/09/trace-code-gvimctagscscope.html

2011年10月5日 星期三

tabs : brows multiple files with tabs

在 vi 的'頭' 顯示一個類似 tabs 的東西,會列出每一個你開啟的 file,
同時可以切換 tabs..

vi -p serial_core.c mxc_uart.c
相關的操作 command: :tabn 下一個 tab

:tabp 上一個 tab

上面的 command 後面還可以加上數字: :tabn 2 後2 個 tab

關掉 tabs (但是檔案還是開著) tabc

ref: http://blog.sriunplugged.com/vi/tab-feature-in-vim/

2011年6月21日 星期二

重新排版 - indent

重新排版功能

1. 先把要重新縮排的區域 Mark 起來
2. 啟動重新縮排

ref : 這一篇 重排所有內容

0 到行首

gg 到檔案開頭

V 進入 visual mode

G 到檔案最後 (就會 mark 到檔案最後)

= 重新縮排 mark 的區域

2011年6月8日 星期三

2011年3月21日 星期一

go to shell, temporary

暫時退出vi,開啟shell
Ctrl-z

這樣就會保留 vi 的 edit sesstion,暫時回到 shell。

當執行玩 shell command 後,用:
$fg
加上 enter

這樣就會回到剛剛的 vi 畫面了。




這樣看來,Ctrl-Z 就跟一般的console一樣,是把vi 放到background..

2011年1月12日 星期三

multiple windows

ref: http://www.cs.swarthmore.edu/help/vim/windows.html

新增一個 垂直的 windows,開啟一個檔案:
: vsplit file

水平的話就用 split。
* vsplit 可以用 vsp 代,split 可以用 sp 代

到另一個 windows (這跟用 diff 時一樣)
Ctrl-W W


關閉這個 window
: hide

好像用 :bd 也可以。


好像有一整組用 Ctrl-W 的 Windows 操作。 ref: http://selinap.com/2009/07/how-to-use-multiple-windows-in-vim/

都是以 Ctrl-W 開始:

開啟,關閉:
  • close window
  • v : split vertically
  • s : split horizontally
移動 cursor:
  • l : 移動 cursor 到右邊的 window
  • h : 移動cursor 到左邊window
  • j : 移動到上面window
  • k: 移動到下面 window
window size 調整:
  • + : windows size 增加一行 (horizontal 時有效)
  • - : windows size 減一行
  • > : 增加size (vertical 時有效)
  • < : 減少size.

2010年12月26日 星期日

Change vimdiff color


hi DiffAdd term=reverse cterm=bold ctermbg=green ctermfg=white
hi DiffChange term=reverse cterm=bold ctermbg=cyan ctermfg=black
hi DiffText term=reverse cterm=bold ctermbg=gray ctermfg=black
hi DiffDelete term=reverse cterm=bold ctermbg=red ctermfg=black


因為diff convert 的某 background C comment 的顏色一樣,所以會看不到 code,
所以改 .vimrc。
用上面的。

ref: http://kimklai.blogspot.com/