Tuesday, November 30, 2010
介紹好用工具:Junction ( Windows 版的 symbolic link )
"NTFS 連接點" ( NTFS junction point ) 是 NTFS 檔案系統提供的一種能力,可以讓某一個實體目錄(Target) 直接 Link (連結) 到另一個 對應的目錄(Junction Directory),這與建立「捷徑(ShortCut)」是個截然不同的概念。
你所建立的 "NTFS 連接點" 目錄跟你原本的目錄內容,完全一模一樣 (包括安全性設定),因為實際上這兩個目錄下的檔案是「完全相同」的一份資料,只是透過了 "NTFS 連接點" 建立了兩個目錄之間的連結。
而建立 "NTFS 連接點" 最大的好處就是這是一個「感覺」完全獨立的目錄,對「檔案總管」或「任何其他的程式」來說,根本不知道這目錄到底是不是一個 NTFS 連接點,而可以直接進行檔案、目錄操作。
再者,有些舊有的程式只能讀取固定目錄下的資料,若因為 C 槽硬碟空間不夠而將資料搬移後,程式可能就無法運作了,這時就可以透過 Junction 工具幫你建立 "NTFS 連接點",省時又省力!
Junction 的使用說明如下:
Junction v1.05 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2007 Mark Russinovich
Systems Internals - http://www.sysinternals.com
The first usage is for displaying reparse point information, and the
second usage is for creating or deleting a NTFS junction point:
usage: junction [-s] [-q]
-q Don't print error messages (quiet)
-s Recurse subdirectories
usage: junction [-d]
-d Delete the specified junction
example: junction d:\link c:\winnt
舉幾個簡單的例子。若你的實體目錄在 D:\MyDocument 而你要建立一個 "NTFS 連接點" 到 C:\Profiles\User1\MyDocs 目錄的話,就可以用以下指令完成:
junction c:\Profiles\User1\MyDocs "d:\tmp\My Documents"
這時你會得到以下訊息:
Junction v1.05 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2007 Mark Russinovich
Systems Internals - http://www.sysinternals.com
Created: c:\Profiles\User1\MyDocs
Targetted at: d:\tmp\My Documents
代表你已經成功建立 C:\Profiles\User1\MyDocs 目錄,而這個目錄是一個 "NTFS 連接點"。從檔案總管中,你完全無法看出這目錄到底是「真實目錄」或是「NTFS 連接點」,但你可以透過「命令提示字元模式」以 dir 指令查看此目錄是不是一個 "NTFS 連接點" (如下圖示)。
若要刪除 "NTFS 連接點" 我發現有三種方式,要是你選錯方式,你的資料就會全部不見,所以使用上要小心謹慎,不要因為偷懶就用檔案總管進行刪除動作:
1. 使用標準的Junction 工具程式進行刪除 ( 建議用法 )
junction -d c:\Profiles\User1\MyDocs
2. 使用檔案總管進行刪除 ( 將"NTFS 連接點" 目錄丟進「資源回收筒」 ),當你用了這招刪除,事實上只是將 "NTFS 連接點" 移到「資源回收筒」而已,所以 "NTFS 連接點" 其實是還在的,所以你的來源目錄裡的檔案還會存在,不過當你「清空資源回收筒」時,包含你的原本的目錄下所有檔案都會一併被刪除!
3. 使用檔案總管進行刪除 ( 使用 Shift + Delete 直接刪除 ),這也是最無藥可救的方式 (其實有藥),一次刪除殆盡,包含你的原本的目錄下所有檔案都會一併被刪除!
在特別提醒一點,如果你從 "NTFS 連接點" 的上層目錄開始刪除的話,你原本資料夾下的目錄一樣會消失喔。又如果你因為使用了 "NTFS 連接點" 而「忘記」有這件事還刪除此目錄的話,那真的算你倒楣了,檔案會全數消失!
如果不放心,還是建議各位不要使用,以免檔案遺失而不自知。不過對於一些罕見的使用情境,確實是一個很棒的解決方案。
http://blog.miniasp.com/post/2009/02/11/Using-Junction-tool-to-make-symbolic-links-in-Windows.aspx
Xming is the leading X Window Server for Microsoft XP/2008/Windows7
http://www.straightrunning.com/XmingNotes/
Installing Wine on Ubuntu
Alternative Command Line Instructions for Installing Wine:
It is also possible to add the Wine PPA and install via the terminal. This may be useful on Kubuntu, Xubuntu, and other Ubuntu derivatives.
sudo add-apt-repository ppa:ubuntu-wine/ppa
Then update APT's package information by running 'sudo apt-get update'. You can now install Wine by typing 'sudo apt-get install wine'.
If you'd like to browse the PPA manually, you can visit its Launchpad page.
http://www.winehq.org/download/deb
Geek / Hacker 與一般的 user / programmer 的分野並不大
- 只是專注的範疇與態度有異
http://blog.linux.org.tw/~jserv/archives/001750.html
我發現駭客都有不喜歡受限的特質
這不是量的差異,這是質的分歧
http://www.javaworld.com.tw/roller/qing/entry/2006_4_17_Hackers_and_Artists
《駭客與畫家》
《駭客與畫家》基本上是一本散文集,它收錄了作者的十五篇文章,而這十五篇文章基本上是以作者做為一名「駭客」,來檢視各種不同的主題。事實上,駭客只是個通稱,在駭客的光譜上,還是散佈著各種不同風格的駭客,有像Richard Stallman這種仙風道骨,有著崇高理想的駭客,也有像本書作者一樣,對於創業別有獨特想法的駭客-事實上,本書作者所創立的Viaweb公司,後來成功的出售給Yahoo,成為著名的Yahoo Store,為作者賺進許多財富。所以,即使本書內的文章嘗試著以駭客的想法出發去談論各種議題,但實際上也僅只是作者做為一名駭客,講出自己心中的觀點。
儘管作者的想法要代表所有的駭客有所困難(畢竟每個駭客都是努力的在做為一名獨一無二的人),但作者的許多論點,對於我們觀察一名駭客心中的世界,以及駭客是如何的看待這個世界,倒是多有幫助。
本書的書名《駭客與畫家》,也正好是眾多散文中的一篇的篇名。本書作者不僅擁有哈佛大學的電腦科學博士學位,他同時還曾經在藝術學校中學習繪畫。在這篇文章中,作者試圖說明駭客與畫家之間的相似性。這樣的結果看似違反常識,但仔細一想,卻又極為合理。他們都是創作者,都試著創作美好的事物。畫家從前人的作品中學習模仿,找出偉大畫作的共通靈魂。而駭客能從開放源碼的專案中,透過閱讀他人的程式碼,琢磨設計程式的要點與精神。繪畫與設計軟體都需要對美感的狂熱奉獻。而在文藝復興時期的許多藝術作品,都是集眾多藝術家之力而完成,而現在的許多開放源碼專案,更跨越國際,憑藉多位駭客之能而問世。
本書中有篇文章叫做《為什麼書呆子不受歡迎》。在文中,作者認為所謂的聰明人在成長過程中常常因為自己的聰明,而使得自己在同儕團體間不受歡迎,而被視為所謂的書呆子。並從自己的受教育的經驗為例,檢討為什麼在美國的校園中,會欺壓所謂的聰明人,導致大家寧可做個受歡迎的人,而不願意做個聰明人。重點是,駭客都是聰明人呀!這篇短文真的渉及了駭客的心理層面問題。駭客在正常的人際關係中都扮演什麼樣的角色?童年或青少年在人際網絡中的不得意,到了成人時期是否仍舊呢?我們所認知的駭客形象,是否就是透過網路交談時極為活躍,但面對人群時卻又木訥寡言呢?
從另一個觀點切入,作者的新創事業-Viaweb,成功的出售給Yahoo,使得他與幾位一同創業的駭客,都獲取了大量的財富。在《另一條路》、《製造財富》以及《留心鴻溝》諸篇文章中,作者不僅描述了他如何透過獨特的觀察,找出應用程式的另一條路,並且據以做為新創的事業開始投入發展,同時他也對於做為一名駭客,應該如何的快速賺取大量財富提供了他自身的經驗之談。站在作者的觀點來看,駭客是具有超凡驚人技藝的人,理當獲取超越凡人的報酬,畢竟十個凡人或一百個凡人,無論如何也比不上一個駭客。這不是量的差異,這是質的分際。而辛勤熱情工作的駭客,更應該獲得更高的報酬。從作者的角度看來,就明顯不是主張駭客當分文不取,而只應為理想努力,不應墮落在資本主義社會的金錢遊戲之下-所以我說駭客的屬性是散佈在一整個光譜上的,沒有絕對同類的駭客。
本書中有幾篇文章談論到「設計」這個主題。我認為所謂的設計是解決問題的手段,低層次的設計關心問題是否被有效解決,而高層次的設計,則關心設計是否達到足夠的美感。其中有一篇叫做《設計與品味》的文章,像這樣子的文章,把軟體的設計和品味放在一塊,基本上就有深度可以談論了。作者列出多個評估設計是否夠好的準則,例如「良好的設計是簡單的」是普遍被接受的想法,而像「好設計歷久彌新」,則掌握了「不夠美好的東西總是會被取代,留下來的東西,才是最好的」這樣子想法的精髓。在這個準則下有個有趣的論點,也就是如果我們的設計想要吸引未來的人,那麼我們的設計肯定要能夠吸引過去的人。誠哉斯言呀,美的價值恆久不變,對於未來的人要是美的,那麼對過去的人來說,也勢必是美的。這彷彿又對流行與時尚,做了更高層次的詮釋。
做為一名駭客,作者對程式語言有他自己特別的觀點。事實上,我發現駭客都有不喜歡受限的特質。所以不難理解作者對於程式語言的偏好方向。但就我來看,終歸是楊過在獨孤求敗的劍塚前所見的一句話-「四十歲後,不滯於物,草木竹石均可為劍。自此精修,漸進於無劍勝有劍之境。」究竟是利劍強、玄鐵重劍強,還是木劍強,這程式語言優劣的論戰,我想始終是難以平息。
Posted at 12:29上午 四月 17, 2006 by Chien-Hsing Wang in 書評 |
http://www.javaworld.com.tw/roller/qing/entry/2006_4_17_Hackers_and_Artists
天才之謎
文/巫石吉
【明慧學校】康德主張「天才是大自然賦予特定人士的稟賦」,其中暗示的普遍信念是:個人非凡成就的原因來自特殊天賦或天生才華。
德國【浮士德】詩劇作者歌德,不僅僅是一位偉大的詩人,他也是一位對科學有濃厚興趣的專家。歌德說:「才華養於平靜,而性格則生於激流。」
在別人懇請偉大的英國畫家泰納透露其成功祕訣時,他直截了當的回答:「我唯一獲得的祕訣就是拼命努力。」與牛頓同時代的一位人士形容牛頓是如此專注,若非操作實驗迫使他必須暫停思考,他會因用功過度而形銷骨毀。
牛頓展現出驚人的頑強,堅持面對困難;為了理解笛卡兒【幾何學】裡的數學,他唯一的辦法就是不斷努力嘗試,他「靠自己閱讀,雖然每次只能弄懂兩、三頁,他毫不氣餒繼續鑽研三、四頁直到遇挫折;然後再度開始,慢慢推進。就這樣持續不懈,直到他讓自己精通整本書。」
我們透過書籍發現到:天才往往專心致志,他們通常都會敏銳覺察到自己的方向,一心一意向前邁進,對其他事務則漠不關心。
此外,常喜歡孤獨寧靜的生活方式,以達爾文為例,他被視為隱遁的科學家,老是擔心自己身體不好,幾乎足不出戶,達四十年之久。同時,他建築高牆,擋住相鄰約一百五十餘公尺的巷道,以保護個人隱私。
然而除了他與其他天才共同具備的優異智能、勇猛決心與專心致志之外,若非他同時擁有驚人的交際手腕,與人周旋的勇氣和傑出技巧,他絕對無法享有最終的成功。
天才其實與凡人相同,就像一般人培養技藝與才能的過程,天才的才幹愈是不尋常,愈是要按部就班、循序漸進養成。這應證了歌德所說:「才華養於平靜,而性格則生於激流。」
維納十八歲獲得博士學位,曾經前往海外遊歷,包括有一段時期在羅素門下工作。一九一九年,二十五歲的維納準備好了,他要建立自己頂尖數學家的聲譽,他獲得麻省理工學院的教職,也真正成了頂尖的數學家。
主要來自於維納整個童年有很愉快的經驗,他母親經常講故事唸書給他聽,家裡有豐富的藏書,他總是看見有人使用和享受這些書籍,於是迅速領悟到閱讀的樂趣。在學習閱讀的努力上,他得到許多鼓勵。他父母親總不斷供應培養孩子想像力的書籍,讓他擁有無遠弗屆的豐富想像空間,無怪乎愛因斯坦說了一段非常漂亮的智慧箴言:「想像力比知識更重要,最重要的是-不能停止懷疑!」
可不是嗎?胡適這位國學大師也說:「作學問要在不疑處有疑。」此外他強調「大膽假設,小心求證。」似乎要成為一位超越生命、成就一方領域的曠世奇才,才氣、膽識、見識與累積的力量是不可或缺的要項。
韋特對小孩的好奇心和學習熱誠印象深刻,他談到:「如果跟孩子的每一次對話或是他在場時,都教導他一件事情,孩子可以學到的東西簡直是無可限量!」西元1800年,韋特在兒子誕生時,湧起將這名男孩栽培得「高人一等」的念頭。他和妻子著手努力,結果相當成功,最後兒子成為研究義大利詩人但丁的權威,在文壇舉足輕重。
每個思維都帶著創造的能量。天才之所以為天才,應該在於對感興趣事物的執著態度。達爾文的演化論風靡至今,他在十歲時喜歡蝴蝶和甲蟲,到了二十歲依然熱忱未減。後來經由航海旅行發現各地區不一樣型態的甲蟲和昆蟲,讓他對於物種演化的概念有不一樣的疑惑與探索。
在天才的概念裡,胡適強調的「大膽假設,小心求證。」似乎是最好的註解,在天才的探索中,思維問題萬千;在探索中,天才孤獨而不孤單。
天才與一般人不一樣的關鍵在於:你要向外發展,便有無限的外展開在你的面前。你若要向內,又有無窮的內展開在你的面前。尤其當中擋在前面的問題叢生,難題始終橫亙交雜,一般人只是灰心退縮,半途而廢;但天才知道堅持下去,用心尋覓解決難題,勉力務之必有喜悅。
可見天才的成功並非只是天資聰穎的代名詞,他們的努力以赴通常超越一般人甚多,我們不該只是看到他們成功的表相,那應該是埋頭苦幹,年復一年的勤奮努力所得的成果代價。
天才的奧祕,其實在每個人的心中,在你兩耳之間的腦內開發。日本著名教育家七田真說了句很妙的話:「每個人出生時都是天才。」為何經過教育後慢慢變得平庸呢!說穿了並未找出自己與生俱來的潛能,等到找到了,體悟出自己潛能時已經漸入中老年,心有餘而力不足矣!
天才的奧祕,奧祕在你心中,你找到了嗎?你善用它了嗎?
http://www.minghui-school.org/school/article/2006/4/29/55078.html
天才的才幹愈是不尋常,愈是要按部就班、循序漸進養成
Monday, November 29, 2010
FreeBSD ZFS 自動產生 Snapshot 工具
FreeBSD 有 ZFS 可以用,最早的時是自己寫 script 產生 snapshot,並且自動 rotate,現在則是用 sysutils/zfs-snapshot-mgmt 做到…
設定檔是 YAML 格式,裡面的數字大多都是以分鐘為單位 (不是秒,曾經搞錯,過了一個禮拜才發現…),由於有 sample config 可以看,這邊就不提了。
除此以外要注意的是,在產生 snapshot 時是以 GMT 時間為準,但命名 snapshot name 時卻是以是以當地時間為準,所以可能會看到八個小時的差異 (如果系統有設定 timezone),基本上不用太在意。如果真的在意,可以用 offset 這個參數調整。
Reference:
http://blog.gslin.org/archives/2009/05/02/2012/freebsd-zfs-%e8%87%aa%e5%8b%95%e7%94%a2%e7%94%9f-snapshot-%e5%b7%a5%e5%85%b7
finch-文字介面使用 IM msn console command line
內容
前言
實作環境
安裝及設定
步驟1.安裝 finch 套件
步驟2.設定 putty 或 pietty 使用 UTF-8
步驟3.設定使用語系
步驟4.設定 finch 來登入 MSN
步驟4-1.新增帳號
步驟4-2.設定 MSN 帳號
步驟4-3.啟用 MSN 帳號
參考
Me FAQ
前言
[Finch] 就是 [Pidgin] 的文字模式,什麼是 [Pidgin] 它是一個支援 multi-protocol Instant Messaging client,讓你很方便的可以利用它來登入不同的 IM、IRC...等,本次實作為利用 [Finch] 來登入 MSN,[Pidgin] 授權方式為 GNU General Public License (GPL) version 2.。
目前 [Pidgin] 支援的 IM 如下:
AIM
Bonjour
Gadu-Gadu
Google Talk
Groupwise
ICQ
IRC
MSN
MySpaceIM
SILC
SIMPLE
Sametime
XMPP
Yahoo!
Zephyr
以下是 [Finch] 會常用到的快速鍵,詳細內容可參考 [Using Finch - Pidgin - Trac]
Ctrl + o:選項視窗 (或 F10)
Ctrl + c:離開 finch (詢問是否離開)
Alt + n:切換視窗 (多視窗時使用)
Alt + 1 ~ 9:多視窗切換 (快速切換)
Alt + c:關閉目前視窗 (例如關掉對話窗)
Alt + a:選項視窗 (ex.帳號清單)
Alt + l:更新畫面
Alt + q:直接離開 finch
實作環境
CentOS 5.1 (Linux 2.6.18-53.1.4.el5)
finch-2.4.0-0.el5 - Finch multi-protocol messaging client (Console UI)
screen-4.0.3-1.el5 - A multi-screen window manager
安裝及設定
步驟1.安裝 finch 套件
利用 yum 指令來安裝 finch 套件
#yum -y install finch步驟2.設定 putty 或 pietty 使用 UTF-8
設定 [putty] 或 [pietty] 使用 UTF-8 並且記得取消勾選 CJK
步驟3.設定使用語系
登入後,您可選擇使用 en_US.UTF-8 或 zh_TW.UTF-8,以我使用 tcsh shell 為例修改 ~/.cshrc
setenv LANG en_US.UTF-8 //使用 en_US.UTF-8
setenv LC_ALL en_US.UTF-8
setenv LANG zh_TW.UTF-8 //使用 zh_TW.UTF-8
setenv LC_ALL zh_TW.UTF-8修改完後存檔,使用 source 指令來載入語系 (以載入 zh_TW.UTF-8 為例)
#source ~/.cshrc //載入 shell 設定
#locale //查看語系設定
LANG=zh_TW.UTF-8
LC_CTYPE="zh_TW.UTF-8"
LC_COLLATE="zh_TW.UTF-8"
LC_TIME="zh_TW.UTF-8"
LC_NUMERIC="zh_TW.UTF-8"
LC_MONETARY="zh_TW.UTF-8"
LC_MESSAGES="zh_TW.UTF-8"
LC_ALL=zh_TW.UTF-8確定語系後使用 screen 指令來進入 screen 模式,進入 screen 後記得在查看語系確定一下。
步驟4.設定 finch 來登入 MSN
步驟4-1.新增帳號
利用 Tab 鍵切換到 Add 後按下 Enter 準備新增 MSN 帳號。(左圖語系設定為 zh_TW.UTF-8 右圖語系設定為 en_US.UTF-8)
步驟4-2.設定 MSN 帳號
利用上下鍵來切換通訊協定並選擇 MSN、填入 MSN 的帳號及密碼、儲存。(左圖語系設定為 zh_TW.UTF-8 右圖語系設定為 en_US.UTF-8)
步驟4-3.啟用 MSN 帳號
切換到剛才建立的 MSN 帳號按下空白鍵來啟用 MSN (登入)。(左圖語系設定為 zh_TW.UTF-8 右圖語系設定為 en_US.UTF-8)
Reference: http://www.weithenn.org/cgi-bin/wiki.pl?finch-%E6%96%87%E5%AD%97%E4%BB%8B%E9%9D%A2%E4%BD%BF%E7%94%A8_IM
vim links
http://blog.ngedit.com/2007/05/17/why-oh-why-do-those-nutheads-use-vi/
http://edyfox.codecarver.org/html/vimplugins.html
http://vimdoc.sourceforge.net/htmldoc/windows.html
http://vim.wikia.com/wiki/Vim_buffer_FAQ
winmanager
http://vim.wikia.com/wiki/VimTip686
Sunday, November 28, 2010
How to use Ctrl-Tab key combination in GNU Screen or Vim
GNU Screen allows you to open several sub-windows within one terminal window. By default, you switch between them using Ctrl-A
followed by n
or p
. I think this is a bit clumsy, I would like to switch with Ctrl-Tab
and Ctrl-Shift-Tab
just like you switch tabs in Firefox and many other applications. The sub-windows in Screen is conceptually just like tabs in Firefox, so it’s logical to use the same keys to switch between them.
Screen can be configured to use any key combination for switching sub-window by using the bindkey
command. However, Screen can only recognize the key combinations that your terminal emulator actually intercept and send a unique code for. By default, most terminal emulators do not intercept Ctrl-Tab
, they just send the same code as for Tab
. And you certainly not want to use that since Tab
is used for tab completion in the shell.
So you need to configure your terminal emulator to intercept and send a unique code for Ctrl-Tab
. In xterm, you can do that by setting the X resource XTerm.vt100.modifyOtherKeys: 2
. Now xterm sends ^[[27;5;9~
for Ctrl-Tab
and ^[[27;6;9~
for Ctrl-Shift-Tab
(^[
is ESC). However, you don’t want to use this since it mess up other things. You need to configure just Ctrl-Tab
and Ctrl-Shift-Tab
without altering any other keys. This can be done using the translation feature. Add this to your ~/.Xresources
file (you need to log out for this to take effect):
*vt100.translations: #override \n\ Ctrl ~Shift <Key>Tab: string(0x1b) string("[27;5;9~") \n \ Ctrl Shift <Key>Tab: string(0x1b) string("[27;6;9~") \n
Then add this to your ~/.screenrc
file:
# Ctrl-Tab bindkey "^[[27;5;9~" next # Ctrl-Shift-Tab bindkey "^[[27;6;9~" prev
This works in xterm. I’m not sure if it works in other terminal emulators.
Reference:
http://www.staldal.nu/tech/2009/01/10/how-to-use-ctrl-tab-in-gnu-screen/
Mapping fast keycodes in terminal Vim
In the GUI version of Vim (gvim), the mapping of keys seem to work for the most part. For instance, it is easy to map Ctrl-Shift-F2 to a keystroke:
"delete all lines in the current buffer :nmap <C-S-F2> ggdG
For terminal versions of Vim (such as xterm, rxvt, win32, etc), mapping something like Ctrl-Shift-F2 needs some extra work. It may seem daunting to deal with archaic terminal keycodes at first, but once you understand what is going on in the right context, it is quite simple.
Contents |
1a. Keycodes Edit
There are two types of keycodes: terminal keycodes and Vim keycodes. Terminal keycodes look something like
^[[1;2A
These are actual bytes that are sent to Vim by the terminal when we type Shift-Up, for example (on an xterm).
Vim keycodes look like
<S-Up>
Vim needs its own representation of keycodes because it runs on a variety of platforms. Vim can act upon its own keycodes, and leave the assignment of terminal keycodes to autodetection (or to manual setup by the user, which is what this guide is for).
1b. List of usable keycodes Edit
To obtain a list of terminal keycodes, look it up in the manual for that terminal.
For a list of Vim's internal keycodes, see :help t_ku.
In addition to the Vim keycodes listed in the help section above, the keycodes for the function keys actually go up to F37. This means, we have
<F1> to <F37> <S-F1> to <S-F37>
We can use this to our advantage to create more "responsive" mappings. More on this later in section 2b.
We can also set these Vim keycodes:
<C-Home>, <C-End> <S-Home>, <S-End> <S-a> to <S-z> <C-a> to <C-z> <A-a> to <A-z>
Be careful when setting these keycodes. See section 3a.
1c. Setting Vim keycodes Edit
Vim keycodes look a lot like the keystrokes we would define in a mapping. They are easy to read, quite intuitive, and are almost treated the same by Vim. The only difference is that we can :set internal keycodes. For example, to manually set the keycode for Shift-Down_arrow:
:set <S-Down>=^[[1;2B
where ^[ is a literal ESC special character.
The following is not allowed because the keycode is not a listed Vim keycode:
:set <C-S-Down>=^[[1;6B
Once the correct terminal keycode is assigned to the appropriate Vim keycode, the keystroke should work in terminal Vim. Shift-Down_arrow should now work, but the Ctrl-Shift-Down_arrow keycode can not be set in the same way. For this we need mappings (see section 2b).
1d. Entering literal characters & terminal codes Edit
Either in the :command line, or in insert mode:
To enter a literal character in Vim, first type Ctrl-v, followed by a single keystroke. Hence, typing Ctrl-v then Esc will produce "^[".
To enter a terminal keycode in Vim, first type Ctrl-v, followed by the keystroke we want to obtain the term keycode from. Hence, typing Ctrl-v + Shift-Down_arrow will produce "^[[1;2B".
This means that there are 2 ways to set the Vim keycode as shown in section 1c:
"literally enter the keycode :set <S-Down>={C-v}{Esc}[1;2B
or
"let the terminal send its keycode to Vim :set <S-Down>={C-v}{S-Down}
where curly brackets {..} denote the action of typing the keystroke. For instance, when you see {C-v}, hold down Ctrl and hit "v". Do not type it literally as "{C-v}".
We shall use the curly brackets as nomenclature from now on.
Also make use of the Normal mode command 'ga' to inspect the resulting keycode. Place the cursor over a special character, and type 'ga'. This will provide you with ascii information of the character under the cursor.
See :help i_ctrl-v, :help ga.
2a. Mappings Edit
The function of mappings in Vim is to map a set of keystrokes to another set of keystrokes. For more information, see :help key-mapping, :help map-modes.
2b. Mapping "fast" keycodes Edit
As mentioned in section 1c, the Ctrl-Shift-Down_arrow keycode cannot be set in the same way as the Shift-Down_arrow keycode because it is not a listed Vim keycode. One way to get around that is to do the following:
:map <Esc>[1;6B <C-S-Down>
This maps the literal terminal keycode to a Vim keystroke (remember, not a Vim keycode).
The disadvantage of this is that Vim will "wait" everytime after the Esc key pressed for a potential "[" + "1" + ";" + "6" + "B" keystroke combination, just in case you meant to do <C-S-Down> instead of a simple <Esc> to Normal mode.
If you typed "{Esc}[1;6B" fast enough, you would do a Ctrl-Shift-Down_arrow. After all, you are mapping a keystroke, not a keycode.
A better way of mapping keycodes is to first assign the terminal keycode to an unused Vim keycode, and then map the newly used Vim keycode. This way, we can set 'ttimeoutlen' to a small value to ensure that the terminal keycode can only be entered into Vim as fast as the terminal can, but will be humanly-impossible to do it manually.
This is how it is done:
:set timeout timeoutlen=1000 ttimeoutlen=100 :set <F13>=^[[1;6B :map <F13> <C-S-Down> :map! <F13> <C-S-Down>
Now, Ctrl-Shift-Down_arrow will work, and Esc will not pause.
Potentially unused Vim keycodes that can be used include:
<F13> to <F37> <S-F13> to <S-F37> <xF1> to <xF4> <S-xF1> to <S-xF4>
When setting any Vim keycode, exercise caution. Do some checks to see if the terminal code you're setting the Vim keycode to is set elsewhere. There must be no conflicts.
For instance, in a Win32 terminal, <F1> to <F4> will always be set the same as <xF1> to <xF4>, respectively. Therefore, you cannot use those keycodes.
See :help 'timeout', :help 'timeoutlen', :help 'ttimeoutlen'.
2c. Laying it out in vimrc Edit
Here is an example of how to set up the keycodes in a vimrc file. It is highly recommended that you start Vim without any startup scripts that may interfere with keycode detection:
$ vim -Nu NONE .vimrc
set timeout timeoutlen=1000 ttimeoutlen=100 if !has("gui_running") if &term == "xterm" set <Home>=^[[H <End>=^[[F <BS>=^? set <S-Up>=^[[1;2A <S-Down>=^[[1;2B <S-Right>=^[[1;2C <S-Left>=^[[1;2D set <xF1>={C-v}{C-S-Up}^[[1;6A <xF2>=^[[1;6B <xF3>=^[[1;6C <xF4>=^[[1;6D map <xF1> <C-S-Up> map <xF2> <C-S-Down> map <xF3> <C-S-Right> map <xF4> <C-S-Left> map! <xF1> <C-S-Up> map! <xF2> <C-S-Down> map! <xF3> <C-S-Right> map! <xF4> <C-S-Left> elseif &term == "win32" ... else ... endif endif
where ^[ is a literal ESC special character and ^? is a literal ascii 0x7f character.
See :help 'term', :help has-patch.
You can also use a function like the following to automatically assign fast keycodes:
" MapFastKeycode: helper for fast keycode mappings " makes use of unused vim keycodes <[S-]F15> to <[S-]F37> function! <SID>MapFastKeycode(key, keycode) if s:fast_i == 46 echohl WarningMsg echomsg "Unable to map ".a:key.": out of spare keycodes" echohl None return endif let vkeycode = '<'.(s:fast_i/23==0 ? '' : 'S-').'F'.(15+s:fast_i%23).'>' exec 'set '.vkeycode.'='.a:keycode exec 'map '.vkeycode.' '.a:key let s:fast_i += 1 endfunction let s:fast_i = 0
It uses Vim keycodes <F15> and above. If you don't have keys for <F13> and <F14> on your keyboard, you can also make use of those, for a total of 50 keys you can map this way.
Once defined, you can use the function to map keys like so: (example for rxvt)
call <SID>MapFastKeycode('<C-F3>', "\e[13^") call <SID>MapFastKeycode('<C-F4>', "\e[14^") call <SID>MapFastKeycode('<M-j>', "\ej") call <SID>MapFastKeycode('<M-k>', "\ek") " ...
3a. Troubleshooting I Edit
You only need to employ unused Vim keycodes either when
(a) there are no available Vim keycodes that match the terminal keycode, and
(b) the terminal keycode is longer than a single keystroke
or
(c) setting the Vim keycode that matches the terminal keycode causes weird behavior.
Example where (a) is not true:
:set <F13>=^[[1;2B :map <F13> <S-Down>
Setting the unused <F13> is redundant, because <S-Down> is already available as a Vim keycode. Do this instead:
:set <S-Down>=^[[1;2B
Example where (b) is not true:
For a Win32 terminal, mapping Ctrl-Backspace is done by
:map <C-{C-v}{BS}> <C-BS>
Since the terminal keycode can be represented by <C-^?>, which is a single keystroke, there is no need to employ an unused Vim keycode.
Example where (c) is true:
For an rxvt terminal, merely doing
:set <A-j>=^[j <A-u>=^[u <A-6>=^[6
will not make Alt-j, Alt-u and Alt-6 work. Reason unknown. Need to assign to unused Vim keycodes.
3b. Troubleshooting II Edit
Sometimes, performing
:set <xF1>={C-v}{A-S-F1}
produces
:set <xF1>=^[<S-F1>
where ^[ is a literal ESC special character and "<S-F1>" is literal text.
This means that you may have already set Vim's keycode <S-F1>, and that it was part of the Alt-Shift-F1 terminal keycode that was sent to Vim. Vim processes the entire terminal keycode and noticed that it could replace part of it with its own internal representation.
To fix this and enter the full terminal keycode, backspace up to "<" and do {C-v}{S-F1}. The full action would be:
:set <xF1>={C-v}{A-S-F1}{BS 6 times}{C-v}{S-F1}
to produce
:set <xF1>=^[^[[23~
To give some perspective, here are the actual terminal keycodes for rxvt:
<S-F1> = ^[[23~ <A-S-F1> = ^[^[[23~
Reference:
http://vim.wikia.com/wiki/Mapping_fast_keycodes_in_terminal_Vim
Alternative tab navigation
Here is an alternative way to open and navigate tabs in Vim 7. Add the following to your vimrc:
" tab navigation like firefox :nmap <C-S-tab> :tabprevious<CR> :nmap <C-tab> :tabnext<CR> :map <C-S-tab> :tabprevious<CR> :map <C-tab> :tabnext<CR> :imap <C-S-tab> <Esc>:tabprevious<CR>i :imap <C-tab> <Esc>:tabnext<CR>i :nmap <C-t> :tabnew<CR> :imap <C-t> <Esc>:tabnew<CR>
You can open a new tab with ctrl-t, go forward through the tabs with ctrl-tab and backwards with ctrl-shift-tab.
This way of navigating resembles the way it is done in Firefox.
Comments Edit
Mine:
map th :tabfirst<CR> map tj :tabnext<CR> map tk :tabprev<CR> map tl :tablast<CR> map tt :tabedit<Space> map tn :tabnext<Space> map tm :tabm<Space>
They are quite like the mappings below, by leading the combinations with the easily-reached/rarely-used "t" key. I use "hjkl" to navigate as they are "left/down/up/right" for vi users. "jk" (down/up) match the directions when using a mouse to scroll through tabs, and "hl" (left/right) stand for leftmost/rightmost tabs.
I ended up with:
map th :tabnext<CR> map tl :tabprev<CR> map tn :tabnew<CR> map td :tabclose<CR>
For something like gnome-terminal tab-related key shortcuts:
:nmap <C-S-t> :tabnew<CR> :imap <C-S-t> <Esc>:tabnew<CR> :imap <C-S-w> <Esc>:tabclose<CR>
Didn't exactly work in Windows' gvim. Had to put it into _gvimrc, like this:
:map <C-S-tab> :tabprevious<CR> :map <C-tab> :tabnext<CR> :map <C-t> :tabnew<CR>
Why not add Ctrl-w to close tabs?
:map <C-w> :tabclose<CR>
A: because <Ctrl-w> is the start of the window commands - like <C-w>n for new window...
I just use the way that Vim provides and it work well:
- gt for tabnext
- gT for tabprevious
Try
:map <S-h> gT :map <S-l> gt
That way you can hold down the shift key while you scroll left and right through the tabs with 'h' and 'l'.
<C-tab> , <C-S-tab> don't work on Eterm, aterm, xterm
We can use <C-PageDown> and <C-PageUp> instead of <C-tab> and <C-S-tab>
This's the default way in Vim 7. By the way, this also work on Firefox.
This tip is really good. But the key sequences used for switching/creating the tabs is clashing with other features of Vim. For example:
- CTRL+T is used for jumping to previous tags [exuberant ctags].
- CTRL+W is used for jumping to next split window in multiple windows
- CTRL+TAB is captured by KDE itself to switch workspaces.
Can you give a nice key combinations which will work with all these features ?. I know tabs is a new feature of vim7. But this script can become awesome if worked properly on.
Vim 7 already has <C-PageUp> and <C-PageDown> to cycle through tabs. To open and close them I added
nmap <C-Insert> :tabnew<CR> nmap <C-Delete> :tabclose<CR>
(not for insert or visual mode because they do something else then).
I know that Z and X both perform functions in normal mode, but I never use them, so I just mapped:
:nmap Z :tabprev<CR> :nmap X :tabnext<CR>
That way shift-z and shift-x takes me forward and backward. It's a lazy, ergonimic one hand solution.
I found this tip
autocmd VimEnter * tab all autocmd BufAdd * exe 'tablast | tabe "' . expand( "<afile") .'"'
I neglected to RTFM. The key combination is <tab number>gt. Here's a handy alternative to switch to tab-N by pressing Alt-FN
map <A-F1> 1gt map <A-F2> 2gt map <A-F3> 3gt map <A-F4> 4gt map <A-F5> 5gt map <A-F6> 6gt map <A-F7> 7gt map <A-F8> 8gt map <A-F9> 9gt map <A-F0> 10g
I found <C-tab> wasn't picked up on the Solaris machine I was working on, so I used function keys.
:map <F9> :tabnew <CR> :map <F10> :tabclose <CR> :map <F11> :tabprevious <CR> :map <F12> :tabnext <CR> :nmap <F9> :tabnew <CR> :nmap <F10> :tabclose <CR> :nmap <F11> :tabprevious <CR> :nmap <F12> :tabnext <CR> :imap <F9> :tabnew <CR> :imap <F10> :tabclose <CR> :imap <F11> :tabprevious <CR> :imap <F12> :tabnext <CR> " F10 = VMS for exit ;-)
Reference:
http://vim.wikia.com/wiki/Alternative_tab_navigation
Fix arrow keys that display A B C D on remote shell
I wonder how many of you are annoyed with the arrow key behavior during insert mode in Vim.
Leave all the key mappings default, do not change your TERM environment. Here's a simple tip,
:set term=cons25
It was tested with Vim 5.8 and 6.1 editions on; FreeBSD 4.X-STABLE; xterm(-color),VT100 remote terminals; (t)csh shells.
Contents |
Comments Edit
additional solution 1 Edit
try this mapping, worked on gvim v7.2
imap ^[OA <ESC>ki imap ^[OB <ESC>ji imap ^[OC <ESC>li imap ^[OD <ESC>hi
Note: ^[ is one symbol, try to enter it using <Ctrl-V>ESC or <Ctrl-V><Ctrl-[>
additional solution 2 Edit
This doesn't really work very well for me on SunOS -- I have the same problem but :set term=cons25 just gives me OC everywhere before each character until I redraw. Weird.
But, :set term=ansi works great, and the arrow keys work.
A better solution is to, while in vi, type in insert mode [ctrl-v][arrow up] and see what you get.
Then, in your .vimrc, add the lines
set t_ku=[ctrl-v][esc]OA # or whatever you saw
so it ends up looking something like
set t_ku=^[OA set t_kd=^[OB set t_kr=^[OC set t_kl=^[OD
(though you want real escape chars instead of "^[".)
And in addition, set term=ansi makes syntax color off in vim (afair FreeBSD with color xterm).
additional solution 3 Edit
So are we to be left with no good freebsd / bash solution?
set t_ku=^[OA set t_kd=^[OB set t_kr=^[OC set t_kl=^[OD
did nothing for me... and the no environment option is not a good one.
set mouse=a at least allows me to move in insert mode.
additional solution 4 Edit
I had the same problem a few days ago. I figured out that i didnt have the ~.vimrc file. So, i went to /usr/local/share/vim/vim58 and copied the vimrc_example.vim to ~.vimrc. Problem solved!
I can confirm that the arrow-key problem (under freebsd) disappeared as soon as I introduced .vimrc
additional solution 5 Edit
After experiencing this issue on FreeBSD 6 and 7 I found the clue as to why arrow keys would display A B C D when in insert mode - buried within :help nocompatible is this nugget:
- By default this option is on and the Vi defaults are used for the options. This default was chosen for those people who want to use Vim just like Vi, and don't even (want to) know about the 'compatible' option.
- When a vimrc or gvimrc file is found while Vim is starting up, this option is switched off, and all options that have not been modified will be set to the Vim defaults. Effectively, this means that when a vimrc or gvimrc file exists, Vim will use the Vim defaults, otherwise it will use the Vi defaults.
So rather than mucking around with termcap settings create an empty .vimrc file first and see if the issue is resolved. Or, copy the default vimrc_example.vim to ~/.vimrc If not, try the termcap suggestions.
Do this:
$ vim ~/.vimrc set t_ku= (now type Ctrl-V and press cursor up) set t_kd= (now type Ctrl-V and press cursor down) set t_kr= (now type Ctrl-V and press cursor right) set t_kr= (now type Ctrl-V and press cursor left)
You should get something that looks like
set t_ku=^[OA set t_kd=^[OB set t_kr=^[OC set t_kl=^[OD
but the ^[ is actually a single escape character.
That certainly works for me here (FreeBSD 6.1, ssh in from Mac OS).
It also appears that it must be started as "vim" rather than "vi" in order to read the .vimrc file. I've added an alias to my bashrc scripts to take care of this... "alias vi=vim"
If you are using vi instead of vim, you need to update the above commands in .exrc file instead of .vimrc. Since alias makes you to work in vim, even you type vi.
"So, i went to /usr/local/share/vim/vim58 and copied the vimrc_example.vim to ~.vimrc. Problem solved!"
Slightly different path for me, but confirmed it works on sunOS 10, also fixed my backspace issue, setup the colors, and search highlights.
additional solution 6 Edit
I have a similar problem but slightly different. After a very recent update of my system including vim71 the left and right arrow keys started to play up. They both moved left and right respectively, but only by word increments!
Setting ":set term=builtin_ansi" fixed the problem, so I knew it was a terminal problem.
Looking further ":set t_kl" reporting the weird string "^[O*D" Setting this to "^[[D" or "^[OD" has no effect on the arrow keys actions, it remains jumping by word.
Only setting "term=ansi" seems to help. But destroys use of function keys, and the alternate editing screen (preserving normal command line output display, when not editing). So I am stuck with 'word jumping arrow keys'.
- I had the same problem, but solved it in a better way by "se term=linux" in my .vimrc. That way, I don't lose my syntax highlighting, function keys, etc, and still managed to stop the "word jumping" arrow keys.
Additional Solution 7 Edit
I had the same problem using Mandriva 2010. The solution was to install vim-common from the urpmi repository.
Additional Solution 8 Edit
I had the arrow keys switching from normal mode to insert mode when I was using vim in the terminal: E.g. pressing "up" would do the same as <Esc>OA, where <Esc> had no effect, O would switch to insert mode, and A would enter the letter "A". The reason was that I in my .vimrc I had mapped <Esc> in normal mode to do funky stuff. Removing the mapping fixed it.
Additional Solution 9 Edit
On my default Ubuntu install, vi meant actual vi, but I was used to vim where this problem didn't occur.
My solution was apt-get install vim.
Additional Solution 10 Edit
On my Gentoo and Debian installs, this problem was caused by the autoclose plugin and using the workarounds above didn't help.
The only solution for me was to get rid of the autoclose plugin.
Same problem here, the issue appeared on my machine after installing "Limp for Vim", Lisp IDE, for all open Lisp files.
Disabling autoclose plugin in Limp got me the arrow keys back.
Thanks!
It does seem like the version of autoclose distributed with Limp is causing this. The latest version doesn't have this problem, although Backspace still isn't working for me.
You can fix the arrow key problem by downloading the newest version of autoclose. You then need to modify mode.vim in Limp. Autoclose no longer has the functions AutoClose_start and AutoClose_stop, and mode.vim calls these functions. Edit mode.vim to change
call AutoClose_start()
to
AutoCloseOn
and
call AutoClose_stop()
to
AutoCloseOff
Hope this helps.
Nick Coleman
Additional Solution 11 Edit
I had this problem with a client's server config. Because I didn't have much power or permission to change things, and time was tight, I simply did the following and resolved the problem.
:set term=ansi
If you ever have that kind of situation, give that a shot.
Reference:
http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell
vim weird code map ctrl ctrl-v key does not work
Use ":set termcap" to check which are supported and
what the codes are.
:set termcap
Reference: http://vimdoc.sourceforge.net/htmldoc/term.html
save and restore the buffer list (last edited opened files) across Vim sessions
Yes. To save and restore the buffer list across Vim session, include the '%' flag in the 'viminfo' option. Note that if Vim is invoked with a filename argument, then the buffer list will not be restored from the last session. To use buffer lists across sessions, invoke Vim without passing filename arguments.
# vim ~/.vimrc
set viminfo+=%
Some scripts for buffer switching are listed below, in order by the script id. You can search Vim Scripts for "buffer" to find others.
[] bufexplorer A popular buffer explorer.
[] SelectBuf A fast and extensible buffer explorer.
[] selbuff Another buffer select script
[] PopupBuffer To select a buffer from a PopUp menu.
[] minibufexpl A popular buffer explorer which takes very little screen space.
[] FavMenu
[] mru
[] incbufswitch Switch buffers using an incremental search.
[] bufmenu2 More balanced buffers menu hierarchy.
[] buflist Buffer browser.
[] TinyBufferExplorer A 1-file buffer list plugin with grouping.
[] bufferlist Simple, fast, and slick non-disturbing buffer list.
[] TabBar
[] bufmap Automatically maps buffers to function keys.
[] buftabs Minimalistic buffer tabs saving screen space.
[] bufpos Switch buffer with Alt-
[] tselectbuffer A quick buffer selector/switcher.
[] LustyExplorer Dynamic filesystem and buffer explorer.
[] QuickBuf Very small but quick and powerful buffer manager.
[] fuzzyfinder Explore buffer/file/MRU/favorite with a fuzzy pattern.
[] LustyJuggler Switch very quickly between active buffers.
[] QuickName Conveniently navigate buffers; incremental search by name.
[] bufmru Quickly switch to most recently used buffer.
[] vimuiex Commands: VxBufListSelect/VxOpenRecentFile/VxFileBrowser, with filtering and MRU
[] QuickNameBuf Quickly navigate between buffers using an incremental search by name (a combination of QuickName and QuickBuf)
Reference:
http://vim.wikia.com/wiki/Vim_buffer_FAQ
http://vim.wikia.com/wiki/VimTip686
Vim tips: Folding fun
Vim's folding commands begin with z
-- which, as the Vim docs point out, sort of looks like a folded piece of paper. OK, maybe not much, but at least the commands are consistent. If you want to do something with folding, it probably starts with z
. (Since Vim 7 added spellchecking, some of the spellcheck commands also start with z
, but that's a topic for another article.)
Creating folds
Let's start with some simple folding actions. Pick a text file, preferably a longer one like your xorg.conf, or just about any text file that's too big to fit on the screen by itself. Open it in Vim, and place the cursor at the beginning of a paragraph. Make sure you're in normal mode, and type zf2j
. After you press j
, Vim will create a fold covering three lines -- the line you started the fold on, and the next two lines. If you're familiar with Vim's movement commands, you know that the command 2j
would usually tell Vim to move down two lines. Vim uses the same movement commands to define folds.
Folding also works in visual mode. If you enter visual mode using v
or V
, then select a few lines of text using the movement keys, and type zf
, Vim will create a fold comprising those lines.
Another option is to specify a range in command mode. For instance, if you wanted to create a fold from lines 20 through 101, you could just use :20,101 fold
and press Enter. In fact, you don't even need to type the entire word -- you can use fo
instead of fold
if you want to save a couple of keystrokes. Note that this will work backwards as well -- so if you use :.,20 fo
when the cursor is on line 101, Vim will reverse the range and create the fold from lines 20 to 101.
Vim can also make some smart text selections. Let's say you're working on a script or program and you have a block of text within braces ({}
). Position the cursor on the first brace, and type zfa}
. Vim will create a fold from the line with the first brace through the last; it's not necessary to know which lines you're working with, Vim will just look for the closing brace. This also works with other block enclosures -- parenthesis, brackets ([]
), and angle brackets (<>
).
As an added bonus, Vim also recognizes nested blocks, so it will create the fold based on matching delimiters, and not the first closing bracket it comes across. Vim will also work backwards. If you're on a closing brace, type zfa{
and Vim will create a fold from the current spot to the beginning brace.
Folds are nestable as well -- that is to say, you can create a fold, and then include that within a larger section of folded text.
Finally, you can create a fold from the cursor position to a search result. If you run zf/string
, Vim will create a fold from the cursor position to the first instance of the search string. Again, this is useful when you know you want a fold to go from the line you're on to a line further down in the file, but you're not sure what the line number is.
Once you've created a fold you'll see a single line of text that shows how many lines are folded, as well as the initial text, like this:
+-- 15 lines: set_up_socket_dir () {--------------------------------------------
The dashes indicate the foldlevel of the particular fold. For a top-level fold, you should see two dashes. Nested folds will have more dashes. We'll cover the foldlevel in more depth in just a bit.
Vim folding commands |
---|
|
Using folds
Creating folds is only half the fun. At some point, you'll no doubt want to unfold the text. One way to do this is to place the cursor on the same line as the fold and type zo
. This will "open" the fold, but not delete it. So if you want to do a quick check on some text that's been folded, use zo
, and then zc
to close it back up when you're finished.
To move between folds quickly, use the zj
and zk
commands to move to the next and previous fold. Note that it doesn't matter whether the folds are open or closed; if they exist, zj
and zk
will move backwards and forwards between them. These commands should be easy to remember, since they combine the fold command (z
) with movement commands (j
to move down a line, and k
to move up a line). If you want to move between the start and end of a fold, use [z
and ]z
, respectively.
As we've already covered, Vim will create nested folds. If you use zo
on a nested fold, it will open only the first fold -- the remaining folds will stay closed. However, if you'd like to open all of the nested folds on that line, use zO
-- Vim will open all of the folds. Running zc
in normal mode will close all the folds after using zO
.
The zr
command will open all first-level folds in Vim, and zR
will open all of the folds in the file. The zm
command will re-fold the first-level folds, and zM
will close all open folds in a file. If you have multiple foldlevels, a fold within a fold within a fold, running zm
will refold them sequentially -- so if all folds are open it will refold the deepest folds, then folds on the next foldlevel, and so on. The zr
command works the same way, only in reverse -- it will open the top-level fold, then the next level, and so forth.
When you decide you want to get rid of a fold entirely, position the cursor on the fold and run zd
. This will work whether the fold is open or closed. To zap all of the folds at once, type zE
. Delete with caution, however. Vim's undo and redo features don't apply to folds; if you delete a fold, you can't press u
and bring it back, and the .
command won't run the last fold command.
You might be wondering, how does Vim treat text within folds when you are searching for text, or doing a search and replace on a file? Vim treats the text normally. If you search for a string of text that's within a fold, Vim will unfold the text when the cursor moves to the string within the fold. Unfortunately, Vim is a little less than tidy in this regard -- if a search causes a fold to be expanded, Vim doesn't re-fold the text after moving the cursor outside the fold. However, a quick zm
will restore the folds.
Vim also treats a folded section of text as a single line for the purpose of deleting and yanking text. If you place the cursor on a section of folded text and use dd
, it will delete the entire section, not just the first line. Vim will also allow you to yank the entire folded section of text just by placing the cursor on the folded text and typing Y
or yy
. The folded section can then be pasted into another section of the text using the p
or P
commands.
The foldmethod option
Vim's foldmethod
option might be of interest to programmers. The default foldmethod
is manual -- that is, creating folds manually. However, Vim can also create folds based on the way a file is indented, its syntax, or markers in the text.
To set the foldmethod according to the way a file is indented, run :set foldmethod=indent
. This will automatically create folds at every indent. Consider a Cascading Style Sheet (CSS) file like this one:
body { background-color: #CCCCCC; font-family: arial, helvetica, sans-serif; } strong.header { font-family: arial, helvetica, sans-serif; color: #1D3C47; }
Once you've set foldmethod=indent
, it will look like this:
body { +--- 2 lines: background-color: #CCCCCC;--------------------------------------- } strong.header { +--- 2 lines: font-family: arial, helvetica, sans-serif;----------------------- }
Vim also offers a marker method, where folds are set by visible text markers rather than invisible manual marks. To set this mode, run :set foldmethod=marker
. Then, when you set a fold, you'll see a marker with three braces at the beginning and end of the fold, like this:
/*{{{*/ Folded text goes here... /*}}}*/
The braces are set off by comment characters, depending on the type of file. So, if you're working with an HTML file, you'll see <!--{{{-->
, and if you're editing a Python script, you'll see #{{{
. You can also insert fold markers manually if you want, and Vim will recognize them as well. Vim will also delete the markers with the zd
command, whether they were set manually or not.
Fold created with the marker method are subject to undo and redo operations, unlike normal manual folds.
It's possible to set the foldmarker to something other than {{{
if you need to, but the Vim docs recommend that you stick with the defaults. To change the foldmarker, use :set foldmarker=nnn,mmm
where nnn
is the character string to start the marker, and mmm
is the string to end the marker.
If you want to use a foldmethod other than "manual" all the time, add this line to your ~/.vimrc:
set foldmethod=foldoption
Vim has a few other foldmethods that may come in useful. See the foldmethod documentation in the Vim reference materials for more on the foldmethod option.
Saving folds
After you've gone to all the trouble of creating several folds in a longer document, it'd be a shame to lose them when you close the file. If you'd like to save the folds before you leave the file, run :mkview
. When you reload the file, run :loadview
, and Vim will restore the folds to the state the file was in at the time you ran :mkview
.
The :mkview
command supports multiple revisions of a file's state, so you can give a view a number and reload later on. For example, if you run :mkview 2
, make a few changes, and then run :loadview 2
, it will revert to the previous state of the file as far as folds are concerned. Note that it won't restore the file itself to the previous state; if you delete 10 lines of text after saving a view, Vim won't restore those lines when you load an older view using :loadview
.
But why do something manually when you can do it automatically? To see to it that Vim saves and restores folds when a file is closed and re-opened, add these two lines to your ~/.vimrc:
au BufWinLeave * mkview au BufWinEnter * silent loadview
Now, each time you close a file, its fold state will be saved and reloaded when you reopen the file in Vim.
When you add folds to other features like viewports and marks and jumps, Vim becomes an insanely productive tool for text editing and programming.
Reference: http://www.linux.com/archive/feature/114138
tmux, a BSD alternative to GNU Screen
I started using tmux today. Its a terminal multiplexer / task switcher for UNIX-likes, very much in the same vein as GNU Screen. However, its a from-scratch implementation, designed to be clean, sane and easy to configure. The more liberal 3-clause BSD license is a plus also, since it means that OpenBSD has been able to integrate it into the source tree, so that its available out of the box.
Comparison with GNU Screen
I’ve been a heavy screen user for many years – almost all my work is done on remote screen sessions. However, screen configuration has always been essentially black magic to me. For this reason, tmux and its nice manual page is a breath of fresh air. `tmux list-commands’ is very straight forward and easy to grok. Furthermore, I like that everything in tmux is scriptable from the command line – you can run commands like `tmux resize-pane-up -t comms’ to resize the pane on a session called ‘comms’.
The other thing I really like about tmux is its default status bar. Some people might hate this, but I find it very useful to have a clock and a list of windows along with the process executing in them. This took quite some work to set up to my liking in GNU screen, but the default in tmux is great.
My config
One thing I don’t much like is the default of C-b as the ‘prefix’ command. I suppose this makes some sense, since the author doesn’t want to clobber GNU screen key bindings. Perhaps he will consider changing it to C-a, like in GNU screen, in the future. In any case, this isn’t hard to change. Also, I am constantly using C-a C-a to switch back to the previous window – the default for this action in tmux is C-b l. Much less friendly in my opinion – of course, its also easy to change!
So here are the contents of my $HOME/.tmux.conf:
set -g prefix C-a bind-key C-a last-window
Getting tmux
I’m sure that packages exist for most operating systems. You can grab the source from http://tmux.sourceforge.net/. On OpenBSD, you can simply run `pkg_add -i tmux’ to get the binary on your system.
UPDATE
Since OpenBSD 4.6, tmux is part of the base system. This means that if you are running OpenBSD 4.6 or later, you don’t need to install any packages in order to get tmux.
Reference: http://niallohiggins.com/2009/06/04/tmux-a-bsd-alternative-to-gnu-screen/
Thursday, November 25, 2010
用 screen + irssi 上 irc 之鄉民版教學 (含Q&A)
最近身旁許多在上 irc 的朋友都問我哪個 irc client 比較好用
其實,只要問過我的,而且又是 unix like 的系統使用者時
基本上都會被我推薦 irssi 這個 command mode 的純文字 irc 程式
(啥?不知道啥是 irc ? Internet Relay Chat 的縮寫,其他請洽 Google 或 Wiki )
然後如果還有 server 的朋友,更是會被我力推 screen + irssi 的組合
好處是,可以遠端持續掛著 irc ,到哪邊都可以 ssh 進去繼續看 irc log
這樣真的很方便,也不用擔心因為沒開自己的電腦,就看不到別人的對話或別人要留給自己的訊息
不過跟太多的朋友推薦了 screen + irssi 後,卻發現一直回來問我問題
原因是... man 寫的陽春到不行,然後就吵著要鄉民教學
所以這篇文章就這麼被誕生了,也順便當我的備忘錄吧 :P
以下是針對我操作時會遇到的問題整理一下
irssi 的程式安裝我就不詳細講了
apt-get or yum 裡面可以直接打撈到
先在這邊釐清第一次使用 screen + irssi 上 irc 的朋友們觀念
請先搞清楚說遇到的問題跟操作,該是由 screen 還是由 irssi 的程式部份來處理
或者本身只是 irc 上面的指令操作罷了
screen 只是方便讓使用者可以利用 server 不關機的優勢,使 irssi 可以連續開著不中斷
然後下次再透過 screen 來接軌回到掛著的 irssi 然後繼續熱血 irc 這樣子
所以,這樣看下面的簡介操作後比較不會搞不懂到底發生啥狀況,如果請教朋友也比較能問到問題處
以下例子皆以 irssi 0.8.10 或以上版本,經實驗都可順利執行
且都以在 FreeNode 上的 OrzLab channel 為例子
(如果不掛 screen 者,這一段請直接跳過)
首先 ssh login 到 server 後
請輸入
screen -RD
-R Reattach if possible, otherwise start a new session.
-D (-r) Detach and logout remote (and reattach here).
然後會進入到新開啟的 screen ,並且 screen 會自動開啟一個新的 window 讓我們使用
接下來直接輸入
irssi
就會在 screen 底下的第一個視窗執行 irssi 這隻程式
如果沒有要掛著 screen 執行 irssi 的朋友
那直接輸入
irssi
就可以直接執行 irssi 這隻程式
進入 irssi 後
直接在提示輸入字元處輸入
/connect irc_server
就會直接連到該個 irc server 了
例如
/connect irc.freenode.net
這樣就會直接連接到 freenode 的 irc server 上
此時,可以先在這邊修改自己要在該 irc server 上的暱稱
如果要修改,則輸入以下格式
/nick you_want_nickname
例如當我輸入
/nick lzy
時,那我顯示出來的暱稱將會是「lzy」的字樣
( irssi 預設的暱稱會是 login 的使用者名稱)
搞定暱稱後,接著我們就可以加入其他頻道了
只要輸入
/j #channel_name
就可以直接加入了,例如
/j #orzlab
這樣就完成加入 #orzlab 這個 irc channel 了
如果該 irc channel 需要 key/password 的話
則輸入:
/j #channel_name password
如果要離開單一個 irc channel 的話
則直接輸入
/wc
就可以離開這個 irc 頻道了
若是要離開一個 irc server 的連線,則輸入
/disconnect irc_server_name
夠簡單了吧?
剛剛是教如何連線而已
現在則是教一些常用的熱鍵與功能
先來看圖說故事一下
上面的這張圖,我們在
[Act: 1,2,4,6,9,10]
這邊發現了幾個不一樣的標示數字
Act 這一欄位會提示你在第幾編號的 channel 上對話狀況,而你還沒切換到該 irc channel 上觀看
灰色一般體的是代表有人登入或登出此 irc channel(如 1,9,10)
白色粗體的是代表有人正在此 irc channel 上談話(如 4,6)
灰色粗體的則是有人在對話中提起你的名字或者呼叫你,總之就是有出現你的名字(如 2)
這是基本識別
再來是知道哪個 channel 有啥狀況後
就是切換到該 irc channel 上觀看了
Alt + 1~0 對應 1~10 的 irc channel 編號
Alt + q~p 對應 11~20 的 irc channel 編號
Ctrl + n/p 可切換 上/下 一個 irc channel
然後按鍵盤上的 PageUP/PageDn 可以直接切換 上/下 頁訊息
如果發現有編碼上的問題,需要轉編碼的話
則可以輸入
/recode add #channel_name code_type
例如要把 orzlab 的 utf8 格式加入,則輸入
/recode add #orzlab utf8
就可以加入此頻道的編碼格式了
然後輸入
/recode
就可以看到加入的編碼列表
(註:因 /recode add 會寫入到設定檔裡,所以僅需要寫入一次即可)
Q&A TIME
Q:如何同時有連接多個IRC server呢?又該怎樣切換不同IRC server來加入該server的IRC channel呢?
A:
請直接再下一次/connect irc_server
就可以再連接一個新的 irc server ,然後按Alt + 1
切換到第一個的特殊頻道上,再按Ctrl + x
來切換目前要下達指令的 IRC server名稱。
Q:如何查看該頻道的所有人呢?
A:
請下達/who
的 irc 指令即可,然後也是一樣按Alt + 1
切換到第一個伺服器狀態頻道上觀看
Q:如何查看單一網友的基本連線資料呢?
A:
請下達 /whois nickname
的 irc 指令即可,然後也是一樣按 Alt + 1 切換到第一個伺服器狀態頻道上觀看
Q:如何傳送私人訊息到指定的朋友暱稱呢?
A:
請下達/msg nickname
即可開啟一個名為指定暱稱的訊息頻道,一樣用alt + 1~0/q~p
來選擇(注意:有些 irc server 會限制此功能,傳訊前請先確認是否有權限)。
Q:已經使用上面所講的方式 screen + irssi 了,那我離開時,是否得下達任何參數或指令?又下次 ssh login 後,需要打什麼指令呢?
A:
為了簡化登入與記憶上的方面,採用 -RD 的參數,所以每次要離開 irssi 時,直接關閉該視窗就好,不需下達任何參數或指令。然後下一次 ssh login 後,只要打 screen -RD
這樣就可以接軌回上次開啟的 irssi 了。
Q:如果遠端連線到 big5 的主機掛 screen + irssi 時,那 client 端是 utf8 時,編碼的亂碼問題如何處理?
A:
首先,先處理 screen 的編碼問題,從 utf8 端要檢視 big5 server 時,先Ctrl + a
,然後輸入:encoding big5 utf8
,把從 server 將送出的 big5 編碼先轉成 utf8 後再送出,然後輸入"/recode"
觀看頻道編碼是否指定正確,照上述設定,理應能順利顯示。
Q:如果不小心按到
Ctrl + z
或鍵盤上的 Pause 時, irssi 被 suspend 了,如何恢復?A:
請直接輸入 fg
後按 enter ,就可以把剛剛被 suspend 的 irssi 給喚醒。
Q:如果我要關閉一個 irssi ,該如何下達指令?
A:
請直接關閉開啟 irssi 的 screen ,請按 Ctrl + k
來關閉 screen window ,然後可以再重新開啟一個全新的 irssi 出來。
Q:要如何自動儲存 irc log ?
A:
在需要 auto irc log 的 irc channel 上輸入 /SET autolog ON
,即可打開 autolog 功能。
Q:要如何指定當開啟 irssi 後,自動連接 irc server 與自動連接 irc channel ?
A:
直接看例子會比較快,先假設我要讓他自動用 lzy 的暱稱連接到 Freenode 的 irc server 上的 #orzlab/network add -nick lzy Freenode
/server add -auto -network Freenode irc.freenode.net 7000
/channel add -auto #orzlab Freenode
如果是需要 key/password 的 irc channel 的話,則把最後一行改為/channel add -auto #irc_channel_name irc_server_name password
也就是直接把密碼接在後面就可以了
如果需要自動對註冊的暱稱進行認證登入的話(設口令為abcde):/network add -autosendcmd "/^msg nickserv ident abcde;wait 2000" Freenode
Q:如何讓 irssi 發出有人提起自己暱稱時的訊息警告(或響鈴)?
A:
在 irssi 裡輸入以下指令,讓 irssi 適時發出聲音訊號:/set beep_when_window_active ON
/set beep_when_away ON
/set beep_msg_level MSGS NOTICES DCC DCCMSGS HILIGHT
/set bell_beeps ON
在 screen 中按下Ctrl +a
,Ctrl + g
的按鍵,讓 screen 把 irssi 發出來的聲音訊號傳出來
或參考絕地大濕的 blog 一文 <會響的 PuTTY + screen + irssi>
Q:如何更換 irssi 的 theme ?
A:
把抓好的 theme (可從官網撈喜歡的),然後複製該 theme 的檔案到 ~/.irssi/ 資料夾路徑,並下達 /SET theme theme_name
來更改該 theme 。
Q:如何自訂 irssi 的 irc 訊息熱鍵?
A:
因為設定比較繁雜,故只提供官網網址,讓有興趣的朋友可以自己去翻閱。
提供幾個額外補充的網站,沒標示語言者,皆為中文
irc 指令介紹 (英文)
irssi startup (英文)
screen 基本觀念與操作介紹
Reference: http://lzy-blah.blogspot.com/2007/08/screen-irssi-irc-q.html
以 screen + irssi 來處理 Unicode 環境及自動轉碼
以下步驟主要都是來自這篇《MiniHowToUTF8》,若已經有裝的就可以略過不理。
---
先裝 PieTTY (用 UTF-8 )或 PuTTY 設定的 Window -> Translation 裡,將 "Received data assumed to be in which character set" 設成 UTF-8
再裝 screen:
cd /usr/ports/sysutils/screen ; make install clean
進入 screen 的 Unicode 模式,打 screen -U,或在 ~/.screenrc 設定檔裡寫 defutf8 on。
(日後若要 attach 回 screen 則用 screen -rU )
最後裝 irssi (IRC client):
cd /usr/ports/irc/irssi ; make install clean
開始執行
env LANG=zh_TW.UTF-8 irssi -c irc.freenode.net
然後打(下面這些行若懶得設定的話,
可直接抓這檔案存為 ~/.irssi/config
ps.為了怕有人亂 autojoin 進進出出,這設定檔預設不 autojoin)
/set term_charset UTF-8
/set recode_fallback UTF-8
/set recode_out_default_charset UTF-8
/set recode_transliterate on
/reocde add #bsdchat
/save
這時候開始打 /j bsdchat 以加入 #bsdchat。
(若還是看不到中文,請先按 ctrl + a 然後打 :encoding utf8 utf8 再按 enter 如下圖)
如果仍然看不到中文或沒辦法輸入 UTF-8 中文,最好檢查一下是否少做了什麼步驟。尤其是 LANG 或 LC_ALL 應該要設成 zh_TW.UTF-8 或 screen 的 UTF-8 模式要開啟之類的...
◆ 記得為自己在 IRC 上所用的 nickname 註冊
請打
/msg nickserv register 想要設定的密碼
來進行註冊,其餘用法請打 /msg chanserv help register 參閱相關說明
◆ QA時間...
(感謝 Cpyang, gslin, Jedi_, jnlin, Eintisy, evilc, llwang 諸位頻眾大德惠賜妙方)
Q:nickserv 跟 chanserv 這是?
A:關於帳號的是 nickserv,關於頻道動作的是 chanserv
Q:關於 nickname 的密碼設定、忘記密碼、被人搶走相關問題,該怎麼辦?
A:/msg nickserv help
Q:怎麼設定類似自動 op?
A: 找有緣人授予 lv 即可 (作法就類似 /msg chanserv access #bsdchat add chinsan 30)
然後 op 部份只要有 lv 一定程度以上,一進頻 chanserv 就會自動給了
Q:為何我一樣是 lv 30 但要發給對方 lv 30 會出現說"You cannot add an access level greater than [29]" ?
A:你現在是 30 就只能給 30 以下的,頂多到 29
Q: 但我下次進來的時候... level 還在嗎?level 不會 reset ?
A: 仍然會在,不會 reset, 只要妳重新登入 freenode 時, 有先打
/msg nickserv IDENTIFY 你所設定的密碼
認證過了,就會自動發 op..
另外,等級夠高的人若被 deop 而沒 op,也可以隨時 /msg chanserv op 要回 op。
Q:何為 op?
A:以下有很妙的回答 XD
16:44 @Jedi_> 表示是接線生
16:44 @Jedi_> 如果你被困在 Matrix 出不來,就要趕快找這些人
16:44 @Jedi_> 請他們告訴你最近的出口在哪裡
16:44 @Jedi_> 這很重要喔,不要忘了
16:44 @Jedi_> 也就是說,生死攸關~
:
:
17:10 mathiss> operator: 我要訂 pizza
Q:recode 只能指定 channel 不能指定 network+channel 嗎?
A:請用 /recode add networkname/channelname UTF-8,
比方說 /recode add freenode/#bsdchat UTF-8
Q:如何設定 channel mode 為 nst ?
A:/msg chanserv set #bsdchat mlock +nst
Q:有等級設定相關說明嗎?
A:/msg chanserv help level 以及 /msg chanserv level #bsdchat list
Reference:
http://blog.bsdchat.com/2008/02/bsdhcat.html
Wednesday, November 24, 2010
FreeBSD 環境設定
利用文書編輯工具修改 root 的 shell (預設為 csh = tcsh) 環境變數設定檔
把 root 的提示符號變成 root@host [/path] #
#ee .cshrc
將 setenv EDITOR vi
改成
setenv EDITOR ee
將 set prompt = "`hostname -s`# "
改成
set prompt = "%{^[[33m%}%n@%m %b%{^[[35m%}%/%b $ "
或
set prompt = "%B%n@%m [%/] # "
或
set prompt = "%{^[[33m%}%B%n@%m [%/] # "
※上面修改的 *.cshrc 內容下次登入才會生效 ,所以馬上執行
# source /etc/csh.cshrc
或
exit 後重新登入
※注意 = 左右都要有一個空白,否則會出錯,導致無法登入,另外如果要統一所有使用者的 prompt 的話,除了更改 /home/使用者/.cshrc 外,最好也在 /etc/csh.cshrc 中設定這一行,另外如要新增新用者也用這種提示,則請在 /usr/share/skel/dot.cshrc 中也加上(或改成)這一行,這是因為:
tcsh 在使用時會先參考 /etc/csh.cshrc 這個檔案,再依 user 去抓其 home 目錄下的 .cshrc,而設定 /usr/share/skel/dot.cshrc 是讓你新使用者時,自動將其 prompt 設定成 set prompt = "%B%n@%m[%/]> "
這種設法,當你在管理多台主機及切換目錄時,就不易搞錯而發生一些慘劇..
※EDITOR 是設定使用者預設的文字編輯器,ee操作起來比較容易些, 如果習慣用 vi 就不用修改 EDITOR
※prompt 這個環境變數可以改變提示符號,DOS也有類似的指令
1. prompt 的使用項目
%? 用來設定上次執行指令的返回碼,在一般 unix 下如無錯誤都是傳回 0
%/ 目前所在路徑
%T 傳回目前的時間格式為24小時制,如 13:30,為方便閱讀,使用 [ 和 ] 包含起來
%B 高亮度使用 %b 則還原為非高亮度
%n 使用者名稱
%m 主機名稱
%# 顯示提示符號,如 root 為 #,其它使用者為 >
另外的 @ : 為自行加入進去的,不是控制項目
2. 顏色的控制部份
顏色控制的格式如下:
%{^[[35m%}
以 % 開頭,用{ }包含,用 ^[[顏色數字代號如35m%設定顏色
如上面的例子是紫色
前景色:
黑 30
紅 31
綠 32
黃 33
藍 34
紫 35
靛 36
白 37
背景色:即前景 +10 即成為背景色
黑 40
紅 41
綠 42
黃 43
藍 44
紫 45
靛 46
白 47
而 0 代表不設定,也就是用原來 monitor 的背景色..
控制方式
[前景色][背景色]prompt控制項
------------------------------------
把其它 user 的提示符號變成 user@host [/path] $
#ee /etc/csh.cshrc
在裡面加入一行
set prompt = "%B%n@%m [%/] $ "
或
set prompt = "%{^[[32m%}%B%n@%m [%/] $ "
※上面修改的 *.cshrc 內容下次登入才會生效 ,所以馬上執行
# source /etc/csh.cshrc
或
exit 後重新登入
1. # vi ~/.bashrc
export PS1="\e[0;32m\u@\h \w #\e[m "
export TERM=dtterm
export PATH=$PATH:/usr/local/bin
2. # vi /.screenrc
hardstatus alwayslastline " %{= W} %-Lw %{= KY}%n%f %t%{-}%+Lw %=| "
cjkwidth off