8 min read

In this article by Nafiul Islam, the author of Mastering Pycharm, we’ll learn all about navigation. It is divided into three parts. The first part is called Omni, which deals with getting to anywhere from any place. The second is called Macro, which deals with navigating to places of significance. The third and final part is about moving within a file and it is called Micro.

By the end of this article, you should be able to navigate freely and quickly within PyCharm, and use the right tool for the job to do so.

Veteran PyCharm users may not find their favorite navigation tool mentioned or explained. This is because the methods of navigation described throughout this article will lead readers to discover their own tools that they prefer over others.

(For more resources related to this topic, see here.)

Omni

In this section, we will discuss the tools that PyCharm provides for a user to go from anywhere to any place. You could be in your project directory one second, the next, you could be inside the Python standard library or a class in your file. These tools are generally slow or at least slower than more precise tools of navigation provided.

Back and Forward

The Back and Forward actions allow you to move your cursor back to the place where it was previously for more than a few seconds or where you’ve made edits. This information persists throughout sessions, so even if you exit the IDE, you can still get back to the positions that you were in before you quit.

This falls into the Omni category because these two actions could potentially get you from any place within a file to any place within a file in your directory (that you have been to) to even parts of the standard library that you’ve looked into as well as your third-party Python packages. The Back and Forward actions are perhaps two of my most used navigation actions, and you can use Keymap. Or, one can simply click on the Navigate menu to see the keyboard shortcuts:

Mastering Pycharm

Macro

The difference between Macro and Omni is subtle. Omni allows you to go to the exact location of a place, even a place of no particular significance (say, the third line of a documentation string) in any file. Macro, on the other hand, allows you to navigate anywhere of significance, such as a function definition, class declaration, or particular class method.

Go to definition or navigate to declaration

Go to definition is the old name for Navigate to Declaration in PyCharm. This action, like the one previously discussed, could lead you anywhere—a class inside your project or a third party library function. What this action does is allow you to go to the source file declaration of a module, package, class, function, and so on. Keymap is once again useful in finding the shortcut for this particular action.

Mastering Pycharm

Using this action will move your cursor to the file where the class or function is declared, may it be in your project or elsewhere. Just place your cursor on the function or class and invoke the action. Your cursor will now be directly where the function or class was declared.

There is, however, a slight problem with this. If one tries to go to the declaration of a .so object, such as the datetime module or the select module, what one will encounter is a stub file (discussed in detail later). These are helper files that allow PyCharm to give you the code completion that it does. Modules that are .so files are indicated by a terminal icon, as shown here:

Mastering Pycharm

Search Everywhere

The action speaks for itself. You search for classes, files, methods, and even actions. Universally invoked using double Shift (pressing Shift twice in quick succession), this nifty action looks similar to any other search bar. Search Everywhere searches only inside your project, by default; however, one can also use it to search non-project items as well. Not using this option leads to faster search and a lower memory footprint.

Mastering Pycharm

Search Everywhere is a gateway to other search actions available in PyCharm. In the preceding screenshot, one can see that Search Everywhere has separate parts, such as Recent Files and Classes. Each of these parts has a shortcut next to their section name. If you find yourself using Search Everywhere for Classes all the time, you might start using the Navigate Class action instead which is much faster.

The Switcher tool

The Switcher tool allows you to quickly navigate through your currently open tabs, recently opened files as well as all of your panels.

Mastering Pycharm

This tool is essential since you always navigate between tabs. A star to the left indicates open tabs; everything else is a recently opened or edited file. If you just have one file open, Switcher will show more of your recently opened files. It’s really handy this way since almost always the files that you want to go to are options in Switcher.

The Project panel

The Project panel is what I use to see the structure of my project as well as search for files that I can’t find with Switcher. This panel is by far the most used panel of all, and for good reason. The Project panel also supports search; just open it up and start typing to find your file.

Mastering Pycharm

However, the Project panel can give you even more of an understanding of what your code looks similar to if you have Show Members enabled.

Mastering Pycharm

Once this is enabled, you can see the classes as well as the declared methods inside your files.

Mastering Pycharm

Note that search works just like before, meaning that your search is limited to only the files/objects that you can see; if you collapse everything, you won’t be able to search either your files or the classes and methods in them.

Micro

Micro deals with getting places within a file. These tools are perhaps what I end up using the most in my development.

The Structure panel

The Structure panel gives you a bird’s eye view of the file that you are currently have your cursor on. This panel is indispensable when trying to understand a project that one is not familiar with.

Mastering Pycharm

The yellow arrow indicates the option to show inherited fields and methods. The red arrow indicates the option to show field names, meaning if that it is turned off, you will only see properties and methods. The orange arrow indicates the option to scroll to and from the source. If both are turned on (scroll to and scroll from), where your cursor is will be synchronized with what method, field, or property is highlighted in the structure panel. Inherited fields are grayed out in the display.

Ace Jump

This is my favorite navigation plugin, and was made by John Lindquist who is a developer at JetBrains (creators of PyCharm). Ace Jump is inspired from the Emacs mode with the same name. It allows you to jump from one place to another within the same file. Before one can use Ace Jump, one has to install the plugin for it. Ace Jump is usually invoked using Ctrl or command + ; (semicolon). You can search for Ace Jump in Keymap as well, and is called Ace Jump. Once invoked, you get a small box in which you can input a letter. Choose a letter from the word that you want to navigate to, and you will see letters on that letter pop up immediately.

Mastering Pycharm

If we were to hit D, the cursor would move to the position indicated by D. This might seem long winded, but it actually leads to really fast navigation.

If we wanted to select the word indicated by the letter, then we’d invoke Ace Jump twice before entering a letter.

Mastering Pycharm

This turns the Ace Jump box red. Upon hitting B, the named parameter rounding will be selected.

Often, we don’t want to go to a word, but rather the beginning or the end of a line. In order to do this, just hit invoke Ace Jump and then the left arrow for line beginnings or the right arrow for line endings.

Mastering Pycharm

In this case, we’d just hit V to jump to the beginning of the line that starts with num_type.

Mastering Pycharm

This is an example, where we hit left arrow instead of the right one, and we get line-ending options.

Summary

In this article, I discussed some of the best tools for navigation. This is by no means an exhaustive list. However, these tools will serve as a gateway to more precise tools available for navigation in PyCharm. I generally use Ace Jump, Back, Forward, and Switcher the most when I write code. The Project panel is always open for me, with the most used files having their classes and methods expanded for quick search.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here