技巧六、Bashmarks
你还没有在.bashrc里使用bashmarks吗?还在等待什么?它真的非常有用。它能帮你保持历史操作,跳回到你经常使用的目录。下面是我的配置文件里脚本,但我想上面的链接能提供你更多技巧:
复制代码 # USAGE:
# s bookmarkname - saves the curr dir as bookmarkname
# g bookmarkname - jumps to the that bookmark
# g b[TAB] - tab completion is available
# l - list all bookmarks
# save current directory to bookmarks
touch ~/.sdirs
function s {
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
mv ~/.sdirs1 ~/.sdirs
echo "export DIR_$1=$PWD" >> ~/.sdirs
}
# jump to bookmark
function g {
source ~/.sdirs
cd $(eval $(echo echo $(echo $DIR_$1)))
}
# list bookmarks with dirnam
function l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*="
}
# list bookmarks without dirname
function _l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
}
# completion command for g
function _gcomp {
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W '`_l`' -- $curw))
return 0
}
# bind completion command for g to _gcomp
complete -F _gcomp g
技巧七、从格式化输出里提取一列(我最常使用的awk技巧)
我几乎天天都会使用它。真的。经常会有一些输出,我只需要其中的第二列,或第三列,下面这个命令就能做到这些:
复制代码#Sample output of git status -s command:
$ git status -s
M .bashrc
?? .vim/bundle/extempore/
# Remove status code from git status and just get the file names
$ git status -s | awk '{print $2}'
.bashrc
.vim/bundle/extempore/
为什么不写个函数,让我们随时都可以用呢?
复制代码 function col {
awk -v col=$1 '{print $col}'
}
这使得提取列非常容易,比如,你不想要第一列?简单:
复制代码$ git status -s | col 2
.bashrc
.vim/bundle/extempore/
技巧八、忽略头x个词
我对xargs很着迷,我感觉它就像一把快刀。但有时候用它获得的结果需要调整一下,也许需要取得一些值。例如,你想去掉下面文件影像里的一些信息:
复制代码function skip {
n=$(($1 + 1))










