Which editor do you like best and use on a daily basis? If I have to say, Vim is a pretty great text editor for everyone who enjoys working with nix-based environments. However, learning to use it in an efficient way is a bit challenging. Although looking up commands from reference, documentation or even you have a cheat sheet beside, it is fairly hard to remember and figure out which commands are useful and used in a few contexts. Thus, I have summarised and written out some commands I think are popularly and widely used as well as fairly advanced on a daily basis. Don’t want to waste your valuable time, below are the list of commands.
Table of Contents
Editing
In Vim, you spend most of your time in “normal” mode, switching to “insert” mode only when you need to add or change some text. This way, all edits become their own self-contained operations that can be repeated or chained with other operations. Most editing commands may optionally be preceded by a number in order to apply it more than once (e.g. to delete three lines, press 3dd).
Delete the whole line
Press the cc to delete the entire line and switch to the edit mode.
Press the dd to delete the entire line and keep in the command mode.
Delete from the current position to the end of the current line
Press the key D (uppercase of letter d or d$) to delete the rest of the current line but keep you in the command mode.
Press the key C (uppercase of letter c or c$) to delete the rest of the current line but put you in the insert/edit mode.
Press the keys de to delete the word following the current cursor.
Press the keys dw to delete the word and whitespaces following the current cursor.
Copy text or lines
Press yy to copy the current line. The letter ‘y’ is short for ‘yank’.
Undo and redo
u and Ctrl + r are the undo and redo operations respectively.
Moving
Go to the beginning or end of the line
You can use A to move to the end of the line and switch to editing mode (Append). Pressing just the $ (dollar sign) key. To jump the last non-blank character, you can press g then _ keys. We could combine g_ or g$ to move to the end of the current line rapidly.
The opposite of A is I (Insert mode at the beginning of the line), as an aside. Pressing just the ^ (hat sign) will place your cursor at the first non-white-space character of the line. Most of the cases we have are to move the cursor to the beginning of the current line.
The distinction shows up in the end-of-line commands as well.
- $ and 0 moves to the end or beginning of the physical line or paragraph respectively.
- g$ and g0 move to the end or beginning of the screen line or paragraph respectively.
- fn + ← – move to the beginning line.
- fn + → – move to end line.
Jump to the beginning among paragraphs
If you are working with blocks of text, you might appreciate the command { and } in order to move a paragraph back and forward respectively.
Move to the beginning and the end of the document
gg and G move the cursor to the beginning and the end of the document respectively.
Selecting blocks
Select text and paragraphs
If you want to do the same thing to a collection of lines, like cut, copy, sort, or format, you first need to select the text. Get out of insert mode, hit one of the options below, and then move up or down a few lines. You should see the selected text highlighted.
V - selects entire lines v - selects range of text ctrl-v - selects columns gv - reselect block
After selecting the text, try d to delete, or y to copy, or :s/match/replace/, or :center, or !sort, or…
Here’s one way to move the selected text over a few spaces:
- select a chunk of code using capital V and the arrow keys (or j, k) - type colon - then type s/^/ / - hit return
What you’ve done is replace the beginning of each selected line (the ^ symbol means “the beginning of the line”) with spaces.
Select text in column mode
I have written a post about selecting text in column mode with VIM. Shift + V and proceed any command or Shift + I to switch to the edit mode.
Managing buffers, tabs and windows
Split screen
Like many other editors, you can split a window into small areas called buffers or screens or open multiple files into many tabs. I genuinely love this writing where you can find how to split, switch and resize windows.
Work files in multiple tabs
Opening files in multiple tabs:
vim -p /path/to/file1 /path/to/file2 /path/to/file3
To jump between the files you can use the following vim commands:
:n(ext) # jumps to the next file :prev # jumps to the previous file
Of course, you can also open a new tab when you’re already in Vim in the normal mode:
:tabe [/path/to/file] (command-line command)
References
- How do I move to end of line in Vim?
- How to Use the vi Editor*
- Everyday Vim – A Basic Vim Commands Cheat Sheet
- Vim tips and tricks
- How To Use Vim Split Screen
- Mastering Vim: Working with multiple files
There are many tutorials, cheatsheets and tips/tricks you can easily find in the huge data storages of the human being. Having said that, I hope this post has brought to you some ideas of how to improve your productivity on your regular work with VIM. All constructive comments should be welcomed. What are some other Vim commands that you are using every day?