分享一个有趣的.bashrc设置


在使用Mac与Linux的命令行终端时,经常会在一些目录之间频繁的切换,于是我参考了一些资料,在.bashrc中写了几个小函数,来方便这样的操作。

操作示例
1.进入某个目录并通过mark命令创建一个链接
[dong@Dong-MacBookPro ~]$ cd Downloads/Pictures/
[dong@Dong-MacBookPro Pictures]$ mark pictures

2.执行jump命令,在敲Tab键后的自动完成列表中选择想要进入的链接
[dong@Dong-MacBookPro Pictures]$ cd
[dong@Dong-MacBookPro ~]$ jump
chrome github pictures tmp
[dong@Dong-MacBookPro ~]$ jump pictures
[dong@Dong-MacBookPro Pictures]$ pwd
/Users/dong/Downloads/Pictures

3.执行unmark命令,在敲Tab键后的自动完成列表中选择想要删除的链接
[dong@Dong-MacBookPro Pictures]$ cd
[dong@Dong-MacBookPro ~]$ unmark
chrome github pictures tmp
[dong@Dong-MacBookPro ~]$ unmark pictures
remove /Users/dong/.marks/pictures? y
[dong@Dong-MacBookPro ~]$ jump
chrome github tmp

相关代码:

# Filesystem Markers & Jump
export MARKPATH=$HOME/.marks
function jump(){
  cd -P $MARKPATH/$1 2>/dev/null || echo "No such mark: $1"
}
function mark(){
  mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH/$1
}
function unmark(){
  rm -i $MARKPATH/$1
}

function _marks(){
  COMPREPLY=()
  local cur=${COMP_WORDS[COMP_CWORD]};
  local com=${COMP_WORDS[COMP_CWORD-1]};
  case $com in
    'jump')
      local marks=($(ls ${MARKPATH}))
      COMPREPLY=($(compgen -W '${marks[@]}' -- $cur))
      ;;
    'unmark')
      local marks=($(ls ${MARKPATH}))
      COMPREPLY=($(compgen -W '${marks[@]}' -- $cur))
      ;;
  esac
}
complete -F _marks jump
complete -F _marks unmark

  1. #1 by holyzhou on 2015/03/14 - 11:32

    彩总 最近很忙吧 好久没更新了。

(will not be published)
*