This is part of a series of tips on using Vi. To see the complete series, click here.
Sometimes when I’m editing a file, I like to have the line I’m looking at centred on the screen. This helps me to see an equal amount of context both above and below that line. I seem to never remember the shortcut that centres the screen on the current line, so I end up using the j or k key to move around in the document until I’ve got the line I want in the middle of the screen, and then I move back to that line. That’s a waste of effort, but vi has a nice easy way of doing it: press z then the period (z.), and it does it for you!
Let’s see what this looks like. In this screenshot, I’m editing a file, and I’ve got my cursor on the comment line that starts “This function calculates…”:
But that’s too low on the screen for my liking; I want that line centred. Press z period (z.), and my screen now looks like:
The line with my cursor is now in the centre of the screen! OK, it’s not that exciting, but the shortcut saves a bit of time when you want to do this.
There’s a couple variations on this that come in handy, too. I can move the current line to the top of the screen by pressing z then the return key (z RETURN), or I can move it to the bottom of the screen by pressing z then the minus sign (z-).