A Beginner’s Guide To Vim
OK. Vim is my favourite text editor. I use IDEs in my job like VS Code but I often end up just using Vim in the command line as it’s where I’m most happy and everybody tells me that the Linux hipsters all use Vim exclusively and I want to join that club desperately.
Jokes aside though I really do love Vim. BUT I’ve grown to love it. I think like everybody who’s followed a demo online where they’ve instructed you to type in
$ vi thisfile in the Terminal, have quickly found yourself Googling for
How to exit Vim.
So here is my entry level cheat sheet on using Vim to edit text files. It does so much more than just editing config files but I wanted to keep all the basic commands in one place for you (and myself probably) to check into now and again.
First of all make sure you have
Vi Improved actually installed as some distros might have the older and simpler but not wiser version installed. Merely type
sudo apt install vim -y (Ubuntu/Zorin/Mint/Pop etc) or
sudo dnf install vim -y (Fedora/RHEL/Centos/Rocky) to ensure you have the up to date software. So far, so easy.
Let’s say you need to create a brand new file in the folder you’re currently sat in called
test.txt. Just type
vi test.txt or
vim test.txt. Either will work just fine. I tend to write
vi. Now you are faced with a blank screen with tildes (
~) floating down the left hand side. Cool. Now what? Well you should be in typing mode AKA
Insert Mode as displayed at the bottom of the screen. Vim has two main modes. Insert mode, that’s typing mode, and Command mode which is a mode where you can do things in the built-in command line at the bottom of the screen. Type a load of nonsense. Now you want to save this nonsense to the file
test.txt and quit altogether. Press
Esc to go to the command mode we mentioned before and type
:wq. Yes you need to type the colon too. The
w is for
write or save and the
q is for….. yes you guessed it
quit. Now do a quick
ls to see if your file exists. Lo and behold you are a technical genius and will be forever known as Hackerman by your close friends and family. But that’s not all! Here are some useful commands for when you are editing files in Vim. N.B. Please note the colons aren’t to look pretty, you really do need to type a colon. P.S. Why is it called a colon? We need to change that.