1. 29
  1.  

  2. 3

    One of the more approachable things I’ve read on how to configure Vim as a beginner.

    One thing that I keep wondering about and can’t find an answer to: What does g do in Vim? It seems to be a part of a bunch of vaguely-related commands. Is there some general theme for what it does, or are there just a bunch of commands that you have to remember are g-something?

    1. 4

      Open vim and type :help g and you’ll get a detailed description of the commands.

      1. 3

        Yeah, the trouble is that it’s kind of a pain to navigate the help until you get a good understanding of Vim. Luckily, I have a copy of the helpfile as a PDF.

        For what I wanted to know, it looks like there isn’t much of a theme behind the g commands, or the other prefix characters. No wonder people get so fired up about Vim vs Emacs - seems you need a lot of time invested in either one to use even a fraction of their features, and it’s all useless on the other one.

        1. 3

          Agreed regarding the time investment needed to get up to speed on Vim, but g is most definitely a special case.

          g is a “kitchen sink” of sorts where a bunch of unrelated commands have been stuck because on a QWERTY keyboard it’s a very easy-to-reach key.

          That said, the two most common uses of g can be thought of as goto and global.

          For goto, things like g10 take you to the 10th line.

          For global, this is taken from vim help:

          :[range]g[lobal]/{pattern}/[cmd]
                  Execute the Ex command [cmd] (default ":p") on the
                  lines within [range] where {pattern} matches.
          

          So it’s a way to apply a command to multiple lines.

    2. 1

      While I did understand my vimrc at one point, I have forgotten all the vimscript I once knew (by choice, vimscript is a major pain point for me). Good point, though, and a nice, clean .vimrc to boot.

      1. 1

        Another addition to the folding:

        nnoremap <space> za
        vnoremap <space> zf
        

        The first line is in the post, but the second one also enables folding on visual select. So you can just select text and create fold with space. Then use space to toggle it.

        And obligatory .vimrc link.