I am using bash autocompletion for pake
_pake()
{
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(/www/grepo/vendor/bin/pake tasklist)" -- $cur) )
}
complete -F _pake pake
That works fine for all pake tasks, but how do I now add completion for my file system in addition to the task list?
Got a reply to that question as a message. The solution is quite easy, you can add a fallback behaviour to complete
complete -o default -F _pake pake
Given that, the auto complete behaviour falls bask to shell default if the custom completion does not deliver results
Related
I have a custom cli tool that i want to setup with bash_completion but want [tab][tab] to perform an enter action on cmdline.
my bash_completion file for wonder is:
_wonder()
{
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "audit nodes tools create debug delete update" -- $cur) )
}
complete -F _wonder wonder
currently:
$ wonder [tab] [tab]
returns:
ip-10-99-18-249:loco_dsl jasonparmar$ wonder
audit create debug delete nodes tools update
What I want is when i use the tools option from wonder:
$ wonder tools [tab] [tab]
I want the [tab][tab] to force an enter on the cmdline
How can i edit my bash_completion file for wonder to achieve this.
Is this even possible with bash_completion.
Thanks in advance.
If you don't mind using external tools to simulate keyboard input (such as xdotool), try adding this as the first line of your completion function:
(( COMP_CWORD > 1 )) && xdotool key Return
As noted by #RandomUser, completion executing a command is unexpected behavior. See, for instance: echo oops; wonder audit <tab><tab> and imagine rm -rf * instead of echo. Consider simply stopping the completion to indicate wonder accepts only one argument:
(( COMP_CWORD > 1 )) && return
after hours of search I ended up writing my first stackoverflow question.
I want to create a completion function for a bash script, so far so good ;).
This bash script calls other executables that have their own autocompletion.
Example:
$ my_script foo par
# calls /usr/local/libexec/my_script/foo par
Autocompleting the first parameter of my_script (in this case "foo") works, because the possible options are the files in the folder "/usr/local/libexec/my_script/".
Each program in this folder does have a working auto completion, which was a byproduct of using boost::program_options.
I now want to implement the auto completion for the next parameters of my_script by referencing to the auto completion of the program gooing to be called.
$ my_script foo <tab>
# should output possible options to the foo subcommand
# like /usr/local/libexec/my_script/foo <tab>
I've started by this answer Bash completion from another completion, but _command or _command_offset 1 does not seem to work for me.
How can I get the options of foo, and how can I use this in my_script?
My current /etc/bash_completion.d/my_script looks like the following
_my_script()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ "$COMP_CWORD" == 1 ]]; then
# 1. param: for program to be loaded
for i in $( ls /usr/local/libexec/my_script/ ); do
opts="${opts} ${i} "
done
COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
else
# next param: of the program to be loaded
# how do I get the options of "foo" here?
fi
return 0
}
complete -F _my_script my_script
As soon as I read your question, the completion of sudo and git came to my mind. They both have the similar behavior you desired. So I looked for their completion functions. Here are your missing lines:
local root_command=${COMP_WORDS[0]}
_command_offset 1
It's copied from /usr/share/bash-completion/completions/sudo in Ubuntu 16.04. I totally don't knows its meaning. But it works.
I would like to setup inotifywait so that it monitors a folder and when something is copied to this folder (lsyncd which uses rsync) I would like inotifywait to sit tight and wait until rsync is done before calling a script to process the new folder.
I have been researching online to see if someone is doing this but I am not finding much.
I'm not the most well versed with bash scripting though I understand some basics.
Here is a little script I found that pauses for a second but it still triggers a dozen events per transfer:
EVENTS="CLOSE_WRITE,MOVED_TO"
if [ -z "$1" ]; then
echo "Usage: $0 cmd ..."
exit -1;
fi
inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
WAITING="";
while true; do
LINE="";
read -t 1 LINE;
if test -z "$LINE"; then
if test ! -z "$WAITING"; then
echo "CHANGE";
WAITING="";
fi;
else
WAITING=1;
fi;
done) | (
while true; do
read TMP;
echo $#
$#
done
)
I would be happy to provide more details or information.
Thank you.
Depending on what action you want to take, you may want to take a look at the tools provided by Watchman.
There are two that might be most useful to you:
If you want to initiate some action after the files are synced up, you may want to try using watchman-make. This is most appropriate if the action is to run a tool like make where the tool itself will look over the tree and produce its output (in other words: where you don't need to pass the precise list of changed files directly to your tool). You can have it run some other tool instead of make. There is a --settle option that you can use to have it wait a few moments after the latest file change notification before executing your tool.
watchman-make --make='process-folder.sh' -p '**/*.*'
watchman-wait is more closely related to inotifywait. It also waits for changes to settle before reporting files as changed, but because this tool doesn't coalesce multiple different file changes into a single event, the settle period is configured as a property of the tree being watched rather than as a command line parameter
Disclaimer: I'm the creator of Watchman
I'm looking for a way to hook in a custom bash completion function. Problem is, I want this completion function not just for a specific command, but for all commands.
Is this even possible? Having looked around for a while, I couldn't find any resources online.
To reduce the problem to the most trivial case: would it be possible to always have tab-completion for the string 'foo'?
Meaning echo f<tab> would expand into echo foo, and ls fo<tab> would expand into ls foo
For context: I'm trying to implement something similar to http://blog.plenz.com/2012-01/zsh-complete-words-from-tmux-pane.html in bash, but I'm starting to fear it's not possible.
You can do that with the -D option of the complete command:
suggest_hello()
{
COMPREPLY=( hello )
return 0
}
complete -D -F suggest_hello
Now whenever I type echo h<Tab>, I get echo hello.
$ help complete
complete: ...
...
-D apply the completions and actions as the default for commands
without any specific completion defined
...
I'm trying to write a small command launcher application, and would like to use bash's tab completions in my own completion system. I've been able to get a list of completions for general commands using compgen -abck.
However, I would also like to get completions for specific commands: for instance, the input git p should display completion for git's commands.
Is there any way I can use compgen to do this? If not, are there any other ways I can get a list of completions programmatically?
[EDIT: To clarify, I'm not trying to provide completion to bash - my app is a GUI command launcher. I'd simply like to use bash's existing completions in my own app.]
I don't really know how it works, but the awesome window manager uses the following Lua code for getting access to bash completion's result:
https://github.com/awesomeWM/awesome/blob/master/lib/awful/completion.lua#L119
Via complete -p we find complete -o bashdefault -o default -o nospace -F _git git. We remember "_git" for later.
The length of "git l" is 5, so we set COMP_COUNT=6. We are completing the first argument to "git", so COMP_CWORD=1.
All together we use the following script:
__print_completions() {
printf '%s\n' "${COMPREPLY[#]}"
}
# load bash-completion functions
source /etc/bash_completion
# load git's completion function
_completion_loader git
COMP_WORDS=(git l)
COMP_LINE='git l'
COMP_POINT=6
COMP_CWORD=1
_git
__print_completions
Output: "log"
Check in the /etc/bash_completion.d/ directory. This is where the different command completion scripts stay.
Quite an old question, but in the mean time I've implemented a script that handles this to reuse completions with ZSH
Here a simple but working example in bash :
function foo_completion()
{
local currentWord=${COMP_WORDS[COMP_CWORD]}
local completionList=""
case "${COMP_CWORD}" in
"1")
completionList="command1 command2 command3";
;;
"2")
completionList="param1 param2 param3";
;;
esac
COMPREPLY=( $( compgen -W "${completionList}" -- ${currentWord} ) )
}
complete -F foo_completion foo
With this kind of code, you will get commandN completed when you type "foo c" + tab and paramN completed when you type "foo command1 p" + tab
You can compute the completion list from the command help.
my2c