2013-11-15 26 views
1

例如,我想編寫一個shell腳本來使用vim打開5個文件。在第一個選項卡中只顯示第一個文件,在第二個選項卡中,其餘4個文件顯示在左上角,右上角,左下角,右下角。當前標籤位於第一個標籤中。這個怎麼做?如果我可以用一條線命令來做到這一點,如在命令行中用特定模式打開Vim中的特定文件數

vim -option file -option 

它會很好。但我嘗試了一下,但沒有這樣做。你有什麼建議嗎?


PS。我知道如何在vim中編輯多個文件。我每天都會這樣做。我現在要做的是從bash中同時打開多個文件。是的,我知道在bash中vim的-O和-O選項。但他們以同樣的方式反覆打開文件,這不符合我的要求。我正在尋找的是構建shell腳本以滿足上述要求的方式。謝謝。

+0

你試過了什麼?它是如何失敗的? – romainl

+0

你讀過這個嗎? [鏈接](http://stackoverflow.com/questions/53664/how-to-effectively-work-with-multiple-files-in-vim) – Antarus

回答

4

您可以將這些配置放入自定義腳本文件中,然後可以選擇使用該腳本文件打開您的工作區配置。

腳本文件:

:bl 
:bf 
:tabe 
:n 
:split 
:vsplit 
:wincmd l 
:n 
:wincmd j 
:n 
:n 
:vsplit 
:wincmd l 
:n 
:wincmd h 
:wincmd k 
:tabn 

而且你可以把它作爲這樣的:

vim -S script.vim file1.txt file2.txt file3.txt file4.txt file5.txt 

這將打開VIM在FILE1.TXT,以開放的標籤旁邊,根據您的配置,並在切換到左上角文件(file2.txt)時將光標放在上面。每一行的

加解釋:

注意,當我們分手時,光標會停留在原來的窗口上,並且每個窗口會顯示不同的文件,可以獨立使用:n:N導航。當創建一個新窗口時,它將顯示與我們在創建窗口時所處窗口相同的文件。

正如我的評論所指出的那樣,前兩行是告訴VIM我們已經讀取了每個文件,因此VIM在會話結束時不會抱怨「需要編輯4個文件」。

:bl  Go to last file 
:bf  Go to first file 
:tabe  Create new tab 
:n  Open next file (file2.txt) 
:split Split horizontally (will create new window below with the content of file2.txt) 
:vsplit Split vertically (will create new window on the top right with the content of file2.txt) 
:wincmd l Go to the window to the right (which currently displays file2.txt) 
:n  Open next file (file3.txt) 
:wincmd j Go to window at the bottom 
:n  Open next file (file3.txt, because bottom window was displaying file2.txt) 
:n  Open next file (file4.txt) 
:vsplit Split vertically (will create new window on the right with content file4.txt) 
:wincmd l Go to window to the right (which is bottom right) 
:n  Open next file (file5.txt, because bottom-right window was displaying file4.txt) 
:wincmd h Go to window to the left 
:wincmd k Go to window above (this sets the cursor on file2.txt on top left) 
:tabn  Go to next tab (the first one, displaying file1.txt) 
+0

所以它會從你的bash命令中自動讀取下一個文件tabe分裂和esplit等腳本? – Chong

+1

是的。我在腳本中使用':n'來瀏覽標籤中的多個文件,並在每個標籤中顯示你想要的文件。開頭的':bl:bf'告訴vim我們已經讀過每一個文件,所以在會話結束時它不會抱怨「需要編輯4個文件」。 – justhalf