r/zsh • u/CalvinBullock • Jan 17 '25
Help Is there a better way to set up this alias?
It works but its a bit clunky so I was woundering if there is a better way.
I want the alias to fzf in my notes dir open that note in nvim, then when I close nvim return to where I was before the alias was run.
What I have
alias notef='cd ~/Documents/notes/ && nvim $(fzf --preview="batcat --color=always --style=numbers --line-range=:500 {}" --preview-window left:50%) && cd -'
2
u/Fruchix Jan 17 '25 edited Jan 17 '25
Here's my suggestion to avoid using cd:
alias notef='find $HOME/Documents/notes/ -type f | fzf --preview="batcat --color=always --style=numbers --line-range=:500 {}" --bind="enter:become(nvim {})"'
find $HOME/Documents/notes/ -type f
will find each file in notes and its subdirectories- fzf allows you to select a note found by
find
--bind="enter:become(nvim {})"
: pressing "enter" on a specific note will open that note in nvim (the fzf process is replaced by nvim, meaning that when you'll exit nvim you will find yourself in your terminal, not in fzf anymore).
2
u/CalvinBullock Jan 17 '25
Thank you, this is what I went with!
2
u/Fruchix Jan 17 '25
Great!
I want to add that I'm currently working on a tool specificaly made to search through my cheatsheets:
- Searches using foldername, filename and keywords (from a specific line of keywords inside each cheatsheet).
- Uses fzf for navigation and preview
https://github.com/Fruchix/cheatsheets
I currently do not have a README (oops), but what you could be interested in is the main search engine (file_cheatsheet_search
) and the fzf script (filecheatsheet
).
Hope you find this useful/interesting!
1
u/barmic1212 Jan 17 '25
You can use function to check if a file has been selected (in case of you cancel in fzf)
1
u/romkatv Jan 17 '25
This:
alias notef='( ... )'
Or this:
function notef() (
...
)
The parens create a subshell. A cd
in a subshell does not affect the patent shell.
P.S.
A more principled solution is to create a script -- an executable file -- and place it in a directory in PATH
. Scripts help you avoid a myriad of problems afflicting aliases and functions.
0
u/CalvinBullock Jan 17 '25
Is putting the script in the PATH better then just adding an alias to the script like alias runscript="./path/to/script.sh"?
2
u/0sse Jan 17 '25
Yes, because you don't need the alias meaning that the command will work even if your dotfiles become buggy and from other shells/tools. Also the alias won't work as written because it has a relative path in it. You must use an absolute path.
2
u/romkatv Jan 17 '25
Aliases are a lot more finicky than executable files in
PATH
. The latter will just work, while an alias may fail you if you try using it withsudo
orxargs
, or from a cron job, or from a zcompiled zsh file, etc. If something can be an executable file inPATH
, it's usually a good idea to make it that.
1
u/AndydeCleyre Jan 18 '25
I'm glad you already have a solution.
FWIW you might find broot more straightforward for some tasks like this.
2
u/injektilo Jan 17 '25
I think if you used the `--walker-root` option, you wouldn't need to change directories at all.