2014-02-28 45 views
1

bang歷史機制允許指定後向相對命令,如!-2重新啓動倒數第二個命令。這很好,但如果你啓動history,你將只有命令序列號,這取決於你的歷史長度可能會更長。zsh:顯示反向歷史編號

所以我想是一種方法來定義命令extended_history至少在Linux上打印

-5 12342 print example 
[…] 
-2 12345 print foo 
-1 12346 print bar 
+0

我感到非常驚訝。'zsh'沒有這個作爲一個內置的選擇! – sanmiguel

回答

2

有一個工具tac(它打印以相反的順序線)和nl(該數字線) 。

所以這應該做的伎倆,雖然沒有領先-

extended_history() { 
    history "[email protected]" | tac | nl | tac 
} 

如果你真的想-

extended_history() { 
    history "[email protected]" | tac | nl | tac | sed 's/^\(*\)\([0-9]\)/\1-\2/' 
} 
+0

出於某種原因,好的反向數字有一個移位2,所以這裏是我成功使用的:'歷史'$ @「| tac | nl -v 2 | sed -e's/\([0-9] \)/ - \ 1 /'| tac'。 – psychoslave

2

historyfc -l的代名詞,而我不知道的方法使用fc來顯示你想要的。幾乎肯定可以擴展zsh來提供這樣的命令,儘管我不太願意寫一個命令。一個簡單的awk命令可以處理輸出的history,雖然:

history | awk "{print \$1-$HISTCMD, \$0}"