You are not logged in.

#1 2013-11-19 11:13:09

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

[solved] run dmenu with custom aliases and functions

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

#2 2013-11-19 23:53:47

Wintervenom
Member
Registered: 2008-08-20
Posts: 1,011

Re: [solved] run dmenu with custom aliases and functions

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

#3 2013-11-20 07:11:19

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] run dmenu with custom aliases and functions

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

#4 2013-12-27 08:40:55

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] run dmenu with custom aliases and functions

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

#5 2014-01-02 17:40:35

ninian
Member
From: United Kingdom
Registered: 2008-02-24
Posts: 726
Website

Re: [solved] run dmenu with custom aliases and functions

orschiro wrote:

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

#6 2014-01-04 12:46:03

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] run dmenu with custom aliases and functions

@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

#7 2014-04-09 22:07:23

dennis123123
Member
Registered: 2009-07-02
Posts: 72

Re: [solved] run dmenu with custom aliases and functions

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 big_smile 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

#8 2014-04-21 07:13:04

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] run dmenu with custom aliases and functions

@dennis

Thank you! Very nice.

I updated the script to 1.3 with the custom functions support.

https://aur.archlinux.org/packages/dmenu-aliases/

Offline

#9 2014-04-21 09:24:15

dennis123123
Member
Registered: 2009-07-02
Posts: 72

Re: [solved] run dmenu with custom aliases and functions

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 smile 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

#10 2014-04-21 17:30:48

dennis123123
Member
Registered: 2009-07-02
Posts: 72

Re: [solved] run dmenu with custom aliases and functions

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

#11 2014-04-22 05:42:04

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [solved] run dmenu with custom aliases and functions

@dennis

Thanks again! I still want zsh support though. I incoporated it along with your modifications in v1.4:

https://github.com/orschiro/pkgbuild/co … 8cdf036e67

Offline

#12 2014-04-22 17:27:23

dennis123123
Member
Registered: 2009-07-02
Posts: 72

Re: [solved] run dmenu with custom aliases and functions

orschiro wrote:

@dennis

Thanks again! I still want zsh support though. I incoporated it along with your modifications in v1.4:

https://github.com/orschiro/pkgbuild/co … 8cdf036e67

You're welcome - thanks for the original script to save me some work smile

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 big_smile
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 wink

Last edited by dennis123123 (2014-04-22 17:27:39)

Offline

#13 2014-07-01 11:24:14

gregkwaste
Member
Registered: 2014-07-01
Posts: 8

Re: [solved] run dmenu with custom aliases and functions

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 sad

Offline

#14 2014-07-01 11:35:38

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,341
Website

Re: [solved] run dmenu with custom aliases and functions

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

Board footer

Powered by FluxBB