You are not logged in.
Pages: 1
Topic closed
Is it possible to run my custom bash aliases and functions with dmenu?
My aliases are placed in ~/.bash_aliases and my functions are in ~/.bash_functions.
I source them in my ~/.bashrc through:
Functions
if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
# Aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Last edited by orschiro (2013-12-27 08:41:40)
Offline
You will have to write a script to pipe them into dmenu and execute the result.
You can use the compgen function to get a list of functions and aliases, using the "-A function" and "-A alias" flags, respectively.
Example :
set -f
result=$(compgen -A function | dmenu)
"$result"
Last edited by Wintervenom (2013-11-20 00:11:15)
Offline
Thanks! But I am not sure if this is how it is supposed to be: http://i.imgur.com/Kq7kvrz.png
I have no completion of commands, nor are my aliases and functions found.
~ $ cat dmenu.sh
set -f
result=$(compgen -A function | dmenu)
"$result"
~ $ ./dmenu.sh | dmenu
./dmenu.sh: line 3: : command not found
~ $ ./dmenu.sh | dmenu_run
cannot grab keyboard
./dmenu.sh: line 3: : command not found
Offline
I found a proposed patch for dmenu_run [1] which adds support for custom aliases.
I added the script with a small modification to the AUR [2]
It looks for aliases in ~/.zsh_aliases and ~/.bash_aliases.
[1] https://groups.google.com/forum/#!topic … vv5i2CPo7A
[2] https://aur.archlinux.org/packages/dmenu-aliases/
Offline
I added the script with a small modification to the AUR
Interesting solution!
At line 16 of dmenu_run_aliases you could replace the two sed commands with one awk command thus:
alias | awk -F '[ =]' '{print $2}'
Every little helps ...
Offline
@ninian
Thanks for your suggestion!
Added to the script: https://github.com/orschiro/pkgbuild/bl … un_aliases
In the future I want to add:
* ignore sudo aliases
* add custom functions
Offline
Great script! Did you ever get it to include bash functions as well?
I gave it a quick try, but my bash scripting skills are a bit limited... it's been a while!
EDIT: scratch that, I found a typo or two This works for me (Bash):
#!/bin/bash
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
cache=$cachedir/dmenu_run
else
cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
fi
cmd=`(
IFS=:
if [ -f ~/.zsh_aliases ]; then
aliases=( ~/.zsh_aliases )
elif [ -f ~/.bash_aliases ]; then
aliases=( ~/.bash_aliases )
fi
if [ ~/.bash_functions ]; then
functions=( ~/.bash_functions )
fi
if stest -dqr -n "$cache" $PATH || stest -fqr -n "$cache" "$aliases" || stest -fqr -n "$cache" "$functions"; then
(
stest -flx $PATH
source $aliases
alias | awk -F '[ =]' '{print $2}'
source $functions
compgen -A function
) | sort -u | tee "$cache" | dmenu "$@"
else
dmenu "$@" < "$cache"
fi
)`
# Perform alias expansion on $cmd
if [ -f ~/.zsh_aliases ]; then
echo -e "source ~/.zsh_aliases \n $cmd" | bash -O expand_aliases &
elif [ -f ~/.bash_aliases ] && [ -f ~/.bash_functions ]; then
echo -e "source ~/.bash_aliases \n source ~/.bash_functions \n $cmd" | bash -O expand_aliases &
fi
Last edited by dennis123123 (2014-04-09 22:18:57)
Offline
@dennis
Thank you! Very nice.
I updated the script to 1.3 with the custom functions support.
Offline
No problem!... sorry, I was about to post an "improved" version, but it breaks support for functions, doh!
...
...
EDIT: finally got it to work as I wanted Sorry, zsh stuff is gone from mine, I don't use it -- feel free to borrow the improvements for your version though!
v1.4 Changes:
- removed unnecessary if statements
- removed unnecessary 'source' command
- added some logic to see if the command is a real program, or an alias/function - this saves spawning bash shells that aren't needed
v1.5 TODO list:
- add some "history" feature so that commonly used commands appear first in the list
#!/bin/bash
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
cache=$cachedir/dmenu_run
else
cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
fi
cmd=`(
IFS=:
if [ -f ~/.bash_aliases ]; then
aliases=( ~/.bash_aliases )
fi
if [ ~/.bash_functions ]; then
functions=( ~/.bash_functions )
fi
if stest -dqr -n "$cache" $PATH || stest -fqr -n "$cache" "$aliases" || stest -fqr -n "$cache" "$functions"; then
(
stest -flx $PATH
source $aliases
alias | awk -F '[ =]' '{print $2}'
compgen -A function
) | sort -u | tee "$cache" | dmenu "$@"
else
dmenu "$@" < "$cache"
fi
)`
if [ -f ~/.bash_aliases ]; then
if [ ! -z $(which $cmd) ]; then
exec $cmd &
else
echo -e "source ~/.bash_aliases \n $cmd" | bash -O expand_aliases &
fi
fi
Last edited by dennis123123 (2014-04-21 10:09:39)
Offline
v1.4b (bugfix version to fix when you have an alias with the same name as an executable e.g. alias libreoffice='libreoffice --nologo'
#!/bin/bash
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
cache=$cachedir/dmenu_run
else
cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
fi
if [ -f ~/.bash_aliases ]; then
aliases=( ~/.bash_aliases )
fi
if [ ~/.bash_functions ]; then
functions=( ~/.bash_functions )
fi
source $aliases
cmd=`(
IFS=:
if stest -dqr -n "$cache" $PATH || stest -fqr -n "$cache" "$aliases" || stest -fqr -n "$cache" "$functions"; then
(
stest -flx $PATH
alias | awk -F '[ =]' '{print $2}'
compgen -A function
) | sort -u | tee "$cache" | dmenu "$@"
else
dmenu "$@" < "$cache"
fi
)`
if [ -f ~/.bash_aliases ]; then
if [ ! -z "$(grep '^alias' $aliases|cut -d'=' -f1|grep $cmd)" ] || [ -z $(which $cmd) ]; then
echo -e "source ~/.bash_aliases \n $cmd" | bash -O expand_aliases &
else
exec $cmd &
fi
fi
Offline
@dennis
Thanks again! I still want zsh support though. I incoporated it along with your modifications in v1.4:
Offline
@dennis
Thanks again! I still want zsh support though. I incoporated it along with your modifications in v1.4:
You're welcome - thanks for the original script to save me some work
It's no problem about zsh, I classed the script as "finished" for my purpose so stripped the stuff that I didnt need... but then I found the bugs
Glad to see you merged in the changes and still kept zsh functionality for you
I will implement a "commonly used items first" feature probably in about 1 week when I can spare some free time... unless you do first
Last edited by dennis123123 (2014-04-22 17:27:39)
Offline
Hi i am using Crunchbang, and i really love the dmenu launcher, so in order to get my aliases to work in there i stumbled upon this topic, but the script does not seem to work for some reason.
I get an error with the stest command. Is it available on any debian distribution? I can't find any info on google as well
Offline
Greg,
I'm glad you found our forums (partially) useful. We do hope information we gather can be useful to the larger open source community. However, we do restrict the scope of issues we can address directly to ones we can know about and contribute to - in other words, problems on arch linux only.
Stest is part of dmenu. I'm pretty sure it is part of dmenu from upstream [1]. So if you do not have it in your dmenu package, I'd gather the debian devs saw reason to remove it. Install dmenu directly from upstream, or contact debian's package maintainer for dmenu. You could also follow up on that google code link above that supplied the inspiration for these scripts.
[1] confirmed: http://tools.suckless.org/dmenu/
mod note: Closing.
EDIT: I see dmenu does not seem to be in the debian repos at all. If your dmenu comes from a crunchbang specific repo, you could check with them. The crunchbang community is (last time I was there) quite active and helpful.
Last edited by Trilby (2014-07-01 11:37:05)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Pages: 1
Topic closed