Search and navigation in CLion

When working with large projects, it’s often beneficial to be able to quickly locate files and folders, as well as types and their members. It is also very helpful to be able to quickly visualize and navigate inheritance hierarchies as well as include dependencies. This is precisely what IDEs are made for, and CLion is no exception. Here is an overview of some of the navigation facilities that come with CLion.

Go To Actions

CLion comes with several quick navigation actions available with their respective shortcuts (given here for Windows and Linux, as well as for OS X for the cases it differs from Windows and Linux one). These include

  • Go to Class (Ctrl+N/⌘O) takes you a particular type

    Notice how you don’t need to enter the full type name – just a few letters are enough to find something. Also, it’s possible to press Ctrl+N/⌘O once again to have the search include files which are not part of the project as such.
  • Go to File Name is invoked with Ctrl+Shift+N/⇧⌘O and, as the name suggests, this takes you to a particular file.

    One thing to note is that you don’t have to use a proper substring to search on. For example, typing the letters cml will locate the CMakeLists.txt file:
  • Go to Symbol, invoked with Ctrl+Alt+Shift+N/⌥⌘O, locates a symbol, whether it is the name of a type or type member.

File Structure

One neat way of visualizing the contents of the file you are currently in is the File Structure window. This gives you a tree-like presentation of all the structures (classes, members, nested classes, etc.) in a file. CLion supports two-way synchronization, so selecting the item in the tree takes you to the corresponding place in the code, and vice versa:

You can bring up this tool window by pressing Ctrl+F12/⌘F12. Please note that it only supports structured code files.

In-Code Navigation

CLion comes with plenty of navigation options when you’ve got the cursor on a particular code element. First, there’s Navigate to Related Symbols (Ctrl+Alt+Home/^⌘↑) — this does exactly what the name suggests, meaning that if you’re on a function declaration, for example, it will take you to the definition.

CLion also has a shortcut in case you want to Navigate to Declaration (Ctrl+B/⌘B). You can also Navigate to Super Definition to jump up the inheritance hierarchy.

Hierarchy Windows

Another useful tool window is the Type Hierarchy window, which shows an inheritance hierarchy for a particular type:

This hierarchy window can be shown with Ctrl+H, and can show either all the descendants or all the parents (which is helpful when multiple inheritance is concerned).

Another type of hierarchy that CLion knows how to illustrate is the Imports Hierarchy (Alt+Shift+H), usable for quickly browsing the chain of includes that a particular header file brings.

Yet another type of hierarchy window is the Caller/Callee Hierarchy (Ctrl+Alt+H), which illustrates who exactly calls a particular function. The hierarchy illustrates all the call chains as a tree that you can navigate:

Recent Files and the Switcher

To navigate to any of the currently open files in the solution, simply press Ctrl+Tab. Similarly, to navigate to any of the recent files you’ve been working with, press Ctrl+E/⌘E. Both of these windows use the Switcher mechanism:

Note the elements on the left: selecting them will open up the corresponding tool window. The entire list can be scrolled forwards and backwards with Tab and Shift+Tab.

Error Navigation

To quickly go through the errors in a project, you can press F2 to Go to Next Highlighted Error, and Shift+F2 to go back.

Watch CLion search and navigation features in action:

If you haven’t seen CLion already, grab the EAP build and give it a go! ■

This entry was posted in Tips'n'Tricks and tagged , . Bookmark the permalink.

20 Responses to Search and navigation in CLion

  1. Andy Aylward says:

    This is very exciting! I’m thrilled about you folks making a c++ IDE!

  2. I find this whole series of tips and tricks invaluable. I use CLion and PyCharm, and every time I read one of your articles I find out some little time-saving feature I didn’t know about.

  3. Oaheix says:

    Thanks very much for the helpful tips and tricks! But I have one simple question: When I navigate through a complex code hierarchy, and go deeply into some position, is there a way to go back to the previous cursor positions, i.e. to go back along the history line, during the code navigation?

  4. Alexey says:

    “File Structure…CLion supports two-way synchronization, so selecting the item in the tree takes you to the corresponding place in the code, and vice versa:”

    This is not true about “vice versa support”. “File Structure” tool window does not support “Autoscroll from source”. There are not setting items “Autoscroll to source, Autoscroll from source” for “File Structure” tool window.
    It this is a pity. WebStorm seems also does not have this useful capability.

    I thought to create request feature for this, but it seems I should create bug report.

  5. Pedro Gomes says:

    How do I search for a method inside a file?

    This is one of the things I miss from Sublime is that on a large file with many methods I can do a goto and with ‘@’ I can easily find a method name or whatever in the file I’m at.

    CLion has the hierarchy, but even there I can’t search, can I?

    • Anastasia Kazakova says:

      You can search for a symbol by name – that’s through the whole project. Or you can open File Structure view, select it and simply start typing the method name. If autoscroll to source is on, then you’ll also be navigated to this method in the editor.

  6. Eric Lariviere says:

    Is there a way to get the caller hierarchy for a variable instead of a method?

    In other words, I would like to have the caller hierarchy for the methods and functions where a specific variable is used.

    This is possible with the caller hierarchy in eclipse.

    • Anastasia Kazakova says:

      No, it’s not possible currently. You can do find usages to find all functions and build hierarchies for these functions.

  7. Rodrigo Reichert says:

    Is there a way in CLion to display the “Class Hierarchy” (both parent + subclasses in one tree)? I have the button disabled on my CLion application and it appears that that the second screenshot above under the “Hierarchy Windows” section does as well.

    • Anastasia Kazakova says:

      It’s called type hierarchy (Ctrl+H). Put a caret on a class name and call it. The tree can be reverted to see base or derived classes.

      • Rodrigo Reichert says:

        I’m aware that by pressing (Ctrl+H) opens up the type hierarchy, however from this point forth, I can only see either the tree for either the derived classes (if I hover over the button the tool tip shows “Subtype Hierarchy”) or the tree for the super classes (tool tip shows “Super Types”). What I can’t seem to do is show both sets of information in one tree, which I’m assuming would be possible with the button who’s tool tip shows “Class Hierarchy”, however that button is always disabled.

        I can’t seem to get the button to enable itself so that I may get that functionality. I tried to draw your attention to the screenshot:

        https://d3nmt5vlzunoa1.cloudfront.net/clion/files/2015/03/8.png

        Which is shown above on this exact website to demonstrate that even in your examples, the button is disabled (the left most button on that screenshot).

  8. John says:

    Searching for symbols (alt-ctrl-shift-N) doesn’t seem to work in C code, do I have to do something to make it work or whats the reason for it?

    • Anastasia Kazakova says:

      Please check that the corresponding files are included into the project explicitly in CMake (and not greyed out in the project view)

Leave a Reply

Your email address will not be published. Required fields are marked *