Vim Tips Wiki
Register
Advertisement
Tip 970 Printable Monobook Previous Next

created 2005 · complexity advanced · author Sanel Zukan · version 5.7


There is a nifty feature for gtk 2.x and key bindings. Unfortunately the gtk authors didn't document how to append ours. But gtk2.x ships with emacs "theme", and why we don't start from it.

Since emacs often use <SHIFT> or <CTRL> (some key) for text handling, appending bindings for it, especially for GtkEntry is not the problem. On other hand, gtk knows only <SHIFT>, <CTRL>, <ALT> and mod[1-5] special keys, and appending something like <Esc>(wait until other key is pressed) is not possible.

So this solution (I know that is limited) is focused only on GtkTextView and GtkTreeView classes (read this as: you can use keys in non editable text areas and in browser, tree widgets). Those keys are standard vi "h-j-k-l" for one line movement and <CTRL>-d <CTRL>-u for scrolling.

Detailed list

Textview widgets (readonly text areas):

h - left
j - down
k - up
l - right
<CTRL>-u - scroll 5 lines up
<CTRL>-d - scroll 5 lines down


'Similar' keys:

<CTRL>-e - down
<CTRL>-y - up
<CTRL>-f - scroll 5 lines down
<CTRL>-b - scroll 5 lines up


Tree widgets (any kind of browser widget, including trees):

h - selection left
j - selection down
k - selection up
l - selection righy

So to do 'the real work', put the following in the 'gtkrc' file.

# A key-binding set for vi-like key-bindings

binding "gtk-vi-text-view"
{
  bind "<ctrl>d" { "move-cursor" (display-lines, 5, 0) }
  bind "<ctrl>f" { "move-cursor" (display-lines, 5, 0) }
  bind "<ctrl>u" { "move-cursor" (display-lines, -5, 0) }
  bind "<ctrl>b" { "move-cursor" (display-lines, -5, 0) }
  bind "<ctrl>e" { "move-cursor" (display-lines, -1, 0) }
  bind "<ctrl>y" { "move-cursor" (display-lines, 1, 0) }
  bind "j" { "move-cursor" (display-lines, 1, 0) }
  bind "k" { "move-cursor" (display-lines, -1, 0) }
  bind "l" { "move-cursor" (logical-positions, 1, 0) }
  bind "h" { "move-cursor" (logical-positions, -1, 0) }
}

binding "gtk-vi-tree-view"
{
  bind "j" { "move-cursor" (display-lines, 1) }
  bind "k" { "move-cursor" (display-lines, -1) }
  bind "l" { "move-cursor" (logical-positions, 1) }
  bind "h" { "move-cursor" (logical-positions, -1) }
}

class "GtkTextView" binding "gtk-vi-text-view"
class "GtkTreeView" binding "gtk-vi-tree-view"

Now create in themes directory (usually in /usr/share/themes) a directory named 'Vi' (or 'Vim' if you prefer). In it, make 'gtk-2.0-key' directory too. Copy created 'gtkrc' file there. At the end, you should get something like:

'/usr/share/themes/Vi/gtk-2.0-key/gtkrc'

The last part is to note your local gtk configuration file. Check in your home directory file named '.gtkrc-2.0'. and "touch" it. The only thing left to be done is appending:

gtk-key-theme="Vi"

If you used 'Vim' for the name of the directory, use 'Vim' instead of 'Vi' in the above line.

Now start some gtk2.x application and play with those keys.

For scrolling I used 5 lines because window size can't be calculated. Increase or decrease scroll jump as you like.

Comments[]

Instead of /usr/share/themes you can use ~/.themes

Also you can set the gconf key with

gconftool-2 -s /desktop/gnome/interface/gtk_key_theme --type string "Vi"



gtk-key-theme="Vi"

Should be…

gtk-key-theme-name="Vi"

Another three keys I've always wanted…

binding "gtk-vi-text-entry" {
	bind "<ctrl>h" { "delete-from-cursor" (chars, -1) }
	bind "<ctrl>u" { "delete-from-cursor" (paragraph-ends, -1) }
	bind "<ctrl>w" { "delete-from-cursor" (word-ends, -1) }
}

class "GtkEntry" binding "gtk-vi-text-entry"
class "GtkTextView" binding "gtk-vi-text-entry"

port to gtk-3.0[]

0x255@SonicTorment:~$ cat ~/.themes/Vi/gtk-3.0/gtk-keys.css
@binding-set gtk-vi-text-entry
{

  bind "<ctrl>u" { "delete-from-cursor" (paragraph-ends, -1) };
  bind "<ctrl>h" { "delete-from-cursor" (chars, -1) };
  bind "<ctrl>w" { "delete-from-cursor" (word-ends, -1) }; /* delete word */
}

@binding-set gtk-vi-text-view
{
    bind "<ctrl>d" { "move-cursor" (display-lines, 5, 0) };      /* 5 lines down */
    bind "<ctrl>f" { "move-cursor" (display-lines, 5, 0) };      /* 5 lines down */
    bind "<ctrl>u" { "move-cursor" (display-lines, -5, 0) };     /* 5 lines up */
    bind "<ctrl>b" { "move-cursor" (display-lines, -5, 0) };     /* 5 lines up */
    bind "<ctrl>e" { "move-cursor" (display-lines, -1, 0) };     /* down */
    bind "<ctrl>y" { "move-cursor" (display-lines, 1, 0) };      /* up */
    bind "j" { "move-cursor" (display-lines, 1, 0) };            /* down */
    bind "k" { "move-cursor" (display-lines, -1, 0) };           /* up */
    bind "l" { "move-cursor" (logical-positions, 1, 0) };        /* right */
    bind "h" { "move-cursor" (logical-positions, -1, 0) };       /* left */
}

@binding-set gtk-vi-tree-view
{
    bind "j" { "move-cursor" (display-lines, 1) };               /* selection down */
    bind "k" { "move-cursor" (display-lines, -1) };              /* selection up */
    bind "l" { "move-cursor" (logical-positions, 1) };           /* selection right */
    bind "h" { "move-cursor" (logical-positions, -1) };          /* selection left */

}

GtkEntry {
  -gtk-key-bindings: gtk-vi-text-entry;
}

GtkTextView {
  -gtk-key-bindings: gtk-vi-text-entry, gtk-vi-text-view;
}

GtkTreeView {
  -gtk-key-bindings: gtk-vi-tree-view;
}

0x255@SonicTorment:~$ cat ~/.config/gtk-3.0/settings.ini
[Settings]
gtk-theme-name=Adwaita
gtk-key-theme-name=Vi
gtk-icon-theme-name=gnome-wine
gtk-font-name=Dejavu Sans Book 8
gtk-toolbar-style=GTK_TOOLBAR_ICONS
gtk-color-scheme="base_color:#ffffff\nfg_color:#000000\ntooltip_fg_color:#000000\nselected_bg_color:#86ABD9\nselected_fg_color:#ffffff\ntext_color:#1A1A1A\nbg_color:#EDECEB\ntooltip_bg_color:#F5F5B5"
Advertisement