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?

Lii
  • 11,553
  • 8
  • 64
  • 88
Alb
  • 3,601
  • 6
  • 33
  • 38
  • 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. – Konstantin Komissarchik Jan 20 '11 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 '11 at 22:20
  • good point. swapping windows tag for linux. – Konstantin Komissarchik Jan 21 '11 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 '13 at 12:22

7 Answers7

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

starball
  • 20,030
  • 7
  • 43
  • 238
Andrew
  • 3,272
  • 2
  • 25
  • 26
  • 5
    I want to hug you so hard. This has bugged me for months! – oschrenk Aug 07 '12 at 16:22
  • Thanks a lot, it solved the problem when I was using Ubuntu + gnome. I now switched to Fluxbox and the issue is back, any suggestion? – RaySF Apr 11 '13 at 23:46
  • 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 '13 at 07:44
  • 2
    This was working great until Eclipse Luna, any ideas why stopped working? – Jonathan Naguin Jun 30 '14 at 12:58
  • This works for Eclipse Juno and Kepler, but doesn't work for Luna! – kolobok Jul 24 '14 at 08:39
  • @akapelko one guess I have is that Luna uses a newer version of GTK than 2.0 so maybe the contents need to be placed in a file with a larger version number? Consider using strace on the process to see where it's looking – Andrew Jul 24 '14 at 20:59
  • 1
    Luna: use "big data nerd"'s answer (see below) – Philipp Sep 11 '14 at 14:18
  • 2
    Adding `bind "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. – Mads Nielsen Dec 20 '14 at 22:06
  • how to do this in Kubuntu? I do not have gnome in use and therefor I have not found a file that looks similar to what you show here in my user home – Remo Liechti Oct 08 '15 at 13:14
  • @akapelko You can deactivate GTK 3 (and thereby falling back to GTK 2) using this: export SWT_GTK3=0 – Michael Nov 24 '15 at 08:05
  • Works perfectly for GnuCash too - thank you! Why on earth are there no keyboard shortcuts by default? Such an impediment to usability (not to mention accessibility - what if you can't use a mouse?) – cdyson37 Dec 24 '17 at 11:59
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;
}
big data nerd
  • 498
  • 5
  • 9
  • 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 '15 at 07:38
  • +1000! Thank you! This is exactly what needs to be done and what I have been looking for for quite a while. You have made my life a whole lot easier!!!! (Linux Mint 17.2, Eclipse 4.5, STS 3.7.0) – Matthias Hryniszak Aug 27 '15 at 10:54
  • 1
    Take a look at http://stackoverflow.com/a/32529393/306047, IMHO it is useful to bind the "Left" key to "select-cursor-parent" as well. – snorbi Aug 17 '16 at 09:45
  • 1
    Tested on Ubuntu 16.04 and worked smoothly. Thanks :) – Ad Infinitum Mar 29 '17 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!! – Shoaib Khan May 25 '17 at 07: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 08 '17 at 17:51
  • See my answer for GTK 3.2+ update https://stackoverflow.com/a/55537016/800413 – YMomb Feb 18 '20 at 16:20
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;
}
Andrew Lazarev
  • 206
  • 2
  • 2
  • Suggestion, the answer would be more helpful if you explicitly described how this behaves differently. – studgeek May 05 '16 at 03:58
  • Excellent. I adapted the css to use shift+left instead. [Check it out](https://gist.github.com/mppfiles/1c541ec9bd90e655aab28ade4c0ce21f). – mppfiles Mar 28 '19 at 12:12
  • Changed the accepted answer to this one as I guess most are using GTK3 by now – Alb Jun 20 '20 at 00: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

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
  • 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 '12 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.

YMomb
  • 2,366
  • 1
  • 27
  • 36
  • 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 '20 at 13:44
  • Yes. Thank you. That finally worked for me with Eclipse 2021-09 with slightly different mapping: ```{ bind "Up" { "select-cursor-parent" ()}; bind "Left" { "expand-collapse-cursor-row" (0,0,0) }; bind "Right" { "expand-collapse-cursor-row" (0,1,0) }; }``` – Marek Podyma Dec 03 '21 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.

PinnamuR
  • 66
  • 3
  • 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 '11 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: http://mail.gnome.org/archives/gtk-devel-list/2004-March/msg00223.html (and yes, that's a very old mail) – Thomas Broyer May 30 '11 at 08: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;
}
Marek Podyma
  • 913
  • 10
  • 12