Skip to content

Instantly share code, notes, and snippets.

@chrisdarroch
Created October 17, 2013 03:40
Show Gist options
  • Select an option

  • Save chrisdarroch/7018927 to your computer and use it in GitHub Desktop.

Select an option

Save chrisdarroch/7018927 to your computer and use it in GitHub Desktop.
Open a project in IntelliJ IDEA from your command line!
#!/bin/sh
# check for where the latest version of IDEA is installed
IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
wd=`pwd`
# were we given a directory?
if [ -d "$1" ]; then
# echo "checking for things in the working dir given"
wd=`ls -1d "$1" | head -n1`
fi
# were we given a file?
if [ -f "$1" ]; then
# echo "opening '$1'"
open -a "$IDEA" "$1"
else
# let's check for stuff in our working directory.
pushd $wd > /dev/null
# does our working dir have an .idea directory?
if [ -d ".idea" ]; then
# echo "opening via the .idea dir"
open -a "$IDEA" .
# is there an IDEA project file?
elif [ -f *.ipr ]; then
# echo "opening via the project file"
open -a "$IDEA" `ls -1d *.ipr | head -n1`
# Is there a pom.xml?
elif [ -f pom.xml ]; then
# echo "importing from pom"
open -a "$IDEA" "pom.xml"
# can't do anything smart; just open IDEA
else
# echo 'cbf'
open "$IDEA"
fi
popd > /dev/null
fi
@liufsd
Copy link
Copy Markdown

liufsd commented Dec 10, 2015

Great work.Thanks for sharing, this is very useful.

Copy link
Copy Markdown

ghost commented Jan 12, 2016

Super useful for Alfred. Thanks for sharing!

@themartorana
Copy link
Copy Markdown

I'll add my thanks to the chorus - works perfectly!

@AfzalivE
Copy link
Copy Markdown

@berkgaut
Copy link
Copy Markdown

Quite useful, thanks for sharing

@GuiSim
Copy link
Copy Markdown

GuiSim commented Oct 12, 2016

Does anyone have a fish function version?

Either way, thanks for sharing this!

@moos
Copy link
Copy Markdown

moos commented Nov 1, 2016

Nice! -- To open new a IntelliJ instance in a virgin folder:

$ mkdir .idea
$ idea .

@mkhb654
Copy link
Copy Markdown

mkhb654 commented Nov 2, 2016

Fantastic work. Thanks guys

@tranquangtiensa
Copy link
Copy Markdown

tranquangtiensa commented Dec 19, 2016

Setup

curl -L "https://gist.githubusercontent.com/chrisdarroch/7018927/raw/9a6d663fd7a52aa76a943fe8a9bc6091ad06b18d/idea" -o /usr/local/bin/idea
chmod +x /usr/local/bin/idea

usage: open terminal and navigate to your project

idea .

Tested on Mac OS X

Note: If you get a “Permission denied” error, your /usr/local/bin directory probably isn’t writable and you’ll need to install script as the superuser. Run sudo -i, then the two commands above, then exit.

@gvrayden
Copy link
Copy Markdown

Thanks, worked well. 👍

@a-yasui
Copy link
Copy Markdown

a-yasui commented Jan 5, 2017

Thanks, Great work 👍

@gitleet
Copy link
Copy Markdown

gitleet commented Jan 24, 2017

Nice!

@SButterfly
Copy link
Copy Markdown

Guys, jetbrains products already support open projects from command line: http://stackoverflow.com/a/9174446/1611015

@zEvg
Copy link
Copy Markdown

zEvg commented Feb 22, 2017

@SButterfly, thanks for pointing that out! 👍

@tgvdinesh
Copy link
Copy Markdown

@tientq Thanks for making it simple.

@DaddyMoe
Copy link
Copy Markdown

Worked like a charm on OS X 10.11.6. Thank you

@fens85
Copy link
Copy Markdown

fens85 commented Mar 27, 2017

thanks! 👍

@luyiisme
Copy link
Copy Markdown

cool!

@namral
Copy link
Copy Markdown

namral commented May 5, 2017

Works great ! Thank you 👍

@Laffs2k5
Copy link
Copy Markdown

Thank you for this!
Inspired by your logic I've created a powershell cmdlet to help windows users do the same:
https://github.com/Laffs2k5/powershell_various/blob/master/Open-DirectoryAsIdeaProject.ps1

@yoqu
Copy link
Copy Markdown

yoqu commented Jun 26, 2017

good job.
Thank you

@mfkenson
Copy link
Copy Markdown

mfkenson commented Jul 6, 2017

nice! thanks!

@zerda
Copy link
Copy Markdown

zerda commented Jan 26, 2018

Alternative way, IntelliJ IDEA -> Tools -> Create Command-line Launcher...

@gufertum
Copy link
Copy Markdown

👍

@andrzejsliwa
Copy link
Copy Markdown

to use it with Jetbrains TOOLBOX you need to modify 4th line:

IDEA=`ls -1d /Applications/JetBrains\ TOOLBOX/apps/IDEA-U/*/*/IntelliJ\ IDEA.app | tail -n1`

@dotCipher
Copy link
Copy Markdown

This is great!

I improved it a bit with some logic that will dynamically pick the either intellij installation found (non-toolbox or toolbox installed) app, check it out here:
https://gist.github.com/dotCipher/9c5f7647bda088fde5dc561cc121b0a5

@vjpr
Copy link
Copy Markdown

vjpr commented Dec 15, 2018

This function doesn't return the latest version.

183.4886.12
183.4886.3 <-- returns this one

I had to sort by modified date using ls -tr.


I use this:

export JETBRAINS_TOOLBOX_ROOT="${HOME}/Library/Application Support/JetBrains/Toolbox/apps"

# product = IDEA-U, CLion, etc.
# bin = idea, clion, etc.
jetbrainsToolboxPath() {
  product=$1
  bin=$2
  # -tr = sort by last update in reverse
  ret=`ls -tr -1d "${JETBRAINS_TOOLBOX_ROOT}"/$product/*/*/*.app/Contents/MacOS/$bin | tail -n1`
   # Escape string because it has spaces.
  echo $(printf '%q' $ret)
}

export LATEST_IDEA_VERSION_CLI=`jetbrainsToolboxPath IDEA-U idea`
export LATEST_CLION_VERSION_CLI=`jetbrainsToolboxPath Clion clion`
export LATEST_APPCODE_VERSION_CLI=`jetbrainsToolboxPath AppCode appcode`

@manisi
Copy link
Copy Markdown

manisi commented Sep 7, 2019

Setup

curl -L "https://gist.githubusercontent.com/chrisdarroch/7018927/raw/9a6d663fd7a52aa76a943fe8a9bc6091ad06b18d/idea" -o /usr/local/bin/idea
chmod +x /usr/local/bin/idea

usage: open terminal and navigate to your project

idea .

Tested on Mac OS X

Note: If you get a “Permission denied” error, your /usr/local/bin directory probably isn’t writable and you’ll need to install script as the superuser. Run sudo -i, then the two commands above, then exit.

Thank you

@kakulukia
Copy link
Copy Markdown

thx!

@Merkur82
Copy link
Copy Markdown

Thx @cris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment