📜 ⬆️ ⬇️

Setting up Vim-like controls in GTK

I use the Vim text editor for almost all tasks related to interactive text editing: for configuration files and source code, as LaTeX, for writing letters in conjunction with mutt and even as an external editor in a web browser.

Well-established hotkeys for cursor control [hjkl] began to move beyond the editor, for example, into the command shell (in conjunction with the modifier).

However, I would like to have the usual hotkeys in graphics applications. Actually, the following discussion focuses on setting up a GTK environment.

Perhaps, many people know about the existence of gtk-emacs binders, located in / usr / share / themes / Emacs, which can be simply embedded in the GTK user configuration file. For Vim, unfortunately, ready-made recipes "out of the box" are not provided, moreover, the implementation of multi-mode is not a trivial task, so you have to do with one mode using modifiers.
')
It was decided to hang the cursor keys on Mod1 + [hjkl], the beginning / end of the line Mod1 + [wq], plus describe the classic binding of the form ^ H, ^ U and ^ W.

Without further ado, I give examples of files:

~ / .gtkrc-2.0
binding "vim-like" { 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) } bind "<alt>j" { "move-cursor" (display-lines, 1, 0) } bind "<alt>k" { "move-cursor" (display-lines, -1, 0) } bind "<alt>l" { "move-cursor" (logical-positions, 1, 0) } bind "<alt>h" { "move-cursor" (logical-positions, -1, 0) } bind "<shift><alt>j" { "move-cursor" (display-lines, 1, 1) } bind "<shift><alt>k" { "move-cursor" (display-lines, -1, 1) } bind "<shift><alt>l" { "move-cursor" (logical-positions, 1, 1) } bind "<shift><alt>h" { "move-cursor" (logical-positions, -1, 1) } bind "<alt>q" { "move-cursor" (paragraph-ends, -1, 0) } bind "<shift><alt>q" { "move-cursor" (paragraph-ends, -1, 1) } bind "<alt>w" { "move-cursor" (paragraph-ends, 1, 0) } bind "<shift><alt>w" { "move-cursor" (paragraph-ends, 1, 1) } } class "GtkEntry" binding "vim-like" class "GtkTextView" binding "vim-like" 


~ / .config / gtk-3.0 / settings.ini
 gtk-key-theme-name=Vim 


~ / .themes / Vim / 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) }; bind "<alt>j" { "move-cursor" (display-lines, 1, 0) }; bind "<alt>k" { "move-cursor" (display-lines, -1, 0) }; bind "<alt>l" { "move-cursor" (logical-positions, 1, 0) }; bind "<alt>h" { "move-cursor" (logical-positions, -1, 0) }; bind "<shift><alt>j" { "move-cursor" (display-lines, 1, 1) }; bind "<shift><alt>k" { "move-cursor" (display-lines, -1, 1) }; bind "<shift><alt>l" { "move-cursor" (logical-positions, 1, 1) }; bind "<shift><alt>h" { "move-cursor" (logical-positions, -1, 1) }; bind "<alt>q" { "move-cursor" (paragraph-ends, -1, 0) }; bind "<shift><alt>q" { "move-cursor" (paragraph-ends, -1, 1) }; bind "<alt>w" { "move-cursor" (paragraph-ends, 1, 0) }; bind "<shift><alt>w" { "move-cursor" (paragraph-ends, 1, 1) }; } @binding-set gtk-vi-text-view { 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-set 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) }; } 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; } 


The article was based on the Vi key bindings in gtk article, which describes a similar procedure for read-only environments.

Source: https://habr.com/ru/post/266669/


All Articles