This course will get you up to speed with the fundamentals of Linux and prepare you for further study. You'll learn how to navigate your way through the Linux directory structure and the permissions for doing so. We'll also cover files and the multitude of ways in which they can be created, managed, and deleted.
Gain a foundational understanding of how to create and modify files in Linux.
- Anyone with little to no knowledge of Linux who wants to learn more about the operating system
- Professionals who want to learn about Linux to enhance their career prospects
This is a beginner-level course so there are no prerequisites, but an interest in Linux and programming knowledge in general would be beneficial.
In this lesson, we'll be talking about how to use the vi editor. While nano is great for simple edits, Vi add Emacs have more advanced and powerful features. There'll be a learning curve to using these editors as they're not exactly intuitive. Learning vi will definitely be harder than, say, learning nano. It will require a bit of a time investment to become proficient.
To use the vi editor, simply type vi, space, and the file name that you want to edit. You can also use vim, space, file name. Vim stands for vi improved. It's compatible with all the commands found in vi, however, it has some additional features like syntax highlighting and the ability to edit files over the network.
On many Linux distributions, when you type vi, you are actually running vim or vi improved. If you wanna view a file but make sure you don't change any of its contents, you can use the view command. I'd like to point out that an advantage of using vi is that it's always available on a Linux system. Another advantage is that once you learn the key mappings for vi, you can apply them to other commands like man, or more, or less, or view, or you can even use vi mode for editing shell commands.
Vi has the concept of modes. You are always working in one of three modes. Command mode, insert mode, or line mode. When vi starts, replaced in the command mode. To get back to command mode at any time, simply hit the escape key. Letters typed while in command mode are not sent to the file, but they are interpreted as commands. Command mode allows you to navigate about the file, perform searches, delete text, copy text, and paste text..
Here's some of the commonly used key bindings for navigation in command mode. k goes up one line, j down, h left, l, right one character. To move right one word, w. b, moves back one word or left one word. The caret symbol takes you to the beginning of the line while the dollar symbol takes you to the end of the line. This graphic shows how the h, j, k, and l keys act as the arrow keys in vi. In order to insert text in a file, enter vi's insert mode. Do this by pressing lowercase i, uppercase I, lowercase a, or uppercase A. Lowercase i inserts text at the current cursor position. Capital I inserts text at the beginning of the line. A appends after the cursor position. Capital A appends to the end of the line.
To use Vi's line mode, begin a command with colon. For example, to write the file or save the file, type :w, enter. To force a file to be saved, type :w!. To quit, :q. To force quit, :q! to save the file and quit or write and quit, :wq!. And :x is the same as wq. Line mode can be used for navigation too. If you wanna go to line 15 in the file, type :15, enter. To go to the last line of the file, type :$. If you wanna see line numbers, type :set nu. And to turn off line numbering, :set space, nonu To get help, type :help and the command that you're interested in.
Again, the modes in the vi are command mode which you can enter by hitting the escape key. Insert mode by using I or A. And line mode by beginning commands with a colon character. You can repeat commands in vi by preceding them with a number. For instance, if you'd like to move the cursor up five lines, simply type 5k. If you'd like to insert a piece of text 80 times, type 80i, and start entering the text. Once you are done, you hit the escape key to return to command mode and the text you type will be repeated 80 times. This simple feature alone already makes it much more powerful than the nano editor.
While in command mode, you can use x to delete a character, dw to delete a word, dd to delete a line, and capital D to delete the remaining text on the line. To replace text, type r for just one character, cw to change an entire word, cc to change an entire line, c$ to change the text from the current position to the end of the line. You can also use capital C for that.
The tilde command reverses the case of the character. To yank or copy the current line, type yy. To Yank a position, type y and a position character. For instance, to yank a word, type yw. If you want to repeat this, you could yank three words with y3w. P will paste the most recently deleted or yanked text. The undo command in vi is u and the redo command is ctrl+R.
To start a forward search, type a forward slash and a search pattern and hit enter. To go to the next match, type n. To go to the previous match type capital N. To start a reverse search, use the question mark followed by a search pattern, and then hit enter. Let's use vi to edit a file. You'll notice at the bottom of the screen, you see a series of tildes. These represent lines that are beyond the file.
So we start out vi in command mode and we can use the j key to navigate down. K goes up. L to the right. H to the left. Caret symbol goes to the beginning of the line and the dollar sign goes to the end of the line. If we wanna enter insert mode, we can use lower case i to insert text at the current position. Escape will take us back to command mode. To insert text at the very beginning of the line, shift+I for capital I. To append, we'll just use a. And then to append to the end of the line is shift+A.
To delete some texts, we can use a x to delete a character. And if we want to delete a word, we can use dw. And dd will delete a line. And a capital D will delete from the current position to the end of the line. To replace text, we can use r. I'll replace this. Can replace an entire word with cw for change word. Escape to get back to command mode. cc to change the entire line. The tilde changes the case. So if it's lower case, it will become upper case. If it's upper case, it will become a lowercase.
To yank a line, yy. To paste, p. To do a forward search, type the forward slash and then lower case n to move to the next item. And upper case N to move to the previous item. To do a backward search, start with a question mark. And to write changes to a file, we can use w. Let's enter some more texts. And to quit, we can type q. Now that there are saved changes, it won't let us stop. So we'll just use q with an exclamation mark to force our quit.
Let's go back in. Let's use Let's turn on line numbering with set nu. And we can go to line 10 by typing :10, or line five, :5, enter. And we'll quit with q. There's also a tutor that's very nice. You can run vim tutor and follow along, and it will take you through some of the commands and features of vi. The vi editor editor is more powerful and advanced than nano. However, it's not as intuitive and it can take some time to learn. One of the unique things about vi is the concept of modes. You're either in command mode, insert mode, or line mode.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.