Even though I use PyCharm, I still drop into Vim occasionally to edit configuration files. This was an annoying process until today, when I discovered that PyCharm and other Intellij editors can open the current file in an external tool. This works with both GUI and console-based applications, and its most trivial use case seems to be opening the current file in an external editor.
So, how does it work? Easy — this should only take a minute or so! Instructions are geared toward OS X users, but should be clear to users on other operating systems.
Step 1: Set up an external tools entry for your editor
- Open PyCharm -> Preferences
- Click on or search for “External Tools”
- Click the “+” button
- Give your editor a name, like MacVim
- Check “Synchronize files after execution” to sync edits made between the editors while the file is open in both
- Uncheck “Open console” if the editor is a GUI application
- Set the program path — for GUI OS X applications, this is the application bundle, e.g. /Applications/MacVim.app
- For “Parameters” enter “$FilePath$”, an Intellij macro that will automatically send the absolute path of the current file to the external editor
- For “Working directory” enter “$ProjectFileDir$”, an Intellij macro that will automatically set the working directory to the directory containing your Intellij project file; this is my preference because in PyCharm the project file directory is usually the directory you opened the first time you opened your code in PyCharm, and for me, that is my git checkout (see “Insert macro” for other directories you can use)
- Click OK
Step 2: Assign a keymap to the editor
One of my favorite things about this feature is that PyCharm lets you assign a key binding to each tool, so now that you have a “MacVim” tool in the menu tree, you can assign it a key binding.
- Open PyCharm -> Preferences
- Click on or search for “Keymap”
- Select your keymap
- Search for “MacVim” or whatever editor name you gave the external tool
- Double click on it and then click “Add keyboard shortcut”
- Set whatever shortcut you like; Control+Shift+E worked for me
- Click OK
Step 3: Try it out
All right! Now the moment of truth. Open a file in PyCharm and use your keybinding, or navigate to the Tools -> Editors -> MacVim (for example) menu item. This should open MacVim with the file you chose, and better yet, the working directory will be set to the project root (at least in my setup, where all my PyCharm project files live in my project root directories).
Hope this was helpful! It certainly made my day. (Now if only I could change the detected file type of an open file within PyCharm, like I can with every other text editor on the planet.)