I know the title is not so interesting because we all know PhpStorm can be launched from the command line just using the “pstorm” command. Like this:
1 |
pstorm |
But I love the word “storm” more than “pstorm”, so I did this in my .zshrc:
1 |
alias storm=pstorm |
How do we open a directory in PhpStorm? Just like the usual:
1 |
storm . |
Ah, that will open the current directory in PS 🙂
But what if the command line tool was not installed properly? What if, while upgrading, the tool broke? Or if like me, you install EAPs all the time to try out new features and the command line tool can’t find the EAP version’s path? Well, in this blog post, I will share the python script that is actually installed as the “pstorm” tool. I will also tell you what to modify in case it can’t find your PhpStorm. And of course, we will see how to install it.
So first, the python script. If it’s installed on your system, you can view the content:
1 |
cat `which pstorm` |
Or to edit it with your preferred text editor (in my case vim):
1 |
vim `which pstorm` |
If you don’t have the script installed, here’s mine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#!/usr/bin/python import socket import struct import sys import os import time # see com.intellij.idea.SocketLock for the server side of this interface RUN_PATH = '/Applications/PhpStorm EAP.app' CONFIG_PATH = '/Users/masnun/Library/Preferences/WebIde70' args = [] skip_next = False for i, arg in enumerate(sys.argv[1:]): if arg == '-h' or arg == '-?' or arg == '--help': print(('Usage:\n' + \ ' {0} -h |-? | --help\n' + \ ' {0} [-l|--line line] file[:line]\n' + \ ' {0} diff file1 file2').format(sys.argv[0])) exit(0) elif arg == 'diff' and i == 0: args.append(arg) elif arg == '-l' or arg == '--line': args.append(arg) skip_next = True elif skip_next: args.append(arg) skip_next = False else: if ':' in arg: filepath, line_number = arg.rsplit(':', 1) if line_number.isdigit(): args.append('-l') args.append(line_number) args.append(os.path.abspath(filepath)) else: args.append(os.path.abspath(arg)) else: args.append(os.path.abspath(arg)) def launch_with_port(port): found = False s = socket.socket() s.settimeout(0.3) try: s.connect(('127.0.0.1', port)) except: return False while True: try: path_len = struct.unpack(">h", s.recv(2))[0] path = s.recv(path_len) path = os.path.abspath(path) if os.path.abspath(path) == os.path.abspath(CONFIG_PATH): found = True break except: break if found: if args: cmd = "activate " + "\0".join(args) encoded = struct.pack(">h", len(cmd)) + cmd s.send(encoded) time.sleep(0.5) # don't close socket immediately return True return False port = -1 try: f = open(os.path.join(CONFIG_PATH, 'port')) port = int(f.read()) except Exception: type, value, traceback = sys.exc_info() print(value) port = -1 if port == -1: # SocketLock actually allows up to 50 ports, but the checking takes too long for port in range(6942, 6942+10): if launch_with_port(port): exit() else: if launch_with_port(port): exit() if sys.platform == "darwin": # Mac OS: RUN_PATH is *.app path if len(args): args.insert(0, "--args") os.execvp("open", ["-a", RUN_PATH] + args) else: # unix common bin_dir, bin_file = os.path.split(RUN_PATH) os.chdir(bin_dir) os.execv(bin_file, [bin_file] + args) |
If you know Python, feel free to read and understand what’s actually doing. Now, this script needs to know two things:
— The path to PhpStorm directory (where the executable is)
— The preferences directory
You should be able to find the first trying to locate where PhpStorm is installed. For the second, it’s actually platform dependent. On OS X, it’s inside the “Home” > “Library” > “Preferences” > “WebIde**” directory.
The preference directories are named with a zero appended to the major version of PhpStorm. So, for PS 7.x it’s WebIde70. For 6.x, it’s WebIde60.
Just configure the two variables in the script. Then copy it to your path, in my case, I have “~/.bin” added to my PATH. So I did this:
1 2 |
cp pstorm ~/.bin/pstorm chmod a+x ~/.bin/pstorm |
The last line makes it executable. Now open a new terminal and try the “pstorm” command.
2 replies on “Using PhpStorm from Command Line”
Is there a way to open a file or directory in a new window in an already open PhpStorm?
Yes, from file menu, open a new directory or file, it would ask you for options.