Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RFC: autocomplete newest file in directory #637

compholio started this conversation in Ideas
Discussion options

Some time ago I put together a modification to _filedir to allow autocomplete to return the latest file in a directory. I've found this to be incredibly useful, so I've put together a fork with these changes:
https://github.com/compholio/bash-completion
Examples:

mv ?<TAB> -> mv latest-file.pdf
mv ~/Downloads/?<TAB> -> mv ~/Downloads/latest-file.pdf

I'd be curious to see how others feel about this and whether people think this feature (or a version of it) is worth including in bash-completion.

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

I don't remember missing such a feature myself. It can surely be useful in some use cases and workflows, but then again so could be completing with the oldest/smallest/largest/random/first-sorted/last-sorted one. And offhand I think more often than not we should respect the globs for completing these special cases so that e.g. for a PDF viewer one would get the newest/oldest/... PDF. I guess I find the idea interesting, but there are so many variables at play that I'm not sure an easy enough to use implementation exists.

You must be logged in to vote
2 replies
Comment options

Is there an easy way to communicate a different keystroke to bash-autocomplete? It could make sense to bind a keystroke (similar to menu-complete) and then we could know to complete something like mv ~/Downloads/*.pdf<SHIFT-TAB> to the latest pdf. It appears to me that complete and menu-complete are bash builtins and that there's not a way to define a "latest-complete" and have it call the regular complete.

I ask because the closest I've been able to get to calling bash-completion from a keybinding is something that looks like this (and it's not perfect):

bind -x '"\e[Z":latest-complete'
latest-complete() {
 local COMP_LINE COMP_WORDS COMP_POINT COMP_CWORD routine COMPREPLY=()
 COMP_LINE="${READLINE_LINE}"
 COMP_WORDS=(${COMP_LINE})
 COMP_POINT=${READLINE_POINT}
 COMP_CWORD=$((${#COMP_WORDS[@]}-1)) # this needs to find the current word based on the COMP_POINT
 routine=$(complete -p "${COMP_WORDS[0]}" | cut -d' ' -f3)
 "${routine}"
 COMP_WORDS[${COMP_CWORD}]=$(printf '%q\n' "${COMPREPLY[@]}" | sed 's/\\~/~/')
 READLINE_LINE=$(echo "${COMP_WORDS[*]// /}")
 READLINE_POINT=${#READLINE_LINE}
}
Comment options

I think that I've found another way to approach this that might be more reasonable. New bash binding:

bind -x '"\C-t1":COMP_LATEST=1'
bind '"\C-t2":complete'
bind -x '"\C-t3":unset COMP_LATEST'
bind '"\e[Z":"\C-t1\C-t2\C-t3"'

Then inside _filedir we can use COMP_LATEST to split the path and override xspec such that, for example, ls ~/Downloads/*.pdf<SHIFT-TAB> will return the latest PDF. How does that sound?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /