3

How can we create an alias or shortcut command to run a programme in Ubuntu, I have to start multiple sessions of intelliJ and eclipse, every time I restart my computer I have to got to each directory and start the eclipse from there. I want to have a single command that can be run from terminal from any directory to start these programmes.

  • When You execute IntelliJ You'll have an icon of app in sidebar. Just click and hold that icon and move up, it will pin it as an app for future use. – num8er Nov 10 '17 at 1:21
  • @num8er Thank you for your simple solution, it works only problem is when I exit AndroidStudio or IntelliJ for that matter, I get error "Studio was unable to save some project files,are you sure you want to close this project anyway?Read-only files:/home/xyz/.idea/workspace.xml. Please note that I created this project with root permission before following your suggestion. – Navneet Rai Nov 10 '17 at 6:12
6

The simplest way would be, to start a script that does that for you, I think.

Something like this (the 1st line is necessary, it's not a comment):

#!/bin/bash
echo "Running 1st app..."
$HOME/path_to_1st/app &     # this runs an app from user's home directory
                            # the ampersand makes the app `fork`
                            # it means it'll start in the background
                            # and the script will continue to execute
echo "Running eclipse..."
cd directory/project1/
eclipse -param1 -param2 path/to/something &

echo "Running 2nd app..."
cd directory/project2
other_app_executable &

The $HOME variable represents the user's home directory. Mind it's the user who runs the script, not necessarily you. Save above as startmyapps.sh and change permissions to make it executable:

chmod u+x startmyapps.sh

The u+x means:

  • u - user
  • + - add permission
  • x - executable

To run startmyapps.sh in terminal from any working directory, you can either make an alias pointing to its absolute path (the /home/username/path/to/startmyapps.sh - whole thing; on aliases see the other responses) or you need to put it in $PATH. The most common way would be, to save it in your $HOME/bin/ directory and then add this line at the end of your $HOME/.bashrc (or .zshrc or whatever, depending on the shell you use, AFAIK bash is default in Ubuntu):

export PATH=$PATH:$HOME/bin

meaning:

  • export - make variable available for children of this terminal
  • PATH - work on variable named PATH
  • = - assign it a new value, which is
  • $PATH - the old value
  • : - a separator
  • $HOME/bin - and your newly created bin directory

Extras

As a side note I want to mention its common to work in virtual environments as a way to separate projects and freeze dependencies, e.g. python's way of creating one would be to call python -m venv new_env_name. Usually this environments come with extensions like workon that can activate a given environment for you wherever you are in the directory tree (as in: you don't have to be in your project's directory, just call workon new_env_name). All those extensions do is calling a starting script from new_env_name/bin/activate - so this is where you'd add necessary calls to start your applications.

  • 1
    Might want to background all those executables though. – Ignacio Vazquez-Abrams Nov 9 '17 at 16:51
  • Good catch, edited. – cprn Nov 9 '17 at 16:56
  • @Archemar Yeah... Also I never knew shebang can't contain comment, never used one before. But now I copy&pasted this commented version and tried to run it, it didn't. Good catch, fixed. – cprn Nov 9 '17 at 23:13
  • 1
    Thank you it is the complete solution i was looking for, works like charm. I just want to add one more info that I came across while following this solution, for new users that $HOME points to the path of home directory of the current user running this startmyapps.sh script – Navneet Rai Nov 10 '17 at 6:37
  • I'll put that in. – cprn Nov 10 '17 at 15:43
4
  1. To add a custom keyboard board shortcut open System Settings and select Keyboard -> Shortcuts tab -> Custom Shortcuts.

  2. Open any application, for example Eclipse, and check in the System Monitor app to find the name of the command to start that application from the terminal. For example, the command to start Eclipse is eclipse.

  3. Click the + button in the lower left corner of the Shortcuts pane to add a new keyboard shortcut.

  4. A new little Custom Shortcut window will open up. After where it says Name: type Eclipse. After where it says Command: type eclipse with a lowercase e. Click the Apply button to apply the new keyboard shortcut.

    enter image description here

    The command eclipse can also be customized to open eclipse with custom command line arguments.

  5. Click the Eclipse shortcut that you added to the list of custom shortcuts where it says Disabled, which will make New accelerator... appear after where it says Eclipse instead of Disabled. Press any keyboard shortcut combination to assign it to Eclipse.

    enter image description here

  6. In order to undo an existing keyboard shortcut, click the existing keyboard shortcut in the list of shortcuts and undo it using the Backspace key.

2

You can use the alias command in your .bashrc for simplifying commands (example):

alias toclip='xclip -sel clip'

which sens piped output to clipboard using xclip. So for comming closer to your question this could be:

alias startup='eclipse /path/to/project'

Or if a single line is not sufficient enough to start all you need you can define a function in your .bashrc (example):

function extract {
 if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
 else
    if [ -f $1 ] ; then
        # NAME=${1%.*}
        # mkdir $NAME && cd $NAME
        case $1 in
          *.tar.bz2)   tar xvjf ../$1    ;;
          *.tar.gz)    tar xvzf ../$1    ;;
          *.tar.xz)    tar xvJf ../$1    ;;
          *.lzma)      unlzma ../$1      ;;
          *.bz2)       bunzip2 ../$1     ;;
          *.rar)       unrar x -ad ../$1 ;;
          *.gz)        gunzip ../$1      ;;
          *.tar)       tar xvf ../$1     ;;
          *.tbz2)      tar xvjf ../$1    ;;
          *.tgz)       tar xvzf ../$1    ;;
          *.zip)       unzip ../$1       ;;
          *.Z)         uncompress ../$1  ;;
          *.7z)        7z x ../$1        ;;
          *.xz)        unxz ../$1        ;;
          *.exe)       cabextract ../$1  ;;
          *)           echo "extract: '$1' - unknown archive method" ;;
        esac
    else
        echo "$1 - file does not exist"
    fi
fi
}

This defines for example a function extract which takes any kind of filename and tries to extract it. so to keep it more inline with what you want a small example how it could look like:

function startup {
    eclipse /path/to/a/AFile
    eclipse /path/to/a/BFile
    eclipse /path/to/a/CFile
    eclipse /path/to/a/DFile
}

The good of this approach is that you not have to worry about path variables and so on, you get them this way simply when you start your terminal. The above ones are just examples, you can put whatever you want into your alias or your function as long it is bash compliant.

  • Also a perfectly valid approach. – cprn Nov 9 '17 at 16:57
  • 1
    As always there are many ways leading to rome :) – Videonauth Nov 9 '17 at 17:01
  • 1
    I'd have suggested an alias like alias eclipse /path/to/eclipse, as that appears to be more in line with the original question. – TMN Nov 9 '17 at 18:58
0

You can use this command to create shortcut (short link), so simply:

ln -s /path/to/file /path/to/symlink

Goodluck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.