Ubuntu – Unity command runner

unity

I'm searching for command runner for Unity. I known that there is default runner under shortcut ALT + F2. But as it was explained in that question Run command don't run command line programs it don't allow to run command lines programs without adding some prefixes to command.

In short I'm searching for command runner which:

  • has auto completion
  • has history of last used commands
  • allow to run both commands vim test.txt or gedit test.txt without any prefixes, suffixes etc.
  • if command line program was run, terminal window should be closed after exiting program

From different command runners I check gmrun, but it don't have auto completion or history. In other hand xfrun4 as it is described in Xfce4 Alt F2 – xfrun4 command impotent in 14.04 Trusty require some “!” prefix to work.

Do you known some command runner which could be integrated with Unity and meet that requirements?

Best Answer

Create your own Command Runner with the gnome-terminal:

First create a file $HOME/.cmdrunrc

The content should be:

# include .bashrc to load aliases from the .bashrc in the command runner
source .bashrc

# create a separated history file for the command runner
HISTFILE=~/.cmdrun_history

# shorten the command prompt
PS1='\w\$ '

# function to check an array if it contains an element
containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

# your guis which will not open a additional terminal window on execution 
guis=(firefox mousepad gedit geany leafpad nautilus thunar pcmanfm xterm) 

# after this commands finished the terminal will be closed automatically  
close_after=(vi nano top)

# the function that will be executed with every command
custom_run() { 
    if [ -n "$*" ]; then   
        # write the real command to the history without "custom_run" 
        history -s "$*"
        history -a

        # check if the command is an alias
        pure_cmd=$(echo "$*" | xargs | cut -d' ' -f1)
        if [ $(type -t "$pure_cmd") == "alias" ]; then
            # get the real command from the alias
            pure_cmd=$(type "$pure_cmd" | grep -oP "(?<=»)[^']+(?=«)")
            pure_cmd=$(echo "$pure_cmd" | cut -d' ' -f1)
        fi

        # check if command opens a gui or should be closed after it finished
        if containsElement "$pure_cmd" "${guis[@]}"; then
            bash -ic "($* &) &>/dev/null"
        elif containsElement "$pure_cmd" "${close_after[@]}"; then
            gnome-terminal -x bash -ic "$*"
        else
            gnome-terminal -x bash -ic "$*; bash"
        fi
    else
        # remove last command ("custom run") from history, if nothing is entered
        history -d $((HISTCMD-1))
    fi
    exit 0
}

# write the function "custom_run" before every command if the user presses return
bind 'RETURN: "\e[1~ custom_run "\e[4~"\n"'

Then create a .desktop file or a shell script with this command:

gnome-terminal --title="Command Runner" --hide-menubar --geometry="50x1" -x bash -ic "cd ~; bash --rcfile .cmdrunrc"