4

I have some long C & C++ header files with a lot of nested #if statements in them.

#if FOO
    ...
#elif BLAR
    #ifndef WIDGET
    #endif
#else
    ...
#end

Is there a way to jump between matching statements in Eclipse? I have found a similar question that says it is possible in Visual Studio which suggests that the CTRL + ] key combination should do it, but that seems to only work for matching braces.

Community
  • 1
  • 1
TafT
  • 2,764
  • 6
  • 33
  • 51

2 Answers2

0

No, there is not a way. However, this bug report submitted in 2007 requests this feature: https://bugs.eclipse.org/bugs/show_bug.cgi?id=182579.

If you want this feature, go there and vote on it to raise this request's importance and give it more priority. You might also leave a comment.

Until this feature is added, a super lousy sort of work-around, as you suggested, is to use the "Find/Replace" dialog and "Find Next"/"Find Previous" shortcut keys, searching for the # character.

Steps: Ctrl + F then enter # in the "Find" box. Press the "Find" button. This will jump to the next # character it finds. With the # character now cached in the Find/Replace dialog, you may close the "Find/Replace" dialog box and use the shortcut keys if you like:

  • Ctrl + K = "Find Next"
  • Ctrl + Shift + K = "Find Previous"

You may optionally change these keys via the settings under Window --> Preferences --> General --> Keys (source).

You may use CTRL+Shift+L to see the "Show Key Assist" List of shortcuts (depending on the context), as explained by this answer here. Continue pressing CTRL+Shift+L a couple more times to automatically open up the Window --> Preferences --> General --> Keys settings window.

Tested in Eclipse IDE for C/C++ Developers v4.7.3a in Ubuntu 14.04.

Please vote on the Eclipse feature request "bug" above to get the developers to add in a proper solution.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • You are stating a method as mine that is not what I was doing. "Find Next" and "Find Previous" in my examples worked off of being at a statement, not off of having something in the Find/Search box. Some 4 or 5 years later how this work may have changed but it is not how I originally worked around the problem. – TafT Feb 14 '20 at 09:35
  • I suppose I'd have to see a video of you doing your technique. I wish Eclipse would add the ability to find matching # statements natively. It has been a long time. Perhaps a solution now exists? – Gabriel Staples Feb 14 '20 at 09:39
  • I no longer use Eclipse day to day as I am working more in Go. When I return to C or C++ work I will try to see what a more modern version of Eclipse does. – TafT Feb 14 '20 at 10:08
-3

It seem that with the cursor on a #statment Ctrl+k will move down the file to a matching statement while Ctrl+K will move in reverse.


While writing this up I came across a Visual Studio 2010 question: How to jump to matching #if/#elif/#endif statements? This question states that Ctrl K / Ctrl J will do what I want in Visual C++ 6.0.

As shown above, the Eclipse specific key command also uses k.


I also found that Ctrl+L brings up a a list of keyboard shortcuts and lists the Ctrl+k as Find Next and the Ctrl+K as Find Previous. Pressing Ctrl+L a second time takes you to the Preferences > Keys menu where it is possible to add new combinations.

TafT
  • 2,764
  • 6
  • 33
  • 51
  • 1
    Although your question is good, your answer doesn't seem to even answer your own question, so it baffles me that you'd ask a good question about Eclipse, write your own answer that doesn't apply to Eclipse and therefore doesn't even answer your question, and then mark your answer as correct. – Gabriel Staples Jun 08 '19 at 16:59
  • @GabrielStaples I gave the Eclipse specific answer to my question. – TafT Jun 11 '19 at 06:36
  • I must be mistaken. If so, my apologies, but I am unable to make it work in my Eclipse for C++ installation. – Gabriel Staples Jun 11 '19 at 06:38
  • @GabrielStaples What does the last part of my answer tell you is the shortcut? I have the answer, my source and a way for users to find further similar answers by accessing the short cut prompt. – TafT Jun 11 '19 at 06:40
  • It looks like this still works for me in Eclipse IDE for C/C++ Developers version 4.8.0. Further it seems that pressing Ctrl + Shift + L twice, will take you to the prompt menu and then on to the configuration menu, where you can check your keyboard scheme and set new shortcuts if required. – TafT Jun 11 '19 at 06:45
  • I've taken another look and decided to leave my downvote on your answer (but my upvote on your question). None of the shortcuts you provide work for me. I'm running Eclipse IDE for C/C++ Developers v4.7.3a on Linux Ubuntu 14.04. – Gabriel Staples Jul 19 '19 at 21:48
  • Update: With the `#` character stored in your find cache (via Ctrl + F then searching for `#`), `Ctrl` + `K` will find the next matching line with a `#` symbol (next match), and `Ctrl` + `Shift` + `K` will find the previous matching line with a `#` symbol (previous match). This is *not* a smart search, however, to find actual matching statements (like opening/closing braces would do), but rather is a dumb match simply looking for the `#` character. – Gabriel Staples Jul 19 '19 at 21:53
  • I also gave you the downvote because `Ctrl` + `L` doesn't do what you said it does. The correct sequence is `Ctrl` + `Shift` + `L`. – Gabriel Staples Jul 19 '19 at 22:10
  • Gabriel if you do not include the shift key on most systems what you are doing is l, not L. I can update my answer if the convention is to specifc the use of shift but I was not sure which systems will need it and whether it is general practise on stack* – TafT Jul 22 '19 at 11:32
  • This is very strange. Looking back at my notes it seems likely that I was using the same setup as you are now (Ubuntu 14 and for C developers) when I first found this worked for me. I wonder if I had a slightly different version of Eclipse or perhapse some plugin installed. In the recent test I was certainly working without plugins and using the example code given. I wonder what could have given us the different behaviour. Thank you for the feedback. – TafT Jul 22 '19 at 11:40