Skip to main content

命令行的高级片段管理器。

项目描述

片段

snippet允许您在命令行上创建、查看和使用片段。

用法

片段 Cli 预览

# Add a new snippet to the database
$ snippet -e archive/extract-tgz -f 'tar -xzvf <archive>'

# Edit a snippet (will open vim)
$ snippet -e archive/extract-tgz

# Search and use a snippet which include the term "extract" (will open fzf)
$ snippet -t extract

# Fill snippet with a value
$ snippet -t archive/extract-tgz /path/to/foo.tar.gz
tar -xvf /path/to/foo.tar.gz

# Fill snippet with multiple values
$ snippet -t archive/extract-tgz /path/to/foo.tar.gz /path/to/bar.tar.gz
tar -xvf /path/to/foo.tar.gz
tar -xvf /path/to/bar.tar.gz

# Fill snippet with multiple values while using repeatable placeholders (e.g. <file...>)
$ snippet -f "tar -czvf <archive> <file...>" /path/to/foo.tar file=foo bar
tar -czvf /path/to/foo.tar.gz foo bar

# Using presets (e.g. '<datetime>' to include current datetime)
$ snippet -f "tar -czvf '<datetime>.tar.gz' <file...>" file=foo bar
tar -czvf 20770413000000.tar.gz foo bar

# Import values from file
$ cat <<EOF > files.txt
foo
bar
EOF
$ snippet -f "tar -czvf '<datetime>.tar.gz' <file...>" file:files.txt
tar -czvf 20770413000000.tar.gz foo bar

# Using optionals
$ snippet -f "python3 -m http.server[ --bind<lhost>][ <lport>]"
python3 -m http.server
$ snippet -f "python3 -m http.server[ --bind<lhost>][ <lport>]" lport=4444
python3 -m http.server 4444

# Using defaults
$ snippet -f "python3 -m http.server[ --bind<lhost>] <lport=8000>"
python3 -m http.server 8000

# Using codecs
$ snippet -f "tar -czvf <archive|squote> <file:squote...>" /path/to/foo.tar file=foo bar
tar -czvf '/path/to/foo.tar.gz' 'foo' 'bar'

设置

pip3 install snippet-cli

要启用 bash-completion,您可以在 .bashrc 中添加以下行:

eval "$(register-python-argcomplete3 snippet)"

高级用法

  1. 将数据分配给占位符
    1. 使用位置参数
    2. 使用环境变量
    3. 使用预设
  2. 使用字符串格式
    1. 使用即时转换
    2. 使用来自管道的输入
    3. 使用模板
    4. 使用默认值
    5. 使用选项
    6. 使用编解码器
  3. 执行命令
  4. 也可以看看

将数据分配给占位符

要将数据分配给占位符,您有几个选项:

使用位置参数

将数据分配给占位符的最直接的方法是使用位置参数:

$ snippet -f "echo 'hello <arg1>';" snippet
echo 'hello snippet';

要将多个值分配给特定占位符,您需要显式声明应将值分配给的占位符:

$ snippet -f "echo '<arg1> <arg2>';" hello arg2=snippet world
echo 'hello snippet';
echo 'hello world';

使用输入文件

值可以直接从文件中导入:

$ cat <<EOF > input.txt
world
universe
EOF
$ snippet -f "echo 'hello <arg1>';" arg1:input.txt
echo 'hello world';
echo 'hello universe';

使用环境变量

snippet评估环境变量并将数据分配给任何未设置的占位符。为避免与其他环境变量发生冲突,snippet仅评估小写变量名称:

$ export arg1=snippet
$ snippet -f "echo 'hello <arg1>';"
echo 'hello snippet';

要将多个值分配给占位符,必须使用以下语法:

$ export arg1="\('snippet' 'world'\)"
$ snippet -f "echo 'hello <arg1>';"
echo 'hello snippet';
echo 'hello world';

请注意,位置参数可能会覆盖环境变量的评估:

$ export arg1=snippet
$ snippet -f "echo 'hello <arg1>';" world
echo 'hello world';

使用预设

snippet附带一组可自定义的预设占位符,可直接在您的格式字符串中使用(.snippet/snippet_profile.py有关更多信息,请参阅 )。预设占位符可能具有
分配给它们的常量但也动态生成的值:

$ snippet -f "echo '<datetime>';" 
echo '20200322034102';

使用字符串格式

要使用字符串格式,您有几个选项:

使用 --format-string 参数

如果您阅读了上一节,您已经知道这个-f | --format-string论点:

$ snippet -f "echo 'hello snippet';"
echo 'hello snippet';

使用来自管道的输入

设置字符串格式的另一个选项是使用管道:

$ echo "echo 'hello snippet'" | snippet
echo 'hello snippet';

使用模板

snippet允许您使用-t | --template参数从文件中导入格式字符串。

有两种创建模板的方法:

  1. 创建一个带有.snippet扩展名的文件:
$ echo -n "echo 'hello, <arg>!'" > example.snippet
$ snippet -t example.snippet world!
  1. 使用参数创建模板-e | --edit
# Create a template called example with the specified string format
$ snippet -e example -f "echo 'hello, <arg>!'"
# Open vim to edit or add a new template
$ snippet -e example world!
# Use the template
$ snippet -t example world!

如果您启用了 bash-completion,您可以按<TAB>两次以自动完成模板名称。

此外,-t | --template当未找到指定的模板名称时,该参数将打开交互式搜索提示。

要列出所有可用模板,您可以使用该--list-templates 参数。

使用编解码器

snippet支持简单的字符串转换。可以使用 --list-codecs参数查看可用编解码器的列表。

要转换占位符,请使用以下<PLACEHOLDER[|CODEC[:ARGUMENT] ...]>格式:

$ snippet -f "<arg|b64>" "hello snippet"
aGVsbG8gcmV2YW1w
$ snippet -f "<arg> <arg|b64|b64>" "hello snippet"
hello snippet YUdWc2JHOGdjbVYyWVcxdw==
$ snippet -f "<arg...|join:', '>!" arg=hello snippet
hello, snippet!

使用默认值

snippet支持为占位符指定默认值:

$ snippet -f "<arg1> <arg2='default'>" hello
hello default

使用选项

snippet支持用方括号括起来以字符串格式指定可选部分:

$ snippet -f "<arg1> [<arg2>]" hello snippet
hello snippet
$ snippet -f "<arg1> [<arg2>]" hello
hello
$ snippet -f "<arg> [my <arg2='snippet'>]" hello
hello my snippet
$ snippet -f "<arg> [my <arg2='snippet'>]" hello arg2=
hello

如果您需要方括号作为字符串格式的一部分,则需要相应地转义它们:

$ snippet -f "\[<arg>\]" hello
[hello]

使用可重复项

如果为占位符指定多个值,snippet将打印所有可能的排列。由于这并不总是您想要做的事情,因此snippet允许将占位符标记为可重复的。这是通过在占位符末尾放置三个点来完成的。默认情况下,与可重复占位符关联的参数由空格分隔。要指定自定义字符序列,您可以使用join编解码器:

$ snippet -f "<arg1>" hello world
hello
world
$ snippet -f "<arg1...>" hello world
hello world
$ snippet -f "<arg1...|join:','>" hello world
hello,world

执行命令

snippet可用于轻松地按顺序执行交替命令:

$ snippet -f "echo 'hello <arg1>';" arg1=snippet world | bash
hello world
hello snippet

使用xargs生成的命令也可以并行执行:

snippet -f "echo 'hello <arg1>';" arg1=snippet arg1=world | xargs --max-procs=4 -I CMD bash -c CMD
hello world
hello snippet

也可以看看

为了充分利用此工具,您还可以考虑研究以下项目:

  • bgl - 管理全局 bash 环境变量

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

内置分布

snippet_cli-2.0.1-py3-none-any.whl (42.4 kB 图哈希)

已上传 py3