Function Repository Resource:

VimGraph

Source Notebook

Construct a graph of simple Vim-style movements in text

Contributed by: Pavel Hajek

ResourceFunction["VimGraph"][text]

returns a graph with letters as vertices and Vim-style movements as edges.

Details and Options

Vim is a cult-favorite text editor known for its modal editing—In Normal mode, certain keys are mapped to movements forward or backward to the nearest pattern, reducing keystroke distance and enabling fast editing without using the mouse.
The following simple Vim movements are supported:
ShortcutMovement Description
h / lMove one character left / right on the same line
k / jMove one character up / down; jumps to end of target line if shorter than current horizontal position
w / bJump to the beginning of the next / previous word, across lines
eJump to the end of the next word, across lines
^/$Move to the beginning/end of the current line
ResourceFunction["VimGraph"] allows the options "IncludedVimMovements", "StringPattern" and "CustomPatterns", it is possible to restrict to specific Vim movements or define new movements, respectively.
Also supported are Graph options.

Examples

Basic Examples (2) 

Vim graph for the movements: up, right, and to the beginning of the next word, respectively:

In[1]:=
ResourceFunction["VimGraph"]["How do\nI exit Vim?", "IncludedVimMovements" -> {"k", "l", "w"}]
Out[1]=

The same, with nicer formatting:

In[2]:=
ResourceFunction["VimGraph"]["How do\nI exit Vim?", "IncludedVimMovements" -> {"k", "l", "w"}, VertexSize -> Large, VertexStyle -> LightYellow, VertexLabelStyle -> Directive[Bold, Large], EdgeLabels -> "EdgeTag", EdgeLabelStyle -> Directive[Bold, Orange, Medium]]
Out[2]=

Scope (3) 

Returns a minimal sequence of keystrokes needed to move from one letter to another:

In[3]:=
With[{g = ResourceFunction["VimGraph"][
    "Chuck Norris\nwrites his DNA\n in Vim. In\nNormal mode."]}, HighlightGraph[g, Subgraph[g, FindShortestPath[g, {1, 1}, {4, 10}]], EdgeLabels -> "EdgeTag", EdgeLabelStyle -> Directive[Bold, Large]]]
Out[3]=

Illustrates the relationship between the maximum keystroke distance required to navigate between two letters in a text and the number of randomly inserted newlines:

In[4]:=
BarChart[
 Table[Mean[
   Table[GraphDiameter[
     ResourceFunction["VimGraph"][
      StringInsert[#, "\n", RandomSample[Range[StringLength[#]], n]] & @
       StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 200]]], {m, 1, 10}]], {n, 1, 20}]]
Out[4]=

Use the "CustomPatterns" option to define new movements by passing a string pattern to "StringPattern", with optional shortcuts for jumping forward or backward to the nearest match:

In[5]:=
ResourceFunction[
 "VimGraph"]["How is your\n mode today?\nNormal mode\nInsert mode\nNormal mode\nVisual mode\nInsert mode", "IncludedVimMovements" -> {}, "CustomPatterns" -> {
   <|"StringPattern" -> "Normal", "Forward" -> "n", "Backward" -> "N"|>,
   <|"StringPattern" -> "Insert", "Forward" -> "i"|>,
   <|"StringPattern" -> "Visual", "Backward" -> "V"|>} ,
 VertexSize -> Large, VertexStyle -> LightYellow, VertexLabelStyle -> Directive[Bold, Medium], EdgeLabels -> "EdgeTag",
  EdgeLabelStyle -> Directive[Bold, Orange, Medium]]
Out[5]=

Publisher

Pavel Hajek

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 27 October 2025

License Information