Shift + ZZ is an elegant way to save and exit a file in vim. But true power users have more uses under the hood. Just exclude the Shift to get your screen moving miles! Combine it with search and investigate your files like a coding wizard!
Do you waste a lot of time trying to bring the current line of code to the center, going all the way down and then all the way up to reposition the editor’s viewport? Well, I used to do that too…
For many long-time Vim users, using the lowercase zz in Vim is extremely common. But unfortunate enough as it already is for newcomers, there are hardly any posts that talk about “zz”. Worry not, because your search ends here!
Using ZZ to quit Vim
Before we move on to the scrolling part, let me introduce you to the use of ZZ most people commonly refer to. If you’re familiar with the famous “Unable to quit Vim” memes, you must have already seen all the different ways you can exit vim, some of which are:
Exit vim (if it is not modified):
Exit all open files:
Exit without saving:
Exit after saving:
Exit after saving – version 2:
You should’ve noticed, that all the above commands for exiting Vim require you to first enter Vim Command Mode, which is done by typing a “:“.
If for some reason, you don’t like this convention of needing to enter the Command Mode, Vim has a built-in keybinding for just that! You can leave Vim without entering the Command Mode by simply pressing the following in Vim Normal Mode, which will first save the file and then exit out of it:
[Shift] + ZZ
If you want to exit the file without saving it, use:
[Shift] + ZQ
This pretty much covers the Vim ZZ that most people talk about and use. However, Vim is just too deep to have a single setting for a single task. It always has something under its hood, that is enough to shatter your expertness pride! This time, it’s of no difference.
Using zz to center screen on current line
I present to you, a noble Vim keybinding that will definitely save you a plethora of time! If you’re a software engineer or a hard-earned programmer who always has to look through blocks of code and keep track of the functions that surround your current line of text, you’d be no stranger to the commonly used method to center the screen on your current line, which is to first go a few lines downwards or upwards, and then again go all the way back to that previous line to reposition the viewport. This sure is horribly inconvenient, and more importantly, it wastes a lot of valuable time. If you’re fatigued having coded all they, it becomes even more of a headache for your head and a pain for your innocent hands!
Sleep no more, you yawning cat! Because I’m about to show you an efficient way of doing the same thing which takes from 0 to no seconds, if you happen to be Vim user.
First, navigate to the line of code you’d line to center. Once done, press the following in Vim:
Notice that the letters are lowercase and no Shift key is pressed.
This way you, can bring your current line of code to the center of the viewport, without wasting a lot of time and doing it the Vim Way!
Meet the whole family – zt, zb, z. and z-
Lowercase zz in Vim is enough to satisfy most of the programming wolves out there. But there are hungrier folks who’d not be happy with a single dish in their appetite. Remember what I said? Vim is too good to have a single setting for a single task, and likewise, it has more than enough settings for a single form of task to be carried out. You’re not only limited to centering your current line, but also bring the current line to the top or the bottom of the screen.
To bring the current line to the top of the screen, press:
To take the current line to the bottom of the screen, press:
Hold on, Doctor Flash! There’s even more (as long as you’re fine digesting them).
To bring the current line of code to the center, and also reposition the the cursor to the beginning of the line, press:
To reposition the current line of code to the top of the screen, and then reposition the the cursor to the beginning of the line, press:
z + [Enter]
To take the current line of code to the bottom of the screen, and then once again reposition the the cursor to the beginning of the line, press:
Navigate like a Boss – combine zz with search!
You can even parse any of the above zz commands along with a search input in Vim, to land directly on the line that matches the search. The following gif should better demonstrate the niceness of parsing zz comands with search:
Now folks, you should pretty much be able to give your fellow pals, who’re not using vim yet, the cheesiest of eyegasms with these efficient viewport handling commands in Vim. Tell them this you haven’t already –
Tired of running in a code-editing gym?
Why not switch and try out Vim!