70

When using Eclipse I browse through the package explorer tree using the keyboard arrows a lot.

In Windows I can expand a collapsed node by pressing the key. In Linux I need to press Shift + . Is there a way to reconfigure this so that Shift is not required?

4
  • This really isn't an Eclipse question. Eclipse uses native OS widgets, like tree controls and key binding behavior that you are describing comes from windows. I am adding windows tag to your question. Jan 20, 2011 at 16:11
  • 1
    @Konstantin fair point, but wouldn't linux or even ubuntu (to be specific) be a better tag as that's where I'm having the problem, not in windows.
    – Alb
    Jan 20, 2011 at 22:20
  • good point. swapping windows tag for linux. Jan 21, 2011 at 17:01
  • 12
    It would be nice to imitate the Windows behaviour even closer: Right on an already expanded node should jump to the first child, Left on an already collapsed node should jump to its parent node.
    – Adrian H.
    Jan 14, 2013 at 12:22

7 Answers 7

113

Put this into your ~/.gtkrc-2.0 and you should be good to go. The Left and Right lines make the requested change, the rest are just my personal additions to make the tree-view act more vim-like.

binding "gtk-binding-tree-view" {
    bind "j"        { "move-cursor" (display-lines, 1) }
    bind "k"        { "move-cursor" (display-lines, -1) }
    bind "h"        { "expand-collapse-cursor-row" (1,0,0) }
    bind "l"        { "expand-collapse-cursor-row" (1,1,0) }
    bind "o"        { "move-cursor" (pages, 1) }
    bind "u"        { "move-cursor" (pages, -1) }
    bind "g"        { "move-cursor" (buffer-ends, -1) }
    bind "y"        { "move-cursor" (buffer-ends, 1) }
    bind "p"        { "select-cursor-parent" () }
    bind "Left"     { "expand-collapse-cursor-row" (0,0,0) }
    bind "Right"    { "expand-collapse-cursor-row" (0,1,0) }
    bind "semicolon" { "expand-collapse-cursor-row" (0,1,1) }
    bind "slash"    { "start-interactive-search" () }
}
class "GtkTreeView" binding "gtk-binding-tree-view"

then restart your Eclipse to apply new bindings

11
  • 5
    I want to hug you so hard. This has bugged me for months!
    – oschrenk
    Aug 7, 2012 at 16:22
  • 3
    It works indeed great when the current selection is on a folder. But the left arrow behavior is still not exactly the same as on Windows. On an already collapsed folder, it would bring you to the parent folder. Also, when the current selection is a file, it would bring you to the parent folder. However on Mint it still stays in the current selection. But this is still a huge improvement, thanks!
    – BalusC
    Dec 31, 2013 at 7:44
  • 2
    This was working great until Eclipse Luna, any ideas why stopped working? Jun 30, 2014 at 12:58
  • 1
    Luna: use "big data nerd"'s answer (see below)
    – Philipp
    Sep 11, 2014 at 14:18
  • 2
    Adding bind "<Alt>Up" { "select-cursor-parent" () } will enable you to use Alt-Up to jump to the parent node. Then you can easily collapse the tree, even if you current child-node selection is far down the list. Dec 20, 2014 at 22:06
34

If anyone is wondering how to do this with GTK3 - simply open ~/.config/gtk-3.0/gtk.css and add the following:

@binding-set MyTreeViewBinding
{
    bind "Left"     { "expand-collapse-cursor-row" (0,0,0) };
    bind "Right"    { "expand-collapse-cursor-row" (0,1,0) };
}

GtkTreeView
{
  gtk-key-bindings: MyTreeViewBinding;
}
7
  • 1
    This is very relevant as Eclipse recently moved to GTK 3 - while all 4.x releases can use GTK 3 by setting an environment variables, Mars has moved to GTK 3 by default.
    – Guss
    Jan 20, 2015 at 7:38
  • 1
    Take a look at stackoverflow.com/a/32529393/306047, IMHO it is useful to bind the "Left" key to "select-cursor-parent" as well.
    – snorbi
    Aug 17, 2016 at 9:45
  • 1
    Tested on Ubuntu 16.04 and worked smoothly. Thanks :) Mar 29, 2017 at 15:34
  • 1
    Have just created this gtk.css under ~/.config/gtk-3.0/ and it works like a charm on CentOS Linux release 7.3.1611 Now am able to navigate using Keyboard arrows on Eclipse!! May 25, 2017 at 7:17
  • 1
    This worked fine for me for a while, but now on Ubuntu 17.04 with GTK+ 3.22 it stopped working. Any additional hints will be appreciated.
    – Guss
    Jun 8, 2017 at 17:51
19

My version for GTK3 that behaves in more natural way. Add the following to ~/.config/gtk-3.0/gtk.css:

@binding-set MyTreeViewBinding
{
    bind "Left"     { "select-cursor-parent" ()
                      "expand-collapse-cursor-row" (0,0,0) };
    bind "Right"    { "expand-collapse-cursor-row" (0,1,0) };
}

GtkTreeView
{
    gtk-key-bindings: MyTreeViewBinding;
}
3
  • Suggestion, the answer would be more helpful if you explicitly described how this behaves differently.
    – studgeek
    May 5, 2016 at 3:58
  • Excellent. I adapted the css to use shift+left instead. Check it out.
    – mppfiles
    Mar 28, 2019 at 12:12
  • Changed the accepted answer to this one as I guess most are using GTK3 by now
    – Alb
    Jun 20, 2020 at 0:05
12

The answer provided by Andrew is correct. Please note that in newer versions of Ubuntu there is no ~/.gtkrc-2.0 file, so you can either create it or you can edit the gtkrc of your current theme, which is stored in

/usr/share/themes/your_theme/gtk-2.0/gtkrc

1
  • 4
    Since I recently switched to Linux Mint (Cinnamon) I tried to figure out how to restore this useful trick, since the your_theme folder does not contain any gtk* file. The answer is this file: /usr/share/themes/Default/gtk-2.0-key/gtkrc
    – Sebastiano
    Sep 20, 2012 at 22:03
5

I tried to use the answer from @Andrew Lazarev. However due to a non backward compatible change on GTK3.20 (https://bugzilla.gnome.org/show_bug.cgi?id=766166) the bindings have to be slightly adapted:

@binding-set MyTreeViewBinding
{
   bind "Left"     { "select-cursor-parent" ()
                  "expand-collapse-cursor-row" (0,0,0) };
   bind "Right"    { "expand-collapse-cursor-row" (0,1,0) };
}

treeview
{
   -gtk-key-bindings: MyTreeViewBinding;
}

Note the - before gtk-key-bindings and the GtkTreeView renamed to treeview.

2
  • Is there a way to add flow control or conditions? As you see, when you hit left arrow, it navigates to the parent and applies collapse. What a user usually wants is: if current element is collapsable and is expanded: collapse, else if current element no collapsable or is collapsed: navigate to parent. How would you go about that?
    – joker
    Feb 24, 2020 at 13:44
  • Yes. Thank you. That finally worked for me with Eclipse 2021-09 with slightly different mapping: { bind "<Alt>Up" { "select-cursor-parent" ()}; bind "Left" { "expand-collapse-cursor-row" (0,0,0) }; bind "Right" { "expand-collapse-cursor-row" (0,1,0) }; } Dec 3, 2021 at 10:08
2

The navigation of Tree widget is controlled by underlaying widget toolkit - GTK. SWT/Eclipse has no control over it. If any such configuration is required for changing the short-cut, then it has to be made from the GTK side itself.

2
  • Thanks That makes sense. In Nautilus however, I can expand nodes with the arrow keys without shift. I looked in System -> Preferences -> Keyboard Shortcuts but don't see anything about this shortcut there.
    – Alb
    Jan 21, 2011 at 18:02
  • 1
    The reason seems to be that in GTK a TreeView can have multiple columns, and left/right move between columns so they cannot be used to expand/collapse the node: mail.gnome.org/archives/gtk-devel-list/2004-March/msg00223.html (and yes, that's a very old mail) May 30, 2011 at 8:46
1

Basing on YMomb answer I ended up with config bellow (~/.config/gtk-3.0/gtk.css). Works well with Eclipse 2021-09.

@binding-set MyTreeViewBinding
{
    bind "<Ctrl>Left" { "select-cursor-parent" ()
                   "expand-collapse-cursor-row" (0,0,0) };
    bind "Left"     { "expand-collapse-cursor-row" (0,0,0) };
    bind "Right"    { "expand-collapse-cursor-row" (0,1,0) };
}

treeview
{
  -gtk-key-bindings: MyTreeViewBinding;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.