Brendan Dawes
Analogue + Digital

Hail to the g

The more I dig into Vim the more I discover the power of the most simple commands and key combinations. One of those is the use of the g key, both in normal mode and command mode. In this post I'll list some of my fave uses of the humble g key.


This little key combo will format the syntax of your entire document. gg puts the cursor at the beginning of your document. The = in conjunction with G will then format everything to the end of the document.

Yet what if you've done that and want to go back to the line you've been editing? With g there's two ways to do that:


This will move your cursor to the previous position. You can go a stage further and use:


This will move your cursor to the previous position but put you in insert mode, ready for you to carry on editing.

The power of g also comes in handy when using visual selection mode. Let's say you've selected some text maybe using v or shift-v to go into visual line selection mode. Sometimes you might want to reselect that selection again for some reason. That's really easy to do:


This will select your last visual selection.

Want to quickly open a file included in your code? Position your cursor on the string defining the file and then just press:


and that file will open in a new split. Similarly to open a url in a browser just type:


The g key comes in handy for changing the case of entire lines:


will change the current line to UPPERCASE.


will change the current line to lowercase. You can also use it with motions too, so gU$ will change everything from the cursor to the end of the line to UPPERCASE for example.

Command Mode

The global command — defined by g — comes in handy in command mode too.

To delete every line containing a pattern you can simply do this:


Or to delete every line NOT containing a pattern:


Both of these are great when you're working with CSV files and the like.

Of course that technique isn't just for deleting. You could for example change any matching line to all UPPERCASE with this:

:g/pattern/normal gU$

You get the idea.

I'm sure there's a whole lot more the humble g can do, and that's why I love continuing to learn Vim. If you know any others let me know via Twitter or other posts.