Featured image of post Beginner's Guide to Vim

Beginner's Guide to Vim

A basic guide to using Vim for beginners.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Enter insert mode (Adding text) - i 
Replace text - r
Escape insert mode and enter command mode (Panic button!) - Esc

Exit! (when you haven't changed a thing) - :q
Exit! (when you have changed something and don't want to save) - :q!
Save (but carry on editing) - :w
Save and exit - :wq

Search for text - /searchtermhere
After hitting enter, find next occurrence of search term - n
Find next search term up instead of down - N

Undo - u
Redo - Ctrl+r

Select command (will replace when pasting clipboard contents) - v
Select line command - Shift + v
Copy text - y
Copy whole line - yy
Paste - p

Carriage return (Enters insert mode after pressing) - o

Delete character - x
Delete/Cut line - dd
Delete from cursor to end of word - dw
Delete all text - :1,

Move to end of file - G
Move to start of file - gg
Cursor to end of line - $ or End
Cursor to start of line - 0 or Home

(If you have opened multiple files in a folder i.e. "$ vi *")
Move forward a file - :n
Move backward a file - :N
Built with Hugo
Theme Stack designed by Jimmy