shell goto

goto shell实用程序允许用户导航到别名目录,并且还支持自动完成。

这个怎么运作

在使用goto之前,您需要注册目录别名。 例如:

 goto -r dev / home / iridakos / development 

然后转到该目录,例如:

 goto dev 

goto中的自动补全

goto带有一个不错的自动完成脚本-每当在goto命令后按Tab键时,Bash或Zsh都会提示您有关可用别名的建议:

$ goto < tab >
bc / etc / bash_completion.d                    
dev / home / iridakos / development
rubies / home / iridakos / .rvm / rubies

安装goto

有几种安装goto的方法。

通过脚本

克隆存储库并以超级用户或root身份运行安装脚本:

git clone https: // github.com / iridakos / goto.git
cd goto
sudo . / install

手动地

在复制文件系统文件goto.sh的地方,并在您的.zshrc中添加一行或.bashrc中源吧。

例如,如果将文件放在主文件夹中,则只需将以下行添加到.zshrc.bashrc文件中:

 source ~ / goto.sh 

MacOS自制软件

MacOS中的Bash Shell提供了一个名为goto的公式:

 brew install goto 

添加彩色输出

 echo -e " \$ include /etc/inputrc \n set colored-completion-prefix on" >> ~ / .inputrc 

笔记:

  • 安装后需要重新启动外壳。
  • 您需要在MacOS中为Bash启用Bash完成功能(请参阅本期 )。
    • 如果未启用它,则可以使用brew install bash-completion安装它。

goto的使用方法

切换到别名目录

要更改为别名目录,请键入:

 goto < alias > 

例如:

 goto dev 

注册别名

要注册目录别名,请键入:

 goto -r < alias > < directory > 

要么

 goto --register < alias > < directory > 

例如:

 goto -r blog / mnt / external / projects / html / blog 

要么

 goto --register blog / mnt / external / projects / html / blog 

笔记:

  • goto 会扩展目录,因此您可以使用以下命令轻松为当前目录添加别名,该目录将自动别名为整个路径:

     goto -r last_release . 
    
  • 别名后按Tab键可提供Shell的默认目录建议。

取消注册别名

要注销别名,请使用:

 goto -u < alias > 

要么

 goto --unregister < alias > 

例如:

 goto -u last_release 

要么

 goto --unregister last_release 

注意:在命令( -u--unregister )后按Tab键,完成脚本将提示您输入已注册别名的列表。

列出别名

要获取您当前注册的别名的列表,请使用:

 goto -l 

要么

 goto --list 

扩展别名

要将别名扩展为其值,请使用:

 goto -x < alias > 

要么

 goto --expand < alias > 

例如:

 goto -x last_release 

要么

 goto --expand last_release 

清理别名

要从文件系统中不再可访问的目录中清除别名,请使用:

 goto -c 

要么

 goto --cleanup 

得到帮助

要查看该工具的帮助信息,请使用:

 goto -h 

要么

 goto --help 

检查版本

要查看工具的版本,请使用:

 goto -v 

要么

 goto --version 

在更改目录之前先推送

要在更改目录之前将当前目录推送到目录堆栈中,请键入:

 goto -p < alias > 

要么

 goto --push < alias > 

恢复到推送目录

要返回推送目录,请输入:

 goto -o 

要么

 goto --pop 

注意:此命令等效于popd,但在goto命令中。

故障排除

如果看到未找到错误命令: Zsh中的compdef ,则意味着您需要加载bashcompinit 。 为此,请将其附加到您的.zshrc文件中:

autoload bashcompinit
bashcompinit

参与其中

goto工具是根据MIT许可条款开放的源代码,欢迎您提供帮助。 要了解更多信息,请访问goto的GitHub存储库中的Contributing部分。

转到脚本

goto()
{
local target
_goto_resolve_db

if [ -z "$1" ]; then
# display usage and exit when no args
_goto_usage
return
fi

subcommand="$1"
shift
case "$subcommand" in
-c|--cleanup)
_goto_cleanup "$@"
;;
-r|--register) # Register an alias
_goto_register_alias "$@"
;;
-u|--unregister) # Unregister an alias
_goto_unregister_alias "$@"
;;
-p|--push) # Push the current directory onto the pushd stack, then goto
_goto_directory_push "$@"
;;
-o|--pop) # Pop the top directory off of the pushd stack, then change that directory
_goto_directory_pop
;;
-l|--list)
_goto_list_aliases
;;
-x|--expand) # Expand an alias
_goto_expand_alias "$@"
;;
-h|--help)
_goto_usage
;;
-v|--version)
_goto_version
;;
*)
_goto_directory "$subcommand"
;;
esac
return $?
}

_goto_resolve_db()
{
GOTO_DB="${GOTO_DB:-$HOME/.goto}"
touch -a "$GOTO_DB"
}

_goto_usage()
{
cat <<\USAGE
usage: goto [<option>] <alias> [<directory>]

default usage:
goto <alias> - changes to the directory registered for the given alias

OPTIONS:
-r, --register: registers an alias
goto -r|--register <alias> <directory>
-u, --unregister: unregisters an alias
goto -u|--unregister <alias>
-p, --push: pushes the current directory onto the stack, then performs goto
goto -p|--push <alias>
-o, --pop: pops the top directory from the stack, then changes to that directory
goto -o|--pop
-l, --list: lists aliases
goto -l|--list
-x, --expand: expands an alias
goto -x|--expand <alias>
-c, --cleanup: cleans up non existent directory aliases
goto -c|--cleanup
-h, --help: prints this help
goto -h|--help
-v, --version: displays the version of the goto script
goto -v|--version
USAGE
}

# Displays version
_goto_version()
{
echo "goto version 1.2.4.1"
}

# Expands directory.
# Helpful for ~, ., .. paths
_goto_expand_directory()
{
builtin cd "$1" 2>/dev/null && pwd
}

# Lists registered aliases.
_goto_list_aliases()
{
local IFS=$' '
if [ -f "$GOTO_DB" ]; then
while read -r name directory; do
printf '\e[1;36m%20s  \e[0m%s\n' "$name" "$directory"
done < "$GOTO_DB"
else
echo "You haven't configured any directory aliases yet."
fi
}

# Expands a registered alias.
_goto_expand_alias()
{
if [ "$#" -ne "1" ]; then
_goto_error "usage: goto -x|--expand <alias>"
return
fi

local resolved

resolved=$(_goto_find_alias_directory "$1")
if [ -z "$resolved" ]; then
_goto_error "alias '$1' does not exist"
return
fi

echo "$resolved"
}

# Lists duplicate directory aliases
_goto_find_duplicate()
{
local duplicates=

duplicates=$(sed -n 's:[^ ]* '"$1"'$:&:p' "$GOTO_DB" 2>/dev/null)
echo "$duplicates"
}

# Registers and alias.
_goto_register_alias()
{
if [ "$#" -ne "2" ]; then
_goto_error "usage: goto -r|--register <alias> <directory>"
return 1
fi

if ! [[ $1 =~ ^[[:alnum:]]+[a-zA-Z0-9_-]*$ ]]; then
_goto_error "invalid alias - can start with letters or digits followed by letters, digits, hyphens or underscores"
return 1
fi

local resolved
resolved=$(_goto_find_alias_directory "$1")

if [ -n "$resolved" ]; then
_goto_error "alias '$1' exists"
return 1
fi

local directory
directory=$(_goto_expand_directory "$2")
if [ -z "$directory" ]; then
_goto_error "failed to register '$1' to '$2' - can't cd to directory"
return 1
fi

local duplicate
duplicate=$(_goto_find_duplicate "$directory")
if [ -n "$duplicate" ]; then
_goto_warning "duplicate alias(es) found: \\n$duplicate"
fi

# Append entry to file.
echo "$1 $directory" >> "$GOTO_DB"
echo "Alias '$1' registered successfully."
}

# Unregisters the given alias.
_goto_unregister_alias()
{
if [ "$#" -ne "1" ]; then
_goto_error "usage: goto -u|--unregister <alias>"
return 1
fi

local resolved
resolved=$(_goto_find_alias_directory "$1")
if [ -z "$resolved" ]; then
_goto_error "alias '$1' does not exist"
return 1
fi

# shellcheck disable=SC2034
local readonly GOTO_DB_TMP="$HOME/.goto_"
# Delete entry from file.
sed "/^$1 /d" "$GOTO_DB" > "$GOTO_DB_TMP" && mv "$GOTO_DB_TMP" "$GOTO_DB"
echo "Alias '$1' unregistered successfully."
}

# Pushes the current directory onto the stack, then goto
_goto_directory_push()
{
if [ "$#" -ne "1" ]; then
_goto_error "usage: goto -p|--push <alias>"
return
fi

{ pushd . || return; } 1>/dev/null 2>&1

_goto_directory "$@"
}

# Pops the top directory from the stack, then goto
_goto_directory_pop()
{
{ popd || return; } 1>/dev/null 2>&1
}

# Unregisters aliases whose directories no longer exist.
_goto_cleanup()
{
if ! [ -f "$GOTO_DB" ]; then
return
fi

while IFS= read -r i && [ -n "$i" ]; do
echo "Cleaning up: $i"
_goto_unregister_alias "$i"
done <<< "$(awk '{al=$1; $1=""; dir=substr($0,2);
system("[ ! -d \"" dir "\" ] && echo " al)}' "$GOTO_DB")"
}

# Changes to the given alias' directory
_goto_directory()
{
local target

target=$(_goto_resolve_alias "$1") || return 1

builtin cd "$target" 2> /dev/null || \
{ _goto_error "Failed to goto '$target'" && return 1; }
}

# Fetches the alias directory.
_goto_find_alias_directory()
{
local resolved

resolved=$(sed -n "s/^$1 \\(.*\\)/\\1/p" "$GOTO_DB" 2>/dev/null)
echo "$resolved"
}

# Displays the given error.
# Used for common error output.
_goto_error()
{
(>&2 echo -e "goto error: $1")
}

# Displays the given warning.
# Used for common warning output.
_goto_warning()
{
(>&2 echo -e "goto warning: $1")
}

# Displays entries with aliases starting as the given one.
_goto_print_similar()
{
local similar

similar=$(sed -n "/^$1[^ ]* .*/p" "$GOTO_DB" 2>/dev/null)
if [ -n "$similar" ]; then
(>&2 echo "Did you mean:")
(>&2 column -t <<< "$similar")
fi
}

# Fetches alias directory, errors if it doesn't exist.
_goto_resolve_alias()
{
local resolved

resolved=$(_goto_find_alias_directory "$1")

if [ -z "$resolved" ]; then
_goto_error "unregistered alias $1"
_goto_print_similar "$1"
return 1
else
echo "${resolved}"
fi
}

# Completes the goto function with the available commands
_complete_goto_commands()
{
local IFS=$' \t\n'

# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "-r --register -u --unregister -p --push -o --pop -l --list -x --expand -c --cleanup -v --version" -- "$1"))
}

# Completes the goto function with the available aliases
_complete_goto_aliases()
{
local IFS=$'\n' matches
_goto_resolve_db

# shellcheck disable=SC2207
matches=($(sed -n "/^$1/p" "$GOTO_DB" 2>/dev/null))

if [ "${#matches[@]}" -eq "1" ]; then
# remove the filenames attribute from the completion method
compopt +o filenames 2>/dev/null

# if you find only one alias don't append the directory
COMPREPLY=("${matches[0]// *}")
else
for i in "${!matches[@]}"; do
# remove the filenames attribute from the completion method
compopt +o filenames 2>/dev/null

if ! [[ $(uname -s) =~ Darwin* ]]; then
matches[$i]=$(printf '%*s' "-$COLUMNS" "${matches[$i]}")

COMPREPLY+=("$(compgen -W "${matches[$i]}")")
else
COMPREPLY+=("${matches[$i]// */}")
fi
done
fi
}

# Bash programmable completion for the goto function
_complete_goto_bash()
{
local cur="${COMP_WORDS[$COMP_CWORD]}" prev

if [ "$COMP_CWORD" -eq "1" ]; then
# if we are on the first argument
if [[ $cur == -* ]]; then
# and starts like a command, prompt commands
_complete_goto_commands "$cur"
else
# and doesn't start as a command, prompt aliases
_complete_goto_aliases "$cur"
fi
elif [ "$COMP_CWORD" -eq "2" ]; then
# if we are on the second argument
prev="${COMP_WORDS[1]}"

if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then
# prompt with aliases if user tries to unregister one
_complete_goto_aliases "$cur"
elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then
# prompt with aliases if user tries to expand one
_complete_goto_aliases "$cur"
elif [[ $prev = "-p" ]] || [[ $prev = "--push" ]]; then
# prompt with aliases only if user tries to push
_complete_goto_aliases "$cur"
fi
elif [ "$COMP_CWORD" -eq "3" ]; then
# if we are on the third argument
prev="${COMP_WORDS[1]}"

if [[ $prev = "-r" ]] || [[ $prev = "--register" ]]; then
# prompt with directories only if user tries to register an alias
local IFS=$' \t\n'

# shellcheck disable=SC2207
COMPREPLY=($(compgen -d -- "$cur"))
fi
fi
}

# Zsh programmable completion for the goto function
_complete_goto_zsh()
{
local all_aliases=()
while IFS= read -r line; do
all_aliases+=("$line")
done <<< "$(sed -e 's/ /:/g' ~/.goto 2>/dev/null)"

local state
local -a options=(
'(1)'{-r,--register}'[registers an alias]:register:->register'
'(- 1 2)'{-u,--unregister}'[unregisters an alias]:unregister:->unregister'
'(: -)'{-l,--list}'[lists aliases]'
'(*)'{-c,--cleanup}'[cleans up non existent directory aliases]'
'(1 2)'{-x,--expand}'[expands an alias]:expand:->aliases'
'(1 2)'{-p,--push}'[pushes the current directory onto the stack, then performs goto]:push:->aliases'
'(*)'{-o,--pop}'[pops the top directory from stack, then changes to that directory]'
'(: -)'{-h,--help}'[prints this help]'
'(* -)'{-v,--version}'[displays the version of the goto script]'
)

_arguments -C \
"${options[@]}" \
'1:alias:->aliases' \
'2:dir:_files' \
&& ret=0

case ${state} in
(aliases)
_describe -t aliases 'goto aliases:' all_aliases && ret=0
;;
(unregister)
_describe -t aliases 'unregister alias:' all_aliases && ret=0
;;
esac
return $ret
}

goto_aliases=($(alias | sed -n "s/.*\s\(.*\)='goto'/\1/p"))
goto_aliases+=("goto")

for i in "${goto_aliases[@]}"
do
# Register the goto completions.
if [ -n "${BASH_VERSION}" ]; then
if ! [[ $(uname -s) =~ Darwin* ]]; then
complete -o filenames -F _complete_goto_bash $i
else
complete -F _complete_goto_bash $i
fi
elif [ -n "${ZSH_VERSION}" ]; then
compdef _complete_goto_zsh $i
else
echo "Unsupported shell."
exit 1
fi
done



它最初在转到的GitHub存储库中以自述文件的形式发布,并在获得许可的情况下被重用。

翻译自: https://opensource.com/article/20/1/directories-autocomplete-linux

shell goto

shell goto_Linux goto shell实用程序简介相关推荐

  1. Shell脚本之一 Shell脚本简介

    一.什么是shell? 我们平时所说的 Shell 可以理解为 Linux 系统提供给用户的使用界面.Shell 为用户提供了输入命令和参数并可得到命令执行结果的环境.当一个用户登录 Linux 之后 ...

  2. linux+shell+func,Linux shell编程笔记总结

    Linux Shell学习笔记简介 Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称.shell是用户和Linux内核之间的接 ...

  3. linux超级基础系列——什么是shell? bash和shell有什么关系

    什么是Shell?        shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核.    ...

  4. Linux:shell编程(shell基本语法)

    参考菜鸟教程:shell教程 目录 什么是shell? shell脚本的执行方式 一.HelloWorld 二.shell中的变量 2.1.定义变量 2.1.1.定义动态变量 2.1.2.删除变量 2 ...

  5. 一文详解shell编程(shell编程笔记)

    本文为快速入门教程,如果你所在的行业需要用到很深入的shell脚本知识,可参考书籍<Linux命令行与shell脚本编程大全>,豆瓣链接:https://book.douban.com/s ...

  6. Linux之shell编程篇-----Shell 函数和Shell 输入/输出重定向

    ​ 最近发现了一个挺厉害的人工智能学习网站,内容通俗易懂,风趣幽默,感兴趣的可以点击此链接进行查看:床长人工智能教程 废话不多说,请看正文! ​ Shell 函数 1. 自定义函数 linux she ...

  7. 7月11日任务 shell介绍、shell结构和执行 、date命令用法、shell脚本中的变量

    2019独角兽企业重金招聘Python工程师标准>>> 20.1 Shell脚本介绍 • shell是一种脚本语言  aming_linux  blog.lishiming.net ...

  8. 69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量

    2019独角兽企业重金招聘Python工程师标准>>> 1.shell脚本介绍: shell是一种脚本语言和传统的开发语言相比,会比较简单: shell有自己语法,可以支持逻辑判断. ...

  9. 反弹shell与正向shell的区别

    正向shell: 攻击机A,受害者B 正向shell:正常大家都能理解 A主动连接B,直接使用ssh命令:受害者的命令行的输入输出转到控制端 反弹shell: 反弹shell就是控制端监听在某TCP/ ...

最新文章

  1. C语言结构体指针与结构体变量作形参的区别
  2. 【图像】尺度不变特征变换算法(SIFT)
  3. android 字符串对齐,android – 使用Spanable String对齐ImageSpan
  4. mysql主从配置错误_mysql主从配置失败,主从通讯失败
  5. C#多线程技术总结(异步)
  6. OJ1042: 数列求和3(递推思想)
  7. 【JS】call,apply,bind
  8. linux中关于ssh实验,操作系统实验三linux的telnetftpssh的相关配置及验证
  9. tomcat基本使用(二)
  10. 吴恩达机器学习笔记3——线性代数
  11. java底层 文件操作,java底层是怎的对文件操作的
  12. zabbix监控TCP连接数
  13. 通过ODBC接口访问人大金仓数据库
  14. XML编辑器oXygen XML Editor V19大版本更新 | 附下载
  15. 计算机网络复习训练题
  16. 解决es集群Yellow与Red的问题
  17. Computer Vision 杂志对何恺明 Rethinking ImageNet Pre-training 的最新评论
  18. pytorch Tensor及其基本操作
  19. 中国计算机学会(CCF)推荐中文科技期刊目录(2019年)
  20. 使用IDM下载百度网盘的文件(亲测有用)

热门文章

  1. [神经网络]U2Net语义分割网络
  2. 记一次对某招聘网站的数据爬取并可视化
  3. 已解决You are using pip version 10.0.1,however version 21.3.1 is available.
  4. lvextend in HP-UX to Increase Space for a Mounted VXFS Filesystem
  5. python结构体排序
  6. linux字体怪异_在 linux 字体下的显示问题及几种解决办法
  7. 常见的距离计算公式—— 切比雪夫距离 ( Chebyshev Distance )
  8. 现代操作系统和 TCP/IP(第二篇)
  9. Warshall算法求有向图的传递闭包
  10. TRBBex:抱着一夜暴富这种心态是很难賺钱的