Vim or vi is a commandline text editor. And let's just say, it can do a LOT.
Too much, some would say. As the old joke goes, you can lean on your keyboard while using the darn thing and whatever you land on, it actually means something.
I don't personally embrace vim as my text editor of choice.
Still, having even only a moderately powerful text editor in your commandline is very beneficial. Whether you want to work with git or have to ssh onto a server and edit something, even working with your database can become easier in certain circumstances.
This article is not a complete tutorial on vim
or even vi
. There's plenty of "cheat sheets" online already, after all.
But if you've ever looked at some of them, they may look quite intimidating and overwhelming.
The goal of this article is to teach you some very basic stuff. Enough so you can work with vim and maybe feel less overwhelmed if and when you do want to look up how to do more advanced stuff.
VI vs VIM
For the purpose of this article, there's no difference between vi
and vim
.
vim
stands for "vi
improved", adding a lot of functionality on top of vi
like code highlighting etc, that are irrelevant to us here who only want to know the bare minimum to get by.
For the rest of the article, I'm going to use vim
because I do expect most of your machines to come with that, but if your environment only has vi
, use that.
Working with vim
Vim Modes
I promise Fig.1 looks scarier than it is.
In a nutshell, vim has three modes:
- a normal mode, where you can enter instructions by pressing shortcuts
- an insert mode, where you can actually type into the file
- a command mode, where you execute typed-out commands
When you first enter vim, it will be in "normal mode". This is NOT where you can type, and that's where most first-time users will get confused.
Another thing that may trip them up is that all instructions are case sensitive. a
and A
are two different commands, so are i
and I
, p
and P
, etc.
Moving the cursor
You can move your cursor around the file. The arrow keys are useful, but there's certain other combinations to jump to certain positions.
The most useful ones I think you should know are gg
and G
to jump to the first and last line of the document, respectively.
Typing
You get from normal to edit mode by pressing certain keys, for example i
to start typing wherever your cursor is, or A
to start typing at the end of the line on which your cursor is.
From edit mode, you then get back to normal mode by pressing ESC
.
Undo and redo
From normal mode
u
— undoCTRL+r
— redo
easy enough.
Saving
To go from normal mode to command mode, you press :
(and back to normal mode with ESC
or CTRL+c
).
In command mode, you type a command and then confirm with enter
.
The commands you need to know, are
:w
which saves the file but stays in vim:wq
which saves the file and exits vim:cq
which doesn't save the file and exits vim with an error
This last one is very useful for working with git, for example. Let's say you are interactively rebasing and you notice you forgot to do something, before you rebase. :cq
will take you out of vim with an error, and in so doing cancel the git rebase command.
Cutting and pasting
Technically, we could say that you know how to move around in the document, you know how to edit the document and you know how to save or discard the changes. That already qualifies as the "bare minimum".
Still, I cannot resist letting you know about my favourite feature in vim: cut-and-paste.
dd
will cut a linedw
will cut a wordp
andP
will then paste the cut content before/after the cursor again
Why do I like this feature so much?
I frequently rebase, when working with git, and I always do it interactively.
If I need to change the first word, pick
to an e
for edit, for example, I can do it by placing my cursor on that line and then going
dw
— to delete the wordpick
i
— to enter edit modee
— (including space) to put an edit instruction in front of the commit hashESC
— to go back to normal mode
it's very quick. And after a couple of uses, it comes quite naturally.
Or if I need to remove a commit from the rebase todo list, a simple
dd
— to cut the line
will do it.
If I want to re-arrange lines, I can then use p
or P
to add the line back in a place of my choosing.
Searching
Not really the "bare minimum", but also a very useful and basic feature is searching for stuff.
From normal mode, enter search by pressing /
.
Then enter what you want to search for and confirm with enter
.
Technically, you're still in normal mode, but now you can use n
and N
to jump to the next and previous occurrence of the search pattern, respectively.
By default, search is case-sensitive. Command
:set ic
— makes search ignore case:set noic
— makes search not ignore case
For the more advanced users, regex search is done by wrapping your regex into \v(...)
, e.g. to find a four-digit number, you would enter
/\v(\d{4})
More advanced stuff
And that's really already everything I think you need to know to be able to use vim as a basic text editor whenever you need to.
If you want to know more, there's of course plenty of resources online to learn more.
A (somewhat pricy) cheat sheet can be found on https://thingsfittogether.com/vim/ , if you're willing to invest in that, but that's far from the only resource and there are plenty free cheat sheets like this one, too: https://hamwaves.com/vim.tutorial/en/index.html