Follow @learnvim for more Vim tips and tricks!
When I first discovered Vim, I thought it was an outdated editor that was unnecessarily hard to use. Until one day I saw my friend doing Vim magic. I thought that was the coolest thing I have ever seen. Fast forward several years, Vim is now my main editor.
Is it worth learning Vim in 2020, where many are using modern editor like VSCode, Sublime, and Atom?
Definitely.
I will show you how to get started learning Vim. Here are things that I will cover. You don't have to read this linearly.
You may decide that Vim is not for you. That's totally fine. Vim, like other editors, is just a tool. At the end of the day, use whatever tool that lets you get the job done.
Why learn Vim
Vim is not easy to use initially. But Driving a car was not easy initially. Riding a bicycle was not easy initially. Programming was not easy initially. Many things are not easy initially. So why do we stop learning Vim because it was not easy initially?
If you are asking, "why should I learn Vim in 2020? There are many great text editors out there." Here are some advantages of using Vim:
- Lightweight.
- Challenging (for those who love challenges).
- Almost everywhere (if you ever need to SSH to server, you won't find an IDE, but you will almost always find Vim).
- Good integration with command line (with
!
, you can execute commands likels
,curl
. You can also runex
commands with:
). - Rewarding and satisfying. (Understanding Vim grammar, customizing Vim, learning Vim Script, etc, unlock a whole new realm of possibilities. It is like playing video game where you could level up, acquire new skills, and combine them).
- Unique built-in features. (Time travel, macros, text-objects, dot command are built-in features that are almost unique to Vim).
Challenges:
- Hard to exit the first time.
- Steep learning curve (not as bad as you think).
- First time using it will cut productivity (learning always takes time. You don't have to start Vim cold. Keep your regular editor at work. Learn Vim at your time, on your own side project. Switch when you are ready).
Learning Vim
In first part, I will show some practical steps to learn Vim from absolute beginner. In second part, I will share useful tips/tricks.
Steps to Learn Vim
- ✅ Learn how to exit Vim.
- ✅ Go through
:vimtutor
(:Tutor
). - ✅ Learn the most basic commands to get started:
i (insert mode)
Esc (normal mode/ exit insert mode)
x (delete one character)
h/j/k/l (direction. Personally I would avoid arrow keys from the beginning)
:e {filename} (load document)
:w {filename} (save document)
:q (exit)
:q! (exit + ignore unsaved changes)
Next, learn slightly more advanced commands:
dd (cut a line)
yy (copy a line)
p (paste)
/{pattern} (search for text)
n (go to the next result)
:s/pattern/replacement (replace text)
-
✅ Learn Vim Grammar to "talk" to Vim.
-
✅ Learn navigation.
:h motion.txt
/:h navigation
- Moving Around
- 8 Essential Vim Editor Navigation Fundamentals
- Moving Around in Vim
-
✅ Learn Vim modes.
:h vim-modes-intro
- Modeless vs Modal Editors
- Introduction to Vim Modes
-
✅ Learn text object.
:h text-objects
-
✅ Learn how to modify vimrc.
-
✅ Learn basic Command-line mode (ex) commands (first, learn search/replace).
-
✅ Learn the dot command.
-
✅ Learn macros.
-
✅ Learn something new everyday and apply it.
-
✅ Read (and reread) Practical Vim and/or Mastering Vim Quickly.
-
✅ Do Vimgolf.
-
✅ Perpetual self learning.
These should be enough to get you started. There are many more features that I do not list above. You will learn them over time.
Do not feel like you have to know 100% of Vim features. You can get very fast and productive with limited knowledge. There may even be popular features that you will never use. Use whatever features and workflow that helps you most.
Tips/Tricks to learn Vim
- Learning Vim is learning a practical skill. You don't learn how to ride a bike by reading a book on how to ride a bike. You learn how to ride a bike by riding a bike. You are building muscle memory. The goal is to be able to do X without thinking. Learn only a few things each day and learn them well.
- Don't copy paste someone else's
vimrc
. It will cloud you from seeing Vim's native ability. Start with blankvimrc
. Know what every single line does. You will be surprised that you may not need that many plugins. - Learn touch typing.
- When starting, force yourself to use
hjkl
. You can either add this in your vimrc:
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
or install vim-hardtime plugin.
Vim Resources
Below, I compiled some of the popular resources to learn Vim.
Books
Books I found extremely useful:
- Practical Vim
- Mastering Vim
- A Byte of Vim
- More list of books here
Articles
"Must-read" Vim articles:
- Learn vim For the Last Time: A Tutorial and Primer
- Grokking Vi
- Why I love Vim: It’s the lesser-known features that make it so amazing
- operator, the true power of Vim
- Coming Home to Vim
- Vim Anti-Patterns
Misc Online Resources
Other online resources:
- Vim reddit for vim discussions/tips/Q&A
- Vimcasts for tutorials
- Vimgolf for training
- OpenVim
- Vim adventures
- Derek Wyatt Vim tutorials
- Learn Vimscript the Hard Way
List of Plugins
Once you get comfortable, feel free to add plugins. Here is a (opinionated) list of plugins to make Vim more "IDE"-like:
- Quick quote/parentheses: vim-surround
- Search: fzf.vim
- No-nonsense config: vim-sensible
- NERDTree: nerdtree (alternative: netrw, or its enhancement vim-vinegar, ranger)
- Emmet: emmet-vim
- tags: vim-gutentags
- autocompletion: (youcompleteme, coc.nvim, deoplete)
- language pack:vim-polyglot
- tmux-integration: vim-tmux-navigator
- commenting: vim-commentary, tcomment
- LSP support/ syntax: ale
- Git: vim-fugitive (also vim-gitgutter)
- Indent: indentline
- Text-object: vim-textobj-user
How to use :help
At some point, you will need to start using Vim's built-in :help
on your quest to master Vim.
Knowing how to use Vim's built-in help is useful.
Autocomplete
Vim help
lets you autocomplete with tab. If you want to learn more about Vim colorscheme
, you can type :h color
and press <TAB>
. Vim will show you different options to choose from. You can navigate forward with <TAB>
and back with <SHIFT> + <TAB>
.
Jump to definition
While you are in help, sometimes you will see links (it has different color). If you put your cursor on link, you can jump to definition with C-]
.
You can go toggle previous/current window with C-o
and C-i
.
helpgrep
Vim has a lesser-known feature called :helpgrep
. It does what it sounds like: grepping help. It search for phrases in all help section.
For example, if you want to look at everything related to "colorscheme", you can do :helpgrep colorscheme"
. Vim searches for all "colorscheme" keywords and list them in Quickfix window (:h quickfix
).
Some useful navigation:
- List all helpgrep results with
:clist
- Go to next match with
:cnext
- Go to previous match with
:cprev
Conclusion
Is 2020 too late to learn Vim? No.
Do you have to learn Vim to be a better programmer? Absolutely not. I've seen much better programmer than me who does not use Vim.
Is Vim challenging? It takes a lifetime to become a Vim master (I am also still a Vim student). Vim is unlike other editors I have ever used. There is always something to be discovered. Vim is challenging. The fun kind of challenge.
In the end - I will repeat this again - use whatever best tool for the job. Maybe it is Vim for you, maybe it is VSCode, or maybe it is Notepad++ - or maybe it is all of them. It is good to be well-versed with multiple tools.
Let's get things done.