Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aliasrc | improve se() #1433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

aliasrc | improve se() #1433

wants to merge 2 commits into from

Conversation

emrakyz
Copy link
Contributor

@emrakyz emrakyz commented Sep 2, 2024

  1. Remove external commands like find.
  2. Remove extensions (possibly .sh) and path (/home/username/.local/bin/) from the names in fzf.
  3. Only open the editor if there is a selection.

Do all of these without using find, sed, grep.

  • First line creates an array with the files in the scripts directory.

  • Second line removes path :t and the extensions :r from the scripts.

  • [[ "${c}" ]] checks if this variable is non-empty.

  • ${${(M)s:#*/${c}*}[1]} This works like grep

(M) enables "match" mode.
:# anchors the pattern to the start of each array element.
*/${c}* matches any path containing the selected basename.
[1] selects the first matching item.

1. Remove external commands like find.
2. Remove extensions and path (if present) from the names in fzf.
3. Only open Nvim if there is a selection.

Do all of these without using find, sed, grep.

- First line creates an array with the files in the scripts directory.

- Second line removes path (:t) and the extensions (:r) from the scripts.

- [[ "${c}" ]] checks if this variable is non-empty.

- ${${(M)s:#*/${c}*}[1]}
(M) enables "match" mode.
:# anchors the pattern to the start of each array element.
*/${c}* matches any path containing the selected basename.
[1] selects the first matching item.
@2084x
Copy link
Contributor

2084x commented Sep 7, 2024

doesn't pick up scripts in subdirectories eg. statusbar/

@emrakyz
Copy link
Contributor Author

emrakyz commented Sep 7, 2024

@2084x

We can use a more capturing glob pattern then that is supported by Zsh.

s=("${HOME}/.local/bin/"**/*(.))

This would capture sub-directories' contents too.

}
s=("${HOME}/.local/bin/"**/*(.))
c="$(print -lnr ${s:t:r} | fzf)"
[[ "${c}" ]] && "${EDITOR}" ${${(M)s:#*/${c}*}[1]}
Copy link

@RetroViking RetroViking Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[ "${c}" ]] && "${EDITOR}" ${${(M)s:#*/${c}*}[1]}
[ -n "${c}" ] && "${EDITOR}" ${${(M)s:#*/${c}*}[1]}

Prevents that error when you press esc instead of selecting something.
This is a neat showcase of zsh features, but it might break portability.

~/.config/shell/aliasrc – a separate file called by the zshrc for loading aliases that might be shared in common with bash or other shells.
source

I tried this tweak and still use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants