7

Note: this question has two sort of answers on StackOverflow but I thought perhaps a better answer would come from here, and, if not, at least it will be documented where it should be:


What is the semantic meaning of the letter g in normal mode commands? I have used the heck out of gg, G, and <n>G, but until today, I had never thought to find out what the semantics of g are in the larger Zen of Vi.

Today I had to lowercase a bunch of constants in my code, and I figured there had to be a better way than character by character (since <n>~ doesn't work, for whatever odd reason that I am unaware of). I found it in g~, specifically g~iw.

This led me to find the linked answers, but they have less than satisfying explanations, and are on StackOverflow, and not vi.SE, to boot.

So,

What is the vi-semantic meaning of g?

By vi-semantic meaning, I expect people to understand, as ciw means change in word, so c means change, i, means inner text item, and w means word delineated by space or punctuation excluding _.

P.S., how do I tag this?

8

g is a little bit of a weird key in vim. A lot of other keys have a specific word to describe their category:

  • h, j, k, l, }, {, w, e, etc. are all motions. They tell your cursor to move.

  • d, c, y, etc. are all operators. They change the text they operate on in a specific way, and you tell them what text to operate on by giving a motion.

g does not have a name like this. If I had to come up with a name, I would call it a namespace. Bram Moolenaar and Bill Joy I'm sure had many useful operations (such as go the beginning/end of the file, or go to [line]) that make sense to create a shortcut for in default vim, but they're not going to be used often enough to justify giving them a one-key shortcut, since these are reserved for the more useful/frequently used things like hjkl, or d and p, and there's only 95 keys to pick from. There are tons of keyboard shortcuts that all start with g. What do they have in common? Absolutely nothing. They just happen to be useful things to have that aren't worth giving a one-key shortcut for.

So really, gg is a motion just like all the other motions I listed, it just happens to use a two-key sequence. g~ is an operator just like all the other operators I listed, it just happens to use a two-key sequence.

There are several other "namespace" like keys in vim. For example, look up in help

  • :h z (Commands that start with 'z')
  • :h [ (Commands that start with '[')
  • 2
    So g is like a system leader, as opposed to a user leader. Just have to memorize the combos. Thank you. – malan Jan 30 at 16:58
  • 6
    g and z and really like namespaces for the next character. That's because we have been slowly running out of keys on a keyboard, so commands that are useful, had been added to those keys – Christian Brabandt Jan 30 at 17:08
  • @ChristianBrabandt Yes, namespace is a great word for that. – DJMcMayhem Jan 30 at 17:11
  • 1
    IMHO this answer should not use the word leader since a leader key exists in vim and should not be confused with g, z, [, ], and <C-w>. I would use namespace key as suggested. – Hotschke Jan 31 at 8:28
  • 1
    More importantly, g and z were two keys not used in normal mode by vi. Given how few keys were available, using them as prefixes for Vim-specific commands was really the only way to add a lot of new normal mode commands without resorting to chording. – jamessan Feb 1 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.