Skip to content

Instantly share code, notes, and snippets.

@countvajhula
Last active April 20, 2022 00:49
Show Gist options
  • Save countvajhula/0721a5fc40f2124097652071bb9f97fb to your computer and use it in GitHub Desktop.
Save countvajhula/0721a5fc40f2124097652071bb9f97fb to your computer and use it in GitHub Desktop.
A Grammar for Vim's Normal Mode

A Grammar for Vim's Normal Mode

A rough attempt at characterizing Vim's Normal Mode grammar that I made while writing this Vim series. See the bibliography there for more context.

Notation: | means or, [] means optional, * means zero or more, <> means literal class (e.g. <number> could expand to 1, 2, etc.), ... means there are more instances not enumerated here (these would need to be fleshed out to get a complete grammar).

It could be interesting to use a grammar like this one to organize an interactive Vim cheatsheet.

normal-command = motion-form
               | verb-phrase
               | input-expression
               | prepositional-verb-phrase
               | pronoun-verb-form
               | macro-expression
               | special-form

motion-form = [quantifier] motion

verb-phrase = [quantifier] verb-form noun-form
            | [quantifier] grammaticalized-verb-form

input-expression = [quantifier] input-verb

prepositional-verb-phrase = prepositional-destination-form verb-phrase
                          | prepositional-source-form pronoun-verb-form

pronoun-verb-form = p | P | gp | gP

macro-expression = [quantifier] built-in-macro

special-form = quantifier %
             | q direct-object
             | @ indirect-object
             | m mark
             | R | Q | : | @ | % | v | V | C-v | u | C-r | U | g- | g+ | q: | q/ | q?

motion = h j k l w W b B e E 0 $ ^ _ | gE gi gj g0 g$ gg G / ? n N f F t T , ; 'mark `mark ...

quantifier = <number>

verb-form = verb
          | verb verb-clause*

noun-form = [quantifier] noun

input-verb = r i a o O

prepositional-destination-form = preposition indirect-object

prepositional-source-form = preposition direct-object

built-in-macro = x X s C S D I A J gJ ~ & .

direct-object = register | append-register

mark = <lowercase letter> | <uppercase letter> | < | > | ' | ` | ...

verb = c d y g~ gu gU gq = !

verb-clause = adverb | quantifier

noun = motion | text-object

grammaticalized-verb-form = dd | cc | yy | ...

preposition = "

indirect-object = register

register = <lowercase letter> | <number> | " | : | _ | . | ...

append-register = <uppercase letter>

adverb = v | V | C-v

text-object = modifier object

modifier = i a

object = w s p ( ) { } [ ] < > " ' ` gn gN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment