一位高级开发人员在 2022 年写了一篇文章,他的 zsh 配置中有以下别名:
alias which='type -a'
但是 zshwhich
默认有,如果我们which
像这样使用which cp
,输出将是
/bin/cp
而输出type -a cp
是
cp is /bin/cp
差异太细微,无法创建别名。那么我可能忽略了哪些实际差异呢?
一位高级开发人员在 2022 年写了一篇文章,他的 zsh 配置中有以下别名:
alias which='type -a'
但是 zshwhich
默认有,如果我们which
像这样使用which cp
,输出将是
/bin/cp
而输出type -a cp
是
cp is /bin/cp
差异太细微,无法创建别名。那么我可能忽略了哪些实际差异呢?
我的 zsh 中有以下别名:
alias -- -='cd -'
alias ..='cd ..'
alias ...='cd ../..'
当我通过手动输入并按回车键切换到 fish 时fish
,第一个别名会导致错误:
- (line 1): function: -: invalid function name
function - --description 'alias -=cd -'; cd - $argv
如何解决这个问题?
我正在寻找批量压缩一些 pdf 的方法,使用其他地方概述的转换为/从 ps 技巧。我在 .zshrc 文件中定义了一个函数:
function pdfcompress() {
for f in "$1"; do
echo "compressing $1"
pdf2ps "$f" "$f".ps
ps2pdf -dPDFSETTINGS=/default "$f".ps "$f"
rm "$f".ps
done
}
我的理解是,我应该能够传递一个 pdf 列表,然后它会压缩它们。但是,发生了以下情况:
% ls | pdfcompress
compressing
Error: /undefinedfilename in --findlibfile--
Operand stack:
()
Execution stack:
%interp_exit .runexec2 --nostringval-- findlibfile --nostringval-- 2 %stopped_push --nostringval-- findlibfile
findlibfile false 1 %stopped_push findlibfile 1815 1 3 %oparray_pop findlibfile
Dictionary stack:
--dict:743/1123(ro)(G)-- --dict:0/20(G)-- --dict:85/200(L)--
Current allocation mode is local
GPL Ghostscript 10.04.0: Unrecoverable error, exit code 1
GPL Ghostscript 10.04.0: Device 'pdfwrite' requires an output file but no file was specified.
**** Unable to open the initial device, quitting.
由于某种原因,似乎没有一个文件名被传递。我也尝试过使用find . -iname "*.pdf" -exec pdfcompress
和管道到 的方法xargs pdfcompress
,但这些方法都不起作用,因为该函数不在环境中。其他时候,占位符变量似乎被解释为文字字符串。
我已经尝试让它以某种形式工作了一个多小时,但没有成功。有什么建议吗?
我有一个想要维护的 pdf 文件夹结构,因此如果可能的话,在目录内递归工作的解决方案将是最好的。
谢谢你!
我已经安装了 zsh 和 oh-my-zsh,但我的终端(xfce4-terminal
在EndeavourOS(基于 Arch)上)总是以 启动bash
。
假设我已经执行了一个命令
foo a b c d e f
然后我可以用来$!
访问该命令的最后一个参数,即如果我输入
echo !$
它变成了echo f
。同样我可以使用
echo !:4
访问第 4 个元素,d
但这意味着我必须从左边数。有没有从右边访问第 n 个元素的符号?
我必须在 zsh 中为命令设置值自动完成(在下面的最小示例中,我将使用 进行展示testcmd
)。因此,我当前的代码可以很好地处理硬编码值:
function testcmd()
{
echo "Nothing to do, just a test command"
}
_test_complete() {
_values \
"Possible values" \
foo'[Foo]' \
bar'[Bar baz]' \
}
compdef _test_complete testcmd
当我输入时testcmd <tab>
,我正确地得到了以下所需的渲染:
$ testcmd
Possible values
bar -- Bar baz
foo -- Foo
但是正如您所看到的,这些值在函数内部是硬编码的,理想情况下,函数应该从变量中检索它们。
因此很自然地,我将值放入变量中,values_variable
如下所示:
function testcmd()
{
echo "Nothing to do, just a test command"
}
_test_complete() {
local values_variable
values_variable="foo'[Foo]' \
bar'[Bar baz]' \ "
_values \
"Possible values" \
${values_variable}
}
compdef _test_complete testcmd
但是当我尝试时testcmd <tab>
它完全失败了:
$ testcmd
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
$ testcmd
echo ${values_variable} | sed "s/ /\\ /g"
;$values_variable
命令eval
来假装它的内容直接在定义中输入_values
;eval
并逃脱eval $(echo ${values_variable} | sed "s/ /\\ /g")
;echo "$values_variable" | while read -r line; do
eval "$line"
done
${(@f)values_variable}̀
;在 如何使用to (zsh 补全) tread传递文件内容cat
_values
中,我找到了从外部文件导入值的解决方案,但用户似乎面临着相同的空格转义问题。但是,我无法将其调整为内部变量的情况$values_variables
。
我自然尝试了这个,但它也不起作用:
_test_complete() {
local values_variable
values_variable="foo'[Foo]' \
bar'[Bar baz]' \ "
OLD_IFS=$IFS
IFS=$'\n'
_values \
"Possible values" \
${values_variable}
IFS=$OLD_IFS
}
_values
如何从变量加载要提供给自动完成函数内部的值?
function tail() {
shift 1
echo $@
}
% tail one two three
two three
我怎样才能写出这个简单函数的反函数?
我想实现
% init one two three
one two
并获取变量中除最后一个参数之外的所有参数$@
,因此该函数可以写成
function init() {
# ???
echo $@
}
我已经尝试过unset '@[-1]'
,,unset '[@:-1]'
来自https://unix.stackexchange.com/a/611717/696135,但这些不起作用
我已经尝试过set -- "${@:1:$(($#-1))}"
,,来自https://stackoverflow.com/questions/20398499/remove-last-argument-from-argument-list-of-shell-script-bashunset "@[${#@[@]}-1]"
,但这些不起作用set -- "${@:1:$#-1}"
不起作用的脚本示例:
function init() {
set -- ${@:1:$#-1}
echo $@
}
init one two three
以下脚本非递归地删除常规空文件(不包括点文件),然后非递归地删除空目录(不包括点文件),并忽略.DS_Store
文件的存在(也就是说,如果目录只包含一个.DS_Store
文件,那么该目录仍然会被删除)。
rm -f -- *(.L0)
setopt extended_glob
has_files_other_than_DS_Store() [[ -n $REPLY/^(#i).ds_store(#qNDY1) ]]
remove_empty_macos_dirs() {
local dir ret=0
for dir do
rm -f -- $dir/.(#i)ds_store(N.) && rmdir -- $dir || ret=$?
done
return $ret
}
remove_empty_macos_dirs *(/^+has_files_other_than_DS_Store)
有人能演示如何使其递归工作吗?也就是说,第一步是递归删除所有空的常规文件。然后递归删除空目录。
我会自己尝试,但说实话,我只是害怕尝试这个rm
命令。
我一直在按照本指南https://thevaluable.dev/zsh-install-configure-mouseless/来设置我的 autocmp 。我在 MacOS 环境中,但由于某种原因,当我尝试自动完成选项时,它没有列出完整的选项集。这是我的自动完成配置:
#Completion
autoload -Uz compinit
compinit
zmodload zsh/complist
#Amazing article https://thevaluable.dev/zsh-install-configure-mouseless/
zstyle ':completion:*' completer _extensions _complete _approximate
zstyle ':completion:*' menu select
zstyle ':completion:*' file-sort modification
zstyle ':completion:*:*:*:*:descriptions' format '%F{green}-- %d --%f'
zstyle ':completion:*' complete-options true
zstyle ':completion:*' matcher-list 'r:|=*' 'l:|=* r:|=*' #https://unix.stackexchange.com/a/259511
zstyle ':completion:*' verbose yes
zstyle ':completion:*' group-name ''
zstyle ':completion:*' list-colors 'di=34:fi=31:no=33;00'
# zstyle ':completion:*' file-list all #Unfortunetly this messes up colouring
#Move with VIM keys Autocompletion
bindkey '^[[Z' reverse-menu-complete
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
当我尝试自动完成内置 shell 命令的选项时,我得到以下信息:
❯ cp -
-- option --
-H -- follow symlinks on the command line in recursive mode
-L -- follow all symlinks in recursive mode
-P -- do not follow symlinks in recursive mode (default)
-R -- copy directories recursively
-X -- don\'t copy extended attributes or resource forks
-a -- archive mode, same as -RpP
-f -- force overwriting existing file
-i -- confirm before overwriting existing file
-n -- don\'t overwrite existing file
-p -- preserve timestamps, mode, owner, flags, ACLs, and extended attributes
-v -- show file names as they are copied
我希望获得cp
文章中的完整选项列表。这可能是 MacOs 中 zsh 补全的问题吗?
我有这个 zsh 函数:
f() {
/usr/bin/find . -type f | less
}
当我运行此函数时,使用 Ctrl+z 暂停,然后运行 fg,它显示zsh: suspended (tty output) /usr/bin/find . -type f |
并且我无法恢复。这是为什么?我该如何恢复?
arch% f
zsh: suspended /usr/bin/find . -type f |
arch% fg
[2] - continued /usr/bin/find . -type f |
zsh: suspended (tty output) /usr/bin/find . -type f |
zsh: suspended (tty output) /usr/bin/find . -type f |
arch%
如果我/usr/bin/find . -type f | less
直接从命令行运行而不将其作为函数,它将正常暂停和恢复。