how to seperate compadd values with line break (zsh) - autocomplete

I have a compadd function that is meant to list AWS instances tags and return that list as completion options.
_aws_instance_by_name() {
# TODO: Figure out partial completion
local instances=$(aws ec2 describe-instances --query "Reservations[*].Instances[*].{Name:Tags[?Key=='Name']|[0].Value}" --output text)
[[ ${DEBUG} ]] && echo -e "\033[34;1m[DEBUG]\033[0m ${instances}"
compadd ${instances}
}
compdef _aws_instance_by_name aws_instance_id_by_name
But when I hit tab I get all options in option delimited by \n
foo\nbar\nSuper fancy
How do I split each value into the returned suggestions?

#compdef aws_instance_id_by_name
# Wrap output in double quotes to
# preserve all whitespace.
local output="$( <command> )"
# Split on line breaks.
local -a instances=( "${(f)output}" )
# Use convenience function to get
# fancy completion features for
# free. No need to figure out
# partial completion yourself.
_wanted aws-instances 'AWS instance' \
compadd -a instances
If you paste the above into a text file whose name starts with _ and save it into a dir that’s in your $fpath, it will get picked up automatically when you run compinit. No need to call compdef.
Documentation:
Command substitution
Parameter expansion flags
Completion system:
Utility functions
Autoloaded files

Related

How to add tab-completion to command with two arguments (and tab complete both)?

Let's have a command command. This command has an option -o that takes 2 arguments. I'd like to add tab-completion to both of those arguments.
I've tried
complete -c command -x -s o -a "complete first arg"
I but cannot add tab-completion to the second argument.
I would like to autocomplete the command when no option is specified. this work fine:
complete -c command -a "no option completion"
but when it I hit tab after the first argument in -o option, those ^ completions are shown.
like this:
command -o "fist" <tab>
no
option
completion
if I cannot add completion for the second argument I'd like to at least remove those completions.
This command has an option -o that takes 2 arguments.
That's quite unusual. Are you sure about that?
Usually, you'll either have options that take one argument, or options that act as "flags" and change all other arguments. So you would just check their presence.
"-o" "-old-style" options also aren't as common as "--gnu-style" long options or "-s" short options, so I suggest double-checking.
complete -c command -a "no option completion"
This means to offer "no", "option" and "completion" if the command is "command".
There is no condition specified, so these are always offered.
What you want is to use the "--condition" (or "-n") option to complete. This takes script (as a string) that is executed. If it returns 0 (i.e. true), the corresponding completion (the rest of that complete invocation - the option and arguments) is offered.
Something like
# The first condition is just to see that `commandline -opc`,
# which tokenizes (-o) all tokens of the current process (-p)
# up to (but not including) the cursor (-c)
# returns just one token - which must be the command.
#
# Alternatively this condition could also be
# the inverse of the other conditions
# (offer this if nothing else would be)
complete -c command -n 'test (count (commandline -opc)) -lt 2' -a 'stuff for no argument'
# The first argument to the option we handle via "-a" to the option
complete -c command -o the-option -a 'the first argument'
# The second argument needs to be offered
# if the second-to-last token is the option.
#
# This is incomplete, because theoretically a token that
# _looks_ like the option could be offered as an argument to _another_ option.
complete -c command -n 'set -l option (commandline -opc)[-2]; test "$option" = "-o"' -a 'the second argument'

How to expand environment variables using keyboard shortcut in Fish?

In Bash, the shortcut Esc Ctrl-e can be used to expand an environment variable at the shell:
$ echo $PATH
/home/joe
$ $PATH<Press Esc Ctrl-e>
$ /home/joe
Is there a shortcut to achieve something similar in Fish?
You could do something like this
function bind_expand_all
# what are the tokens of the current command line
set tokens (commandline --tokenize)
# erase the current command line (replace with empty string)
commandline -r ""
for token in $tokens
# append the expanded value of each token followed by a space
commandline -a (eval echo $token)" "
end
# move the cursor to the end of the new command line
commandline -C (string length (commandline))
end
then
bind \e\ce bind_expand_all
And if this is your current command line (with the cursor at the underscore):
$ echo $HOME (date -u)_
when you hit AltCtrle, you get
$ echo /home/jackman Thu May 10 19:27:18 UTC 2018 _
To store that binding permanently, add it to your fish_user_key_bindings function (create it if it does not exist):
Key bindings are not saved between sessions by default. Bare bind statements in config.fish won't have any effect because it is sourced before the default keybindings are setup. To save custom keybindings, put the bind statements into a function called fish_user_key_bindings, which will be autoloaded.
https://fishshell.com/docs/2.7/commands.html#bind
A little nicer:
function bind_expand_all
set -l expanded
for token in (commandline --tokenize)
set expanded $expanded (eval echo $token)
end
set -l new (string join " " $expanded)
commandline -r $new
commandline -C (string length $new)
end

Passing optional parameters to rundeck script

I have a python script that I would like to run using rundeck that is invoked as follows:
createInstance.py [-n <name>] <env> <version>
Where name is optional and env and version are required.
e.g. if I want to call the script with a name I would call:
createInstance.py -n test staging 1.2.3.4
If I want to default the name, I would call:
createInstance.py staging 1.2.3.4
The problem i have is that I dont know how to specify the script arguments string in rundeck. I have a job, with 3 options, one for env, version and name and if I define the arguments string as:
-n ${option.name} ${option.env} ${option.version}
Whenever the name is unset, rundeck calls:
createInstance.py -n staging 1.2.3.4
Instead I would like it to omit the -n. Is there any way of doing this? Right now my only option is to change the script to be more forgiving in how it handles the -n, and to always ensure its at the end, e.g.:
createInstance.py staging 1.2.3.4 -n
createInstance.py staging 1.2.3.4 -n test
I would like to avoid making this change though, as I want to be able to use the scripts standalone as well.
Rather than use a command step, try an inline script step. Your inline script can count the number of arguments and if they are set. Then with that logic you can choose how to set the creteInstance.py args.
As #Alex-SF suggests, I've also used an inline script for this, along with a Key Value Data log filter. The script is:
#!/bin/bash
# Parse optional parameters
# https://stackoverflow.com/questions/41233996/passing-optional-parameters-to-rundeck-script
# Arguments to this script should be in the format "flag" "value", eg "-p" ${option.name}
# If value is not missing then return will be "flag value", otherwise blank
echo -n "RUNDECK:DATA:"
while (( "$#" )); do
flag="$1"
value="$2"
if [[ -z "$value" ]] || [[ $value =~ ^\- ]]; then
# no value for this parameter (empty or picking up the next flag)
echo -n ""
shift
else
# value provided for this parameter
echo -n "$flag $value "
shift
shift
fi
done
And the key value data filter uses the pattern ^RUNDECK:DATA:(.*)$ and the name data args. Then I use ${data.args*} as the input for the real command.
It's all rather messy, and I can't find any open issue requesting this as a feature (yet).
Use an inline script and use conditional variable expansion feature from bash.
createInstance.py ${RD_OPTION_NAME:+-n $RD_OPTION_NAME} $RD_OPTION_ENV $RD_OPTION_VERSION
This will omit the first option altogether if it is empty ("").

How can I ensure my autocompleted spaces are fed into my function properly?

I'm using zsh, and am trying to write a function to operate on a URL and a pathname:
function my-function
{
somecommand --url $1 $(readlink -f $2)
}
(to complicate things somewhat, the function actually uses sh syntax, as it is sourced from my ~/.zshrc using a trick like this). The readlink is there to expand symlinks and ensure directories such as . are evaluated correctly (the directory name is stored for later use by somecommand).
When I type a command from the command-line like this:
my-function http://example.org/example /tmp/myexampledirectory
... it works fine, even if I autocomplete the directory name. However, if the directory name contains spaces, zsh completes it like this:
my-function http://example.org/example /tmp/My\ Example\ Directory
For most "normal" commands (cp, mv, etc.) that never seems to cause a problem. However, in my case, somecommand sees $2 as only being /tmp/My - presumably the rest is seen as another argument.
How can I avoid this situation? I would prefer not to alter the standard zsh autocompletion, but rather find a way for my function to handle this.
The zsh completion system works very well here, and the solution is very simple, just put double-quotes around the readlink argument in the script:
somecommand --url $1 $(readlink -f "$2")
The point is that without quotes readlink removes backslashes which escape whitespaces. Compare three results:
1. Without backslashes and quotes readlink -f assumes that there are three different files/directories (with default path in current directory) and produces
$ readlink -f /tmp/My Example Directory
/tmp/My
/home/jimmij/Example
/home/jimmij/Directory
2. With escaping backslashes but without quotes readlink -f understands that there is only one directory, but removes backslashes from output, so that somecommand takes three separate arguments
$ readlink -f /tmp/My\ Example\ Directory
/tmp/My Example Directory
3. With backslashes and with double-quotes readlink -f gives the output with backslashes what is (most probably) expected by somecommand
$ readlink -f "/tmp/My\ Example\ Directory"
/tmp/My\ Example\ Directory
BTW, as a rule of thumb: if there are any problems with whitespaces in the shell-like scripts (bash, zsh, whatever) the first thing to play with is different quotation marks around variables.

MacVim: create new file from command line by using `alias mvim="open -a macvim"`

When I use vim newfilename to open a file and this file does not exit, vim will create a new file with the name newfilename.
However, MacVim does not work in this way --- i.e. mvim newfilename (alias mvim="open -a macvim") will lead to an error: newfilename does not exist
Is there a way to configure MacVim such that mvim newfilename (alias mvim="open -a macvim") will create a new file and open it?
I'm guessing the error message comes from open, not from vim. You can replace your alias with a function;
mvim () {
local f
for f; do
test -e "$f" || touch "$f"
done
open -a macvim "$#"
}
This will create empty files if necessary before opening them.
edit Didn't see #Peter Lyons' comment about this; credit should go to him for first suggesting this solution. I'll be happy to remove this answer if Peter wants to submit his.
You don't need the mvim alias to the open command, you can instead use the mvim launcher script that comes bundled with most MacVim Snaphots. After adding that mvim to your path, then runing mvim newfile, will now open a newfile buffer in an new MacVim window just like gvim would.
The MacVim mvim script as linked to above:
#!/bin/sh
#
# This shell script passes all its arguments to the binary inside the
# MacVim.app application bundle. If you make links to this script as view,
# gvim, etc., then it will peek at the name used to call it and set options
# appropriately.
#
# Based on a script by Wout Mertens and suggestions from Laurent Bihanic. This
# version is the fault of Benji Fisher, 16 May 2005 (with modifications by Nico
# Weber and Bjorn Winckler, Aug 13 2007).
# First, check "All the Usual Suspects" for the location of the Vim.app bundle.
# You can short-circuit this by setting the VIM_APP_DIR environment variable
# or by un-commenting and editing the following line:
# VIM_APP_DIR=/Applications
if [ -z "$VIM_APP_DIR" ]
then
myDir="`dirname "$0"`"
myAppDir="$myDir/../Applications"
for i in ~/Applications ~/Applications/vim $myDir $myDir/vim $myAppDir $myAppDir/vim /Applications /Applications/vim /Applications/Utilities /Applications/Utilities/vim; do
if [ -x "$i/MacVim.app" ]; then
VIM_APP_DIR="$i"
break
fi
done
fi
if [ -z "$VIM_APP_DIR" ]
then
echo "Sorry, cannot find MacVim.app. Try setting the VIM_APP_DIR environment variable to the directory containing MacVim.app."
exit 1
fi
binary="$VIM_APP_DIR/MacVim.app/Contents/MacOS/Vim"
# Next, peek at the name used to invoke this script, and set options
# accordingly.
name="`basename "$0"`"
gui=
opts=
# GUI mode, implies forking
case "$name" in m*|g*|rm*|rg*) gui=true ;; esac
# Restricted mode
case "$name" in r*) opts="$opts -Z";; esac
# vimdiff, view, and ex mode
case "$name" in
*vimdiff)
opts="$opts -dO"
;;
*view)
opts="$opts -R"
;;
*ex)
opts="$opts -e"
;;
esac
# Last step: fire up vim.
# The program should fork by default when started in GUI mode, but it does
# not; we work around this when this script is invoked as "gvim" or "rgview"
# etc., but not when it is invoked as "vim -g".
if [ "$gui" ]; then
# Note: this isn't perfect, because any error output goes to the
# terminal instead of the console log.
# But if you use open instead, you will need to fully qualify the
# path names for any filenames you specify, which is hard.
exec "$binary" -g $opts ${1:+"$#"}
else
exec "$binary" $opts ${1:+"$#"}
fi