项目作者: bytebutcher

项目描述 :
advanced snippet manager for the command-line.
高级语言: Python
项目地址: git://github.com/bytebutcher/snippet.git
创建时间: 2020-03-15T00:33:56Z
项目社区:https://github.com/bytebutcher/snippet

开源协议:GNU General Public License v3.0

下载


snippet

snippet allows you to create, view and use snippets on the command-line.

Usage

Snippet-Cli Preview

  1. # Add a new snippet to the database
  2. $ snippet -e archive/extract-tgz -f 'tar -xzvf <archive>'
  3. # Edit a snippet (will open vim)
  4. $ snippet -e archive/extract-tgz
  5. # Search and use a snippet which include the term "extract" (will open fzf)
  6. $ snippet -t extract
  7. # Fill snippet with a value
  8. $ snippet -t archive/extract-tgz /path/to/foo.tar.gz
  9. tar -xvf /path/to/foo.tar.gz
  10. # Fill snippet with multiple values
  11. $ snippet -t archive/extract-tgz /path/to/foo.tar.gz /path/to/bar.tar.gz
  12. tar -xvf /path/to/foo.tar.gz
  13. tar -xvf /path/to/bar.tar.gz
  14. # Fill snippet with multiple values while using repeatable placeholders (e.g. <file...>)
  15. $ snippet -f "tar -czvf <archive> <file...>" /path/to/foo.tar file=foo bar
  16. tar -czvf /path/to/foo.tar.gz foo bar
  17. # Using presets (e.g. '<datetime>' to include current datetime)
  18. $ snippet -f "tar -czvf '<datetime>.tar.gz' <file...>" file=foo bar
  19. tar -czvf 20770413000000.tar.gz foo bar
  20. # Import values from file
  21. $ cat <<EOF > files.txt
  22. foo
  23. bar
  24. EOF
  25. $ snippet -f "tar -czvf '<datetime>.tar.gz' <file...>" file:files.txt
  26. tar -czvf 20770413000000.tar.gz foo bar
  27. # Using optionals
  28. $ snippet -f "python3 -m http.server[ --bind<lhost>][ <lport>]"
  29. python3 -m http.server
  30. $ snippet -f "python3 -m http.server[ --bind<lhost>][ <lport>]" lport=4444
  31. python3 -m http.server 4444
  32. # Using defaults
  33. $ snippet -f "python3 -m http.server[ --bind<lhost>] <lport=8000>"
  34. python3 -m http.server 8000
  35. # Using codecs
  36. $ snippet -f "tar -czvf <archive|squote> <file:squote...>" /path/to/foo.tar file=foo bar
  37. tar -czvf '/path/to/foo.tar.gz' 'foo' 'bar'

Setup

  1. pip3 install snippet-cli

To enable bash-completion you might add following line to your .bashrc:

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

Advanced usage

  1. Assigning data to placeholders
    1. Using positional arguments
    2. Using environment variables
    3. Using presets
  2. Using string formats
    1. Using on-the-fly transformation
    2. Using input from a pipe
    3. Using templates
    4. Using defaults
    5. Using optionals
    6. Using codecs
  3. Executing commands
  4. See also

Assigning data to placeholders

To assign data to a placeholder you have several options:

Using positional arguments

The most straight forward way of assigning data to a placeholder is to use positional arguments:

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

To assign multiple values to a specific placeholder you need to explicitly declare the placeholder to which the
value should be assigned to:

  1. $ snippet -f "echo '<arg1> <arg2>';" hello arg2=snippet world
  2. echo 'hello snippet';
  3. echo 'hello world';

Using input files

Values can be directly imported from a file:

  1. $ cat <<EOF > input.txt
  2. world
  3. universe
  4. EOF
  5. $ snippet -f "echo 'hello <arg1>';" arg1:input.txt
  6. echo 'hello world';
  7. echo 'hello universe';

Using environment variables

snippet evaluates environment variables and assigns data to any unset
placeholder. To avoid running into conflict with other environment variables snippet only evaluates
lower-case variable names:

  1. $ export arg1=snippet
  2. $ snippet -f "echo 'hello <arg1>';"
  3. echo 'hello snippet';

To assign multiple values to a placeholder following syntax must be used:

  1. $ export arg1="\('snippet' 'world'\)"
  2. $ snippet -f "echo 'hello <arg1>';"
  3. echo 'hello snippet';
  4. echo 'hello world';

Note that positional arguments may override the evaluation of environment variables:

  1. $ export arg1=snippet
  2. $ snippet -f "echo 'hello <arg1>';" world
  3. echo 'hello world';

Using presets

snippet ships with a customizable set of preset placeholders which can be
directly used in your format string
(see .snippet/snippet_profile.py for more information). Preset placeholders may have constant
but also dynamically generated values assigned to them:

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

Using string formats

To use string formats you have several options:

Using the —format-string argument

If you read the previous section you already know the -f | --format-string argument:

  1. $ snippet -f "echo 'hello snippet';"
  2. echo 'hello snippet';

Using input from a pipe

Another option to set the string format is by using a pipe:

  1. $ echo "echo 'hello snippet'" | snippet
  2. echo 'hello snippet';

Using templates

snippet allows you to import format strings from a file by using the -t | --template argument.

There are two ways of creating a template:

  1. Create a file with the .snippet extension:

    1. $ echo -n "echo 'hello, <arg>!'" > example.snippet
    2. $ snippet -t example.snippet world!
  2. Create a template using the -e | --edit argument:

    1. # Create a template called example with the specified string format
    2. $ snippet -e example -f "echo 'hello, <arg>!'"
    3. # Open vim to edit or add a new template
    4. $ snippet -e example world!
    5. # Use the template
    6. $ snippet -t example world!

If you have bash-completion enabled you can press <TAB> twice to autocomplete
template names.

In addition the -t | --template argument will open an interactive search prompt
when the specified template name was not found.

To list all available templates you can use the --list-templates
parameter.

Using codecs

snippet supports simple string transformation. A list of available codecs can be viewed by using the
--list-codecs argument.

To transform a placeholder use the <PLACEHOLDER[|CODEC[:ARGUMENT] ...]> format:

  1. $ snippet -f "<arg|b64>" "hello snippet"
  2. aGVsbG8gcmV2YW1w
  3. $ snippet -f "<arg> <arg|b64|b64>" "hello snippet"
  4. hello snippet YUdWc2JHOGdjbVYyWVcxdw==
  5. $ snippet -f "<arg...|join:', '>!" arg=hello snippet
  6. hello, snippet!

Using defaults

snippet supports specifying default values for your placeholders:

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

Using optionals

snippet supports specifying optional parts in the string format by surrounding them with
square-brackets:

  1. $ snippet -f "<arg1> [<arg2>]" hello snippet
  2. hello snippet
  3. $ snippet -f "<arg1> [<arg2>]" hello
  4. hello
  5. $ snippet -f "<arg> [my <arg2='snippet'>]" hello
  6. hello my snippet
  7. $ snippet -f "<arg> [my <arg2='snippet'>]" hello arg2=
  8. hello

If you need square-brackets to be part of the string format you need to
escape them accordingly:

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

Using repeatables

If you specify multiple values for placeholders snippet will print all possible permutations.
Since this is not always the thing you wanna do snippet allows marking placeholders as repeatable.
This is done by placing three dots at the end of a placeholder. By default arguments which are
associated with a repeatable placeholder are separated by space.
To specify a custom character sequence you may use the join codec:

  1. $ snippet -f "<arg1>" hello world
  2. hello
  3. world
  4. $ snippet -f "<arg1...>" hello world
  5. hello world
  6. $ snippet -f "<arg1...|join:','>" hello world
  7. hello,world

Executing commands

snippet can be used to easily execute alternating commands in sequence:

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

Using xargs the resulting commands can also be executed in parallel:

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

See also

To make the most out of this tool you might also consider to look into the following projects:

  • bgl - manage global bash environment variables