2017年12月14日 星期四

Vim tips - Comparing file with hotkey - 揤一个就會使比較仝名檔案

若是頭一擺來遮,建議先來踏話
請共教育部閩南語常用辭典開--咧,隨時會當查。

The following article are written with Taigi. If you prefer English. Please do directly to the middle of this page


為啥乜愛按呢做?

因為工課 ê 關係, 我定定比較兩 ê 仝款名 ê 檔案. 這款情形定定發生佇咧仝一咧程式, 毋過無仝版本 ê 原始碼, . 你有可能用版本控制軟體親像 git, svn, mercury 來共 in 領--出來, 抑是 kā 壓縮 ê 包袱仔 (tarball) 敨--開. 伊的特色是:
  • 這兩 ê 原始碼 ê 目錄結構是仝款
  • 內底 ê 檔案量其約仔名嘛是相siâng
有時我想欲比較看捌的版本寫啥? 就拍開比較軟體, 親像 meld, 抑是 kdiff. 比如講兩个目錄樹 /home/black/A/a/b/f, 和 /home/black/B/a/b/f, 就算你是 GUI, 嘛是愛(準做 TOPA 是 /home/black/A ê 簡單寫, TOPB 是 /home/black/B ê 簡單寫) TOPA/  TOPA/a  ⇒ TOPA/a/b  TOPB/a/b/f, 按呢順伊 ê 路線行落來, 才閣 TOPB 閣行一遍, 才有通看 in ê 比較.

因為我慣勢用 vim, 而且 vim 本身就有檔案比較 ê 功能, 敢有法度佇 vim 裡, 揤--一个都自動來比較咧?

先揣工具

揣出相對路草

欲按怎佇 vim 內, 當你佇編輯抑是寫程式, 比如講咱當佇寫, 欲按怎知影目前 ê 檔案 ê 相對路草咧?
Vim 裡有一咧現成 ê register '@', 準講咱佇 /home/black/A 內底, vi a/b/f

:echo @%
a/b/f

揣出比較ê命令

佇 Vim 裡 ê 比較命令就是

:vertical diffsplit /home/black/B/a/b/f

所以合起來就是:

:vertical diffsplit /home/black/B/@

欲按怎揤一咧都比較?

這時, 咱愛利用 map 來 kā 鍵對應到一咧燒燙燙 ê 鍵, 比如講 F6, 咱 kā 伊寫佇 .vimrc 內底, 按呢逐擺開 vim 伊就會自動對好:

:map <F6> :vertical diffsplit /home/black/B/@

毋過, 按呢有一咧問題: TOPB ê 所在定定改變, 因為無仝 ê 計畫, 無仝版本, 你可能囥足濟版, 按呢你就愛去改 .vimrc, 傷麻煩. 這時咱會使來利用環境變數:
kā .vimrc ê map 改做:


:map <F6> :vertical diffsplit $TOPB/@



咱佇開 vim 進前, 先 kā TOPB 這个變數設好:

$ export TOPB=/home/black/B
$ cd /home/black/A
$ vim a/b/f

按呢咱一揤 F6, 伊就會隨開 /home/black/B/a/b/f 來和你的 f 比較--阿

紲落來下跤个是英文版--ê


Why I would this?

I often do the file comparison, especially the file with the same name. How would this happen? It often happens when you checkout or extract the same software tarball with different version to different directory. Since they are the same software package, they should share the same directory structure.
Sometimes I want to compare the file opened in vim for editing, I have to open the file comparison application, typing or clicking, following the path to the peer file, and the path my editing file. Now that the vim has the comparison capability, why not I use it? And make it easy to do this within vim?

Get the tool required

Get the relative path

Since these two directory has the same file structure, I must know the relative path to the top of source tree, it is good that vim has provided the ready register '@':

:echo @%

The command to compare

If you are editing a file, say "f", where its path "/home/black/A/a/b/f", and you have another source tree at "/home/black/B/a/b/f"
You can use the command in vim to do the file comparing:

:vertical diffsplit /home/black/B/a/b/f

where the other sourec tree top is "/home/black/B".

And the you can combine these two tools:

:vertical diffsplit /home/black/B/%

And it is done.

Making it easy with key mapping and variables

The next step is to make it easy with kep mapping. To do this we can set the TOPB with either environment variables, or specify it in your customized .vimrc, or set it manually with the 'set' command in vim.

I prefer to use the environment variables before I start the vim, so that I can write a script the enclose it. And it would easier to change whenever I change the project.

Here I map the key, here is what I add in my .vimrc

:map <F6>  :vertical diffsplit $TOPB/%

Then when you set the $TOPB environment variable before you sart the vim, bash shell as an example,


$ export TOPB=/home/black/B

$ cd /home/black/A
$ vim f

When you hit the <F6>, it would compare the /home/black/A/a/b/f with your current editing /home/black/B/a/b/f.



沒有留言:

佇 Linux 來看GPX 檔案

最近定定有戶外活動。使用𤆬路機 (GPS) 來記錄行過的路線。普通我記錄路線,攏是用手機仔抑是專門个𤆬路機,罕得用電腦來看。 毋過,"仙人拍鼓有時錯,跤步踏差啥人無"。有一擺我無細膩,袂記得共一擺活動的路線收煞起來,閣直接開始記錄下一擺的活動,按呢共幾落...