Perform basic file editing operations using vi
Start course
Difficulty
Intermediate
Duration
1h
Students
2223
Ratings
4.9/5
starstarstarstarstar-half
Description

This course will focus on the Linux command line and the text-manipulation tools that let you effectively control just about anything on your system. We'll learn about terminal environments, working with text streams, file management and archives, system processes, advanced text searches, and terminal text editors.

The previous course covered installation package management, while "Filesystems and Partitions" is up next. 

Here's the complete first set of Linux certification courses.

If you have thoughts or suggestions for this course, please contact Cloud Academy at support@cloudacademy.com.

Transcript

Since text files are the core of everything that happens in Linux, and since Linux administrators and developers spend so much of their time working with text, you can easily see how choosing a text editing tool is a big decision. This is something with which you'll probably end up spending more time than with your closest family members, for better or for worse. Linux has a great range of editors freely available to suit every need. Of course, since you'll need to produce clean plain text, you're not likely to settle for a full-featured office suite like LibreOffice.

Once that's off the table, you could always go with a GUI package like Gedit, which includes a great library of built-in formatting tools for bash or all major programming languages. However, since so much of Linux administration takes place in the terminal, most admins prefer a terminal-based text editor. As you might already have noticed, my favorite is nano because of its balance of standard navigation tools and quick keystroke shortcuts like CTRL+X to save and exit, and CTRL+K to delete an entire line.

But admins and developers with very deep experience will often look sadly at people like me who insist on using nano or Gedit, as though we're part of some unfortunate lower level of humanity. These are VI or VIM users, and having tasted a bit of the power of their chosen text editors, I can certainly appreciate why they think this way. The LPIC exam does too, as they've made it an important part of their exam requirements. But first, a little bit of server history. Once upon a time, hardware was much simpler, and system memory was much less available. It wasn't uncommon for the keyboards attached to servers to have pretty much nothing more than the alphabet and numbers. There were no arrow or page up and page down keys, and certainly no function keys. And even if there were, not a lot of operating systems would have known what to do with them. Since there was so little to work with, software interfaces had to be creative and resourceful. Thus, was VI born.

Understanding VI modes and keystroke commands

It's a text editor that never asks your fingers to leave the main keys. And don't even think about using a mouse. There are, therefore, keystroke sequences for doing absolutely everything. By the way, these days, you're much more likely to encounter VIM, which stands for VI Improved, which incorporates the functionality of the original VI, along with some generous nods to modern computing habits. In any case, this style of commands and operations that came to exist from necessity had the fortunate benefit of also being incredibly efficient and fast. Someone who's invested the time and energy to familiarize themself with the workings of VI will almost certainly be able to outperform people on similar tasks using other tools.

But here is the problem, if VI operations require the use of regular alphanumeric keys, then how are you supposed to use those keys to actually enter regular text? To make this possible, VI works in multiple modes.

Normal or command mode, is the mode that by default you will see when you start up VIM. Normal mode doesn't allow you to actually edit text, but it does let you quickly perform just about any other task.

From normal mode, hitting the uppercase I or insert key will take you to insert mode, where you'll do most of your typing. You can add, change and delete text, and even navigate through the page using arrow keys. But you can't launch commands. Command line mode, which is where you perform file management tasks and when necessary, exit VI, is accessed by typing a colon followed by your command.

Let's try it out. From the command line we'll open VI, using either vi, or vim on my Ubuntu system, and the name of a file. Since we are by default in command mode, we can't actually edit the text. I'll hit the insert key and enter insert mode and add a few words. Notice how I can move around the text using the arrow keys. If I hit the ESC key, I'll move back to command mode. Even if I can't edit the text directly in command mode, the position of the cursor is important, as we'll soon see.

You can now move around using the keys between h, and l. H will move one position to the left, j will go down one line, k goes up a line, and l moves one position to the right. The o key will move your cursor back to the start of the line you're on. Now, based on the current cursor position, here's how you use the command line to edit. dw will delete the word to the right of the cursor. U will undo your last operation. D$ will delete everything from the cursor to the end of the line. Dd will delete the entire line.

P for paste, will take the text that was most recently deleted and paste it at the current cursor position. P will also paste text copied using yy, which copies the entire current line. / will take the next text you write and search a document for it, while N will repeat the previous search. Uppercase ZZ will save the current file and exit VI. Using command line mode, as you'll remember always prefaced by a colon, w and enter will save your file, w a space and a new name followed by enter will save the file with it's new name. W! overwrites the current file. Exit, or wq will exit VI, but only if the file hasn't been changed, and wq! will exit without saving the file.

Let's review. There are three modes to VI. Command mode, the default, allows you to edit your text, but not directly. Insert mode, reached through the uppercase I or insert keys, allows you to navigate and edit text directly. Command line mode permits file management operations. Command line operations include :w to save a file, exit or wq to exit VI, and q closes VI without saving. In command mode, dw deletes the word to the right of the cursor, d$ deletes to the end of the line, and dd deletes the entire line. P inserts recently deleted text to the current position, while u undoes the last action. Yy copies the current line, and uppercase ZZ saves and edits. Finally, h moves one position to the left, l one position right, j moves down, and k moves up.

To be honest, you can review these commands all you like and that will get you through the exam. But to really begin to master VI or VIM probably requires at least a week of actually working with it on a real project. While I can't say that I've made it to that level, I'm still a solid nano kind of guy, I strongly suspect that you'll never regret the investment.

About the Author
Students
16155
Courses
12
Learning Paths
5

David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.

Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.

Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.

His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.

Covered Topics