How to Vim. Nifty tips to become a pro Vim coder | by Richard So | Towards Data Science

Learning how to use Vim is kinda intimidating. Correction: it’s very intimidating. You’re essentially learning a different way to edit code, let alone text. Many wrongfully deem it a “waste of time” to pick up Vim.
However, I can testify that Vim has improved my coding efficiency and overall experience in the long run (I’ll explain why in the following sections). I’m not going to shove Vim down anyone’s throat, but I strongly suggest learning it as you continue your journey in programming, data science, or whatnot.
If you are longing to know if you should use Vim and/or who’s it really for, go check out this write up (watch out for the deceptive title!) or watch this YouTube video from the man himself Luke Smith.
With all that said, let's talk in more detail about what Vim really is!
For those who are asking, let me affirm this once more:
Vim is a different way of editing code, let alone text.
Vim works in “modes,” and switching between these changes the function of each key on your keyboard (e.g. pressing the W key unsurprisingly types “ w ” in INSERT mode, but moves your cursor one word forwards in NORMAL mode). This way, your keyboard also acts as an instrument to navigate through your text/code file. In other words, you don’t need your mouse when using Vim.
This is great if you have to constantly switch between editing and traversing through text, which is exactly what we programmers usually do. If you’re a non-Vim coder, you won’t believe how much time you spend reaching your mouse (or trackpad for laptop users) from your keyboard, then moving your cursor, and finally returning back to start typing. (ugh...)
Again, Vim requires some time to get used to, and it’s not an outright replacement for an IDE or something like Visual Studio Code. Yet, it arguably empowers you to edit code faster, and its more basic counterpart (Vi) is the standard terminal text editor for most UNIX-like operating systems.
With that said, let’s head onwards!
I’m not surprised that every single Vim tutorial plugs in vimtutor to start learning this text editor, and I’ll shamelessly do the exact same thing here. There’s no need for playing any “Vim games” (although they are fun) or relying on tools that’ll help you memorize redundant key bindings. Install vimtutor and run through this official Vim tutorial whenever you have around 10–15 minutes to spare. Don’t try memorizing all Vim bindings/shortcuts right off the bat; it’ll come as you continue repeating vimtutor .
NOTE: I suggest that Windows users use WSL (Windows Subsystem for Linux) for access to vimtutor and for using Vim in general. I personally have no experience using gVim on Windows, so I cannot guarantee the true Vim experience there.
Practice makes perfect.
This is the core principle of learning anything new, and Vim is no different. So while you learn Vim through vimtutor, keep on using it.
Use Vim as much as possible. Need to view a text file? Use Vim. Want to make changes to that Python script on the go? Use Vim. Looking to taking some notes? Use Vim. You get the point. And whenever you do, always think to yourself: what is the most efficient (shortest) order of keypresses I should use to accomplish this?
Oh, and cut your habit of using your mouse/trackpad.
Use Vim bindings wherever and whenever you can. Start doing everything the “Vim” way. For instance, if you use a chromium-based browser (Chrome, Brave, Edge) or Firefox, consider Vimium, an extension that ports various Vim movement keybindings (H, J, K, L, etc.) onto your browser.
github.com
If you’re currently using an IDE for development, find a Vim plugin/extension that can emulate Vim bindings for it. PyCharm (or any of Jetbrains’ IDEs) users can use ideavim, while Visual Studio Code (basically an IDE by 2021) has VSCodeVim for its user base.
github.com
github.com
Finally, Jupyterlab users can enable Vim bindings for its integrated text editor and install jupyterlab-vim for full Vim emulation within notebook cells.
github.com
Make sure Vim is mixed in well into your existing workflow. Continue on your revamped Vim lifestyle for a few weeks or so. Within a few sessions on VSCode with Vim emulation, I can testify how much more comfortable it became harnessing its power. (and how glad I was to be freed from my mouse!)
The most useless key on the best location.
This quote basically characterizes the Caps Lock key. Instead, turn Caps Lock into your Escape key! By now, you’ll learn how essential the Escape key is for switching modes in Vim. To maximize efficiency, I highly suggest you make this modification.
Windows and WSL users can use uncap, an executable that turns the Caps Lock key to Escape by default.
github.com
MacOS users are quite lucky: remapping the Caps Lock key is a breeze through System Preferences.
vim.fandom.com
For Linux, StackOverflow and Google are really your best friends. Personally (as an elite Arch Linux user😏), I use setxkbmap to switch Caps Lock as another Escape key, and autorun this following command on startup:
setxkbmap -option caps:escape
After getting used to basic-to-intermediate Vim usage, it’s time to become even more efficient with this tool. Below, I’ve compiled below the most useful/unique Vim commands (on NORMAL mode) I’ve ever learned:
ZZ — save+exit Vim the cool way
zz, zt, zb — move line of your cursor to middle, top, and bottom of your view, respectively
Ctrl+u, Ctrl+d — moves your view up/down half one page
ciw — deletes the word you’re hovering and automatically puts you in INSERT mode (change inside word)
C — deletes from cursor to end of line and puts you in INSERT mode
dt<char> — deletes from your cursor to the next instance of the character you specify (delete to <character>)
~ — toggles the case [upper/lower] on the character hovered or selected (tilde; key below Esc for standard keyboards)
. — repeat your last Vim command (period)
ggvG= — auto-indent the entire file (goto beginning, enter VISUAL mode, go/select to end of file, and indent lines [==] selected)
Again, these might seem overwhelming to you at first. Remember, strive to learn by practice, not memorization. At another perspective, these Vim commands go to show how amazingly powerful this text editor can be without your mouse or any context menu.
Vim is unbelievably powerful, given it doesn’t use any mouse input or context menus.
If you are looking for more Vim command tricks, take a look at this amazing and nerdy 1-hour vimtutor playthrough by Vim Diesel (just kidding, it’s still Luke 😛). He packs a lot of time-saving advice on Vim, so make sure you check it out!
By this time, you’re comfortable enough to use Vim to edit code significantly faster than before. Finally, you can flex your sleek code editing on reddit and your colleagues without ever leaving your home row keys! At this point, you can consider the following:
Install and experiment with Neovim (a refactored fork of Vim for hyper-extensibility and GUI support)
Pimp your Vim on the terminal/command line with Vim-Airline
Try out some popular Vim plugins
With all this said, enjoy a future filled with Vim goodness (and devoid of your mouse/trackpad)!