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

Support for custom aliases and functions #97

Closed
orschiro opened this issue Dec 12, 2014 · 23 comments
Closed

Support for custom aliases and functions #97

orschiro opened this issue Dec 12, 2014 · 23 comments

Comments

@orschiro
Copy link

Hi there,

I was almost able to replace dmenu with rofi except for one missing functionality.

In dmenu I can source and run custom aliases and functions. Basically, I created a file ~/.zsh_aliases which contains all my custom aliases and functions. I am using it together with a small script that sources these aliases and functions and allows me to call them from dmenu. The advantage here is that I can also source the exactly same file in my shell rc file.

What do you think about including support for custom aliases and functions into rofi and what would be the best approach to do that?

Thanks for your advice!

Robert

@DaveDavenport
Copy link
Collaborator

Having support for aliases would be nice..
Executing the aliasses in run mode is a matter of tweaking the command that executes.
For bash this works:

    rofi -rnow -run-command "bash -i -c '{cmd}'"

Question is how (in a portable way) does rofi know about available aliasses.

@orschiro
Copy link
Author

Question is how (in a portable way) does rofi know about available aliasses.

What do you mean by portable way? Does rofi provide a config file where the path to the custom aliases file could be defined?

@DaveDavenport
Copy link
Collaborator

I do not know how different shells handle aliases?
Also they might not be in a single file, I f.e. set aliases from multiple files, depending on purpose.

Under bash I can get a list of aliases using: 'alias' command, maybe that is the way to go?
I am not sure if every shell uses the same format.

@orschiro
Copy link
Author

As far as I know, Bash and Zsh use the same format for aliases. And these two shells are the most frequently used ones. Thus, maybe starting to support these ones first with extending it to more shells, such as Fish, in the future?

@DaveDavenport
Copy link
Collaborator

I have added a command 'run-list-command'. This can call a list that can return a list of 'extra' commands to include in the run-dialog.

@orschiro
Copy link
Author

orschiro commented Jan 6, 2015

@DaveDavenport

Thanks! How can I call it? Tried:

rofi run-list-command

Returns no error, neither any window.

@DaveDavenport
Copy link
Collaborator

rofi -run-list-command "path to magic script returning aliases" -rnow

You might also want adjust the -run-command to execute the result using zsh.

@orschiro
Copy link
Author

orschiro commented Jan 6, 2015

I begin to understand. So, given that my aliases are stored in ~/.zsh_aliases, something like the following should almost do the trick:

 rofi -run-list-command "cat ~/.zsh_aliases" -rnow 

~/.zsh_aliases:

~ ❯ cat .zsh_aliases
#
# .zsh_aliases
#

[ -e "${HOME}/.zshenv" ] && source "${HOME}/.zshenv"

# Package management aliases
alias pkg-install='pacaur -Sy --noedit'
alias pkg-install-ruby='pacgem'
pkg-remove() {
    sudo pacman -Rsn "$@"
}

However, it fails finding the alias file:

~ rofi -run-list-command "cat ~/.zsh_aliases" -rnow
cat: ~/.zsh_aliases: No such file or directory

@DaveDavenport
Copy link
Collaborator

No.

I have a script alias.sh that lists all possible commands:

alias | awk -F'[ =]' '{print $2}'

This only lists aliases, but you can add a list of 'functions' too.

Then I execute rofi like this:

rofi -run-list-command ". ~/Scripts/alias.sh" -run-command "/bin/bash -i -c '{cmd}'" -rnow

You need to do the run-command, otherwise rofi will try to execute the command, but in case of an alias this is not possible, the 'shell' should execute it. By using the run command it will call bash, and use bash to execute it.

How the exact line is for zsh I do not know.

@DaveDavenport
Copy link
Collaborator

Note to self: With history possible duplicate entries are possible.

@orschiro
Copy link
Author

orschiro commented Jan 7, 2015

I have a script alias.sh that lists all possible commands:

Strangely this command does not find all my aliases...

Example of such an alias:

~ ❯ cat .zsh_aliases | grep crashplan
alias crashplan-stop='gksudo systemctl stop crashplan'
alias crashplan-restart='gksudo systemctl restart crashplan'
alias crashplan='CrashPlanDesktop'
    gksudo "systemctl stop crashplan.service insync@orschiro.service dropbox@orschiro.service"
    gksudo "systemctl restart crashplan.service insync@orschiro.service dropbox@orschiro.service"

Output of the awk command returns nothing:

~ ❯ alias | awk -F'[ =]' '{print $2}' | grep crashplan
~ ❯ 

The shell, however, is able to find the alias:

screenshot

@DaveDavenport
Copy link
Collaborator

I have no idea about zsh. You do not have to use the awk line I provided. that was just an example.

@orschiro
Copy link
Author

orschiro commented Jan 7, 2015

Many thanks for your dedicated help! Finally found a zsh-compatible solution.

zsh_aliases_functions.sh:

# fetch all zsh aliases 
alias | awk -F'[ =]' '{print $1}'

# fetch all zsh functions
# http://superuser.com/questions/681575/any-way-to-get-list-of-functions-defined-in-zsh-like-alias-command-for-aliases
print -l ${(ok)functions}

Function to call the menu in Openbox' rc.xml:

    <keybind key="A-F2">
      <action name="execute">
        <execute>rofi -run-list-command ". ~/Bin/zsh_aliases_functions.sh" -run-command "/bin/zsh -i -c '{cmd}'" -rnow</execute>
      </action>
    </keybind>

With this extra feature, rofi is close to perfect. 👍

@DaveDavenport
Copy link
Collaborator

happy you got it working!

@DaveDavenport
Copy link
Collaborator

Need to fix the duplicates with history still :D

@orschiro
Copy link
Author

orschiro commented Jan 7, 2015

Can you outline what you mean by possible duplicates? Haven't noticed any.

@DaveDavenport
Copy link
Collaborator

duplicate entries. It might not be there.. but I am not 100% sure, so want to double check .

@orschiro
Copy link
Author

orschiro commented Jan 7, 2015

Alright.

Please let me know if I can do any testing.

Robert

On 07.01.2015 10:12, Dave Davenport wrote:

duplicate entries. It might not be there.. but I am not 100% sure, so
want to double check .


Reply to this email directly or view it on GitHub
#97 (comment).

Sharing my passion & inspiration for

#sustasty lifestyles #transitions
#OpenSource #Sufficiency
#ResourceEfficiency

Flying is old-school and the
world cannot be saved part-time

Co-Creator of @MindfulMeerkats

www.robert.orzanna.de
twitter.com/@orschiro

@orschiro
Copy link
Author

@DaveDavenport

I renamed my alias wifi-restart to wifiRestart but it still shows up. Is this a result of the remove duplicates issue?

Screenshot:

image

@DaveDavenport
Copy link
Collaborator

It is history... do shift-del to remove the old entry.

@orschiro
Copy link
Author

It is history... do shift-del to remove the old entry.

You are great. Thank you!

@tylerjdurden
Copy link

I'm attempting to do this on bash. I've written a script:

tyler:~/scripts > cat print_alias.sh
alias | awk -F'[ =]' '{print $2}'

tyler:~/scripts > . ./print_alias.sh
ls
spot
vim

I'm running rofi in i3wm. My command to open rofi, without the -run-list-command argument, has worked in the past. Now I've changed my command to be:

bindsym $mod+d exec rofi -run-list-command ". ~/scripts/print_alias.sh" -run-commmand "/bin/bash -i -c '{cmd}'" -rnow -font "terminus 24" -show run

When I press mod+d, rofi opens for a split second then crashes.

First, are there any log files I can look at to find the problem or give you more information?

Secondly, let me know if you know the solution offhand.

@DaveDavenport
Copy link
Collaborator

please do not resurrect an very old bug report.
Please make a new bug report with the requested information.

@davatorium davatorium locked and limited conversation to collaborators Apr 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants