Skip to content

Support for custom aliases and functions #97

Closed

Description

@orschiro

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

Activity

  1. DaveDavenport commented on Dec 12, 2014

    @DaveDavenport
    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.

  2. orschiro commented on Dec 13, 2014

    @orschiro
    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?

  3. DaveDavenport commented on Dec 14, 2014

    @DaveDavenport
    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.

  4. orschiro commented on Dec 15, 2014

    @orschiro
    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?

  5. DaveDavenport commented on Jan 5, 2015

    @DaveDavenport
    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.

  6. orschiro commented on Jan 6, 2015

    @orschiro
    Author

    @DaveDavenport

    Thanks! How can I call it? Tried:

    rofi run-list-command
    

    Returns no error, neither any window.

  7. DaveDavenport commented on Jan 6, 2015

    @DaveDavenport
    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.

  8. orschiro commented on Jan 6, 2015

    @orschiro
    Author

    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
    
  9. DaveDavenport commented on Jan 6, 2015

    @DaveDavenport
    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.

  10. DaveDavenport commented on Jan 6, 2015

    @DaveDavenport
    Collaborator

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

  11. orschiro commented on Jan 7, 2015

    @orschiro
    Author

    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

  12. DaveDavenport commented on Jan 7, 2015

    @DaveDavenport
    Collaborator

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

  13. orschiro commented on Jan 7, 2015

    @orschiro
    Author

    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. 👍

  14. DaveDavenport commented on Jan 7, 2015

    @DaveDavenport
    Collaborator

    happy you got it working!

  15. DaveDavenport commented on Jan 7, 2015

    @DaveDavenport
    Collaborator

    Need to fix the duplicates with history still :D

  16. orschiro commented on Jan 7, 2015

    @orschiro
    Author

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

  17. DaveDavenport commented on Jan 7, 2015

    @DaveDavenport
    Collaborator

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

  18. orschiro commented on Jan 7, 2015

    @orschiro
    Author

    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

  19. added a commit that references this issue on Jan 7, 2015
  20. orschiro commented on Jan 12, 2015

    @orschiro
    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

  21. DaveDavenport commented on Jan 12, 2015

    @DaveDavenport
    Collaborator

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

  22. orschiro commented on Jan 12, 2015

    @orschiro
    Author

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

    You are great. Thank you!

  23. tylerjdurden commented on Apr 10, 2016

    @tylerjdurden

    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.

  24. DaveDavenport commented on Apr 10, 2016

    @DaveDavenport
    Collaborator

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

  25. locked and limited conversation to collaborators on Apr 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions