Vi is a very powerful and easy to use built-in Linux text editor. In fact, once you master it, vi is the only text editing tool you need to know. Many say it’s hard to use, dull and boring but it’s all gibberish! I am writing this post to change your views about vi 🙂
Vi or vim (vi enhanced) are text editors available in Linux distributions by default. It would be quite unfair to simply not be bestowed with their advantages.
If you are used to downloading the file, making changes using a GUI, and then uploading the file back to server, then please stop :p You’ll stop doing this once you get to know vi. Besides, these steps are unnecessary and kafkaesque*, right? This also puts the file in danger by adding extra whitespace which might soon turn into an untrackable production nightmare.
* Nerd fact – Kafkaesque meaning: something that has been made complex illogically and in a bizarre way.
Why learn vi?
- Vi editor is available by default on all Linux distros. You can start working on it right-away.
- Some other Linux utilities, for example, editing cron jobs, work in the same pattern as vi.
- No internet, no mercy. Let’s say you are used to working in other (non-default) editing tools but suddenly if you lose access to those tools, and there is no internet access to install them, then you know where you stand. This could happen if you are trouble shooting in someone else’s evironment.
- Most servers run headless, so in system administration, you don’t necessarily have the luxury of a GUI. But vi has got your back; it would always be there.
- As vi is a CLI tool, it is faster than GUI tools.
- Vi keeps the file properties intact. Downloading the file, editing and uploading it increases the chance of whitespace addition. This could be the case with third party text editing tools as well.
- Vi is suitable for all– beginners and advanced users. Vi supports complex string searches, highlighting searches and much more. Some developers even use vi for code editing, sounds strange right? But you can see the potential here 🙂
How to use vi ?
It’s easy. You need to know the 3 operating modes of Vi and how to switch between them. Keystrokes behave differently in each command mode, and that’s the magic of vi 😀 Let’s dive deeper.
In vi, there are three modes to work with.
When starting vi editor, you land in the command mode by default. This mode allows you to access other modes.
To switch to other modes, you need to be present in the command mode first
This mode allows you to make changes to the file. To enter edit mode, press ‘I’ while in command mode.
Note the ‘– INSERT’ switch at the end of the screen.
This mode allows you to work on a single character, a block of text, or lines of text.
Let’s break it down into simple steps. Remember, use the below combinations when in command mode.
- Shift + V → Select multiple lines.
- Ctrl + V → Block mode
- V → Charatcer mode
The visual mode comes into handy when you need to copy and paste or edit lines in bulk.
Extended command mode.
The extended command mode allows you to perform advanced operations like searching, setting line numbers, and highlighting text. We’ll cover extended mode in the next section.
How to stay on track?
If you forget your current mode, just press ESC twice and you would be back in Command Mode.
Some common operations using vi
1. Copying/ Pasting in Vi
Copy-paste is known as ‘yank’ and ‘put’ in Linux terms.
To copy-paste, follow these steps:
- Select text in visual mode.
- Press ‘y’ to copy/ yank.
- Move your cursor to the required position and press ‘p’.
2. Searching Text
Any series of strings can be searched with vi using the ‘/’ in command mode.
To search, use /string-to-match. Let’s see an example.
The below example finds the word ‘stars’
Let’s highlight all of the searches. Now we’ll make use of the extended command mode.
In the command mode, type “:set hls“. This will highlight the searches.
To ignore case, use “:set ic“
3. Saving files and Exiting vim
First, move to command mode and then use these flags:
Exit without saving → q!
Exit and save → wq!
Using hjkl for navigation
In early days of CLI, the keyboards didn’t have arrow keys. Hence, navigation was done using the set of available keys, hjkl being one of them.
Although arrow keys would work totally fine, you can still experiment with hjkl keys to navigate.
Tip to remember: hang back, jump down, kick up, leap forward.
Time for action !! 😀
Remember learning in IT is nothing without hands-on. To quickly access a Linux shell and practice what you have learned, hop on to replit! It’s free! This tutorial was made using replit.
So, this is it ! You have mastered te basics. With this tutorial, you can easily view, edit and save your files using the vi editor.
About the Author
Hi there, I’m Zaira! I’m an IT professional with diverse experience in Linux-based application management. I keep applications up and running for businesses. I’m currently exploring cloud technologies. My message is simple and straightforward: ‘Always keep exploring and learning. IT is beautiful and almost like magic, you’ll never get bored and always find something surprising. So, keep learning 😊.
If you need any tech-related help, you can connect here: https://www.linkedin.com/in/zaira-hira/