From the course: Linux Tips

Working with text: Vim 101 - Linux Tutorial

From the course: Linux Tips

Working with text: Vim 101

- [Instructor] Vim or vi improved, is a text editor that's common across Linux distros. It can take a little bit of getting used to but many developers, administrators, and other Linux users swear by it and use it very efficiently. One of the primary reasons that new users find Vim hard to understand is the fact that it has different modes, including normal mode and insert mode, rather than just one constant way of working like most other editors. Vim's normal mode is where we issue commands to the editor. Things like saving the file, opening other files, navigation and editing tasks are handled in this mode. Insert mode is where we're adding or changing text in a document. There are some other modes as well, but in this episode we won't focus on them. Opening the editor, we start out in normal mode and to get to insert mode, we can press one of a few keys. A, C, I, O or S. These each move into insertion mode in different ways. To switch to normal mode, we press Escape. These are important keys to remember. My Ubuntu system comes with a minimal version of Vim. To make sure that I have the full version I'll write apt install vim. (computer keys clicking) Then I'll open up Vim here on my system by typing Vim. I can also type VI which is a symlink to the same software. And I'm welcomed with the screen. It tells me about the version, some other information and reminds me how to quit software and how to get help. This notation, with the colon and a command, is something you'll type in normal mode to execute a command. Before we do that, let's press one of the keys to get us into insert mode. I prefer to use I because it reminds me of insert and also because it puts the insertion carrot, the place for the next letter typed will appear, directly in front of the selected character. A will put the insertion carrot after the highlighted character. S will delete the character and put the insertion point where was. O will open up a new line below the current line, for typing. And C will erase the line and put the insertion carrot at the beginning of it. But let's stick with I for now. Now I can write some text, I'll add a sentence here. (computer keys clicking) And yes, I see those typos, we'll take care of them in a moment. Here in this version of Vim, I can move around with the arrow keys, but not all versions of Vim allow that. So, let's use the standard Vim navigation keys instead. These are H, J, K and L. In normal mode, H moves left, J moves down, K moves up and L moves right. I'll press Escape to get to normal mode and then I'll press H until I get to my nearest typo here. I'll position the cursor right after the Z, press I to insert a character and type y. And then I'll press Escape to go back to normal mode. Let's keep moving left to the nearest typo. Here, I'll position the cursor over r, use I to insert and then type e. Escape, moving left with H and here I have a duplicate character. I can either enter insertion mode and backspace it or press X in normal mode to delete it. Now let's head over to the first word. (computer keys clicking) I'll position my character on the w and press R for replace and then I'll type e. That's just replaced the letter without leaving me in insertion mode. This way of working with text can take some getting used to. But many people find it a lot more efficient than other ways, once they practiced and used it a bit. They are short cuts to move by word, by line and more. This is just a quick introduction to the basics and there's a lot more to learn about Vim. Check out our course, Learning vi, for a deeper dive. The help documentation in Vim is very extensive and I encourage you to explore it or, at least, become comfortable moving around it. Let's take a look here. To get to the documentation, I'll move into normal mode with escape and then type :help, and press Enter. That brings down this help window at the top of the screen. At the very beginning reminds us how we can move with the right home row keys and how to dismiss the help window or quit Vim. What will be most helpful here in the help is navigating with tags. I'll move down a little bit with J. Using tags is just like clicking on a hyperlink on a web page. We can move the cursor to a tag and press Control + ]. And we can use Control + T or Control + O to move back, like hitting the back button on a browser. This take some getting used to, but it'll help you navigate the documentation more effectively. I'll exit the Help by typing :Q. And then, I'll exit Vim without saving with :Q! (computer keys clicking) I'll leave you to explore the documentation and practice navigating Vim. But I do want to point you to helpful feature. Aside from having extensive built in documentation, Vim also includes Vim Tutor, an interactive guide to getting you started with the software. It's worth learning how to work with Vim in case you need to use it someday. As you use it more, you may join the ranks of loyal Vim users around the world.

Contents