shell终端多目录间快速cd工具

1.解决的问题当在多个目录间cd的时候,需要输入一大串的路径。例如在不同的项目、不同的分支代码目录跳转,在桌面和文档目录跳转cd ~/Desktop/project_trunkcd ~/Download/cang_lao_shicd ~/code/branch-1.11/cd ~/code/branch-3.1/project/android这个工具就能令这些cd简化成几个字符:g2tg2cg21g2a

2.脚本配置

工具是个shell脚本,附在本文末尾,复制整个代码保存为任意文件名,例如g2.sh,然后在~/.bashrc的最后一行加入单引号里的内容 ‘source g2.sh’ 或 ‘. g2.sh’

3.自定义路径

如果想加入自定义的路径,找到

shortcut_and_paths=(xxxxx)的地方,增加一行就可以了。每行就是一个参数和实际命令。可以看到shortcut_and_paths=( ‘d ~/Desktop’ ‘s ~/Downloads/subversion-1.7.17’ ‘7 ~/Downloads/gmock-1.7.0’ ‘n ~/Documents/department/17无线推广部/内部文档/主管文档’ ‘wk /home/liuhx/Desktop/WebKit/Source/WTF/icu’)第一个空格前是g2的后缀,第一个空格后是cd的路径。增加完需要重新source或者新建一个terminal才会生效。

如果忘了可以g2啥,可以输入g2help(不是g2hell哦)就能列出所有命令。

附脚本:

#!/bin/bash#author # this variable should be complex enough to avoid naming pollutionshortcut_and_paths=( ‘d ~/Desktop’ ‘s ~/Downloads/subversion-1.7.17’ ‘7 ~/Downloads/gmock-1.7.0’ ‘n ~/Documents/department/17无线推广部/内部文档/主管文档’ ‘wk /home/liuhx/Desktop/WebKit/Source/WTF/icu’)for ((i = 0; i < ${#shortcut_and_paths[@]}; i++)); do cmd=${shortcut_and_paths[$i]} shortcut=${cmd%% *} path=${cmd#* } func="g2$shortcut() { cd $path; }" eval $funcdoneg2help() { for ((i = 0; i < ${#shortcut_and_paths[@]}; i++)); docmd=${shortcut_and_paths[$i]}shortcut=${cmd%% *}path=${cmd#* }echo -e "g2$shortcut\t=>\tcd $path" done echo -e "\033[0;33;1mexample: input ‘g2${shortcut_and_paths[0]%% *}’ to run ‘cd ${shortcut_and_paths[0]#* }’\033[0m"}

FAQ:

为什么不用alias呢?因为非交互式shell不会展开alias,,也就是用function就能被其它脚本调用了

转载请注明出处:

一个人行走,若是寂寞了,寻一座霓虹灯迷离闪烁,

shell终端多目录间快速cd工具

相关文章:

你感兴趣的文章:

标签云: