Vim basics for people like me 
I have been using vim for almost 5 years, but somehow I have managed using it with the very few commands that I know. I recently saw a video of person increasing their productivity by switching to neovim and using custom configs to make neovim/vim work better and look better. I was reminded of the friend from years ago who had insane vim skills and I wanted to learn that wizardry. Safe to say I have not really ventured beyond the dd
and yy
and $
and some handy visual block mode edits.
Today I will try to rectify that by logging some of the basic vim commands and how to use them.
Vim as an editor
Vim or Vi improved is a basic text editor. It’s sole job is to edit text material. The reason why I started using vim from Atom as a text editor was because of the large file sizes I was dealing with. If you try opening a large text file in Atom or Word or a table in Excel, the program will load but excruciatingly slowly. In the old days when I had a core i3 chip my computer would hang up 😢.
The reason why command line based text editor like vim and nano can easily open large files is because they stream only parts of the file at a time, never the whole file. And since they are run from the command line you need a terminal emulator like “Terminal” or “iTerm” to actually use vim or nano. The reason why many people stick to vim is also because terminal and vim are always installed on any and every OS, which reduces the time needed to install if switching between multiple devices.
Vim has three modes- the insert mode (when we type or edit the actual text), the command mode (where we use keybindings to work faster) and visual mode (where we select longer strecthes of text “visually”).
To start off we need to open Vim using the vi
or vim
if using the vanilla vim isntall or nvim
if using neovim instead. This opens the default vim message, but instead we can create a test file open it and type in it by using vi
followed by the file name we want vi test.txt
p.
$ vi test.txt
When the file opens it is always in the command mode, which is where most of the vim commands/keybindings come in and what makes vim such a fast editing experience. However, currently the file is empty with nothing to edit so let first add some text. For this example I went to the Lorem Ipsum generator and copied the text. To add any text we need to shift to the insert mode by pressing the letter i
. Now we can paste the text as normal into the file with Ctrl+V/Cmd+V
. To save the changes we made we need to exit the insert mode by pressing the esc
Escape key, which always takes us back to the command mode. In the command mode we can make motions by just pressing certain key/s combinations, and we can execute commands by first pressing:
the colon character, now vim will accept commands, and type w
to save the changes or wq
to save and exit or q!
to close and exit the without saving the changes and hit Enter.
With the very basics of creating a file, adding text and exiting vim out of the way, we can move on to a few more basic commands which are easy to remember and I believe might even be all the vim commands one needs to comfortably work like any other text editor.
How to move in vim
Vim motions are keystrokes/keybindings that help you move across the file without having to use the mouse and much faster too. All motions are done in the command mode so make sure to hit the esc
before any motion. Since for editors like vim the goal is to ditch the graphical pointer all together, we must use the arrow keys to move within the file text. At first this may seem counterintuitive to imcreasing productivity since moving one character at a time with the arrow keys is slower than the mouse. But we will not be moving one character at a time, because we will use vim motions to jump to our desired location. But first, try moving with the arrow keys, left, down, up, right. One thing to notice is that the text is not a stretch of strings nut treated as characters on a grid. And if you look back at the previous gif you will see that at the bottom of the file I have a custom seting which shows me my current Line Line number, Char Character number and Col Column number. You can also see the line number at the start of every line on the left. In the default setting the bottom line only shows the file name and the number of lines in the file and the file size. These numbers will be extremely useful later so can add a few settings to make the line number and column number visible without using any bulky cosfiguration files. First we must make sure we are in the command mode by pressing esc
and then press colon followed by the command to set the line number as such- :set number
and to make the column number visible :set ruler
. One more thing about moving within the file, we can use the keys h, j k l to move left, down, up and right, respectively. Vim not only wants to make the mouse obsolete but the arrow keys too! They are the alternative to the W A S D keys used in games, all so that the user doesn’t have to drift away from the main section of the physical keyboard. Not necessary to use, but good to know.
With the help of the line number and the column number now we have the power to jump to desired location on this “grid”. Again, hit esc
to make sure we are in the command mode and use the format :LINENUMBER
jump to LINENUMBER. Similarly, we use COLNUM|
where the column number followed by the vertical bar takes us to that column in the current line. Note here we do not use the colon, just straight type the command. A few other motions I use all the time are -
-
0
: to jump to the front of the current line -
$
: to jump to the end of the current line -
gg
: to jump to the top of the file/first line -
G
: to jump to the bottom of the file/last line
Enter into different edit types
So far we have seen only one way to enter the edit mode with the i
keystroke, there are other slightly different ways to do this-
-
i
: Enters insert mode at current position. -
a
: I call this the append option, you will enter insert mode but right after the current position. Whereas,i
enters at the same position. -
o
: This moves over to a new line below. -
x
: Deletes the current character. -
r
: Replaces the current character with the character you type next.
-
I
: Jumps to the front of the current line and enters insert mode. -
A
: Jumps to the end of the current line and enters insert mode. -
O
: This moves over to a new line above.
-
u
: Undo’s last edit. -
Ctrl+r
: Redacts last undo. -
dd
: Copy and then delete current line along with the whitespace. -
cc
: Copy and then delete current line but keep the whitespace. -
yy
: “Yank” or copy current line. -
p
: Paste the yanked/copied line below.
Other motions and keybindings combined with repeats
Other commands which I wasn’t using until recently are the next “word” and previous “word” motions. Here, the selections may not always be words but chunks spearated from each other by space or - like separators.
-
w
: move ahead one “word”. -
b
: move back one “word”.
These two also have variations (W, B), which I don’t think are really needed for newbiews. The useful thing about all the commands are that they can be combined with a REPEATNUMBER which tells vim how many times to repeat a command. So if I want to move ahead by 3 words I can just type 3w
and to move back by 5 words 5b
.
Search and replace
To search for a string in the file you just need to type /STRING
where yoou replace the STRING with what you are trying to find. To move on to the next occurrence of the string just press n
and to go back to the previous occurrence of the string press N
.
To search and replace a string pattern, we use the sed command in vim- :%s/FINDSTRING/NEWSTRING/
this replaces the first occurrence of the string for every line. To replace every occurrence of the string in all the lines we add the global flag - :%s/FINDSTRING/NEWSTRING/g
.
Conclusion
These are the basic vim commands and motions which are good enough for most basic users. I will introduce the visual mode next, since I use it a lot to make large selections. Along with macros which I have started using now and a little on vim configurations since the line number settings don’t stay on exiting without changes in th config file. Maybe I will add a walkthrough video later.
References and disclaimer
- I found this Youtube video very helpful, concise and covers the most useful basic commands Youtube.
- This cheat sheet is comprehensive list of all commands but I will not be using many of them here Vim cheat sheet.
- And this is the Markdown cheat sheet Markdown cheat sheet
- I also used ChatGPT to get a list of basic vim commands.