1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Red Hat Enterprise Linux Technical Overview

Editing Files using Vim

Developed with
Red Hat
Start course
1h 43m

Red Hat Enterprise Linux (RHEL) is a rock solid commercial grade Linux operating system. If you're interested in learning RHEL from a system admins perspective then this course is for you!

The "Red Hat Enterprise Linux Technical Overview" course walks you through many of the basic system admin tasks and concepts required to administer RHEL effectively.

This course will provide you with insights to:

  • Working with the Terminal
  • Understanding the Kernel and User Spaces
  • Graphical User Interface
  • File management and the File System Hierarchy
  • Editing Files using Vim
  • Organizing Users and Groups
  • File Permissions
  • Managing Software
  • Configuring Networking
  • Controlling System Startup Processes
  • Introduction to Containers
  • Overview of Cockpit

Hey guys, one of the most important skills that you need to have when working on a Linux system is how to make use of a text editor. And we have a text editor in the form of Vim. It's really powerful and it can be quite tricky and intimidating at first. So, first up I'm going to type in vim its own and you may notice that there are no menus over here.

So if you're trying to think that I can click on file over here, nope, that's not the menu for Vim. You see when you make use of Vim it operates in different modes. The mode that we are looking at right now is called command mode and this is the default mode. Now when you're on command mode this is when you can make use of special Vim commands and

I'll teach you a little bit about that in a moment. Now, if you wanted to edit a file, you wanted to edit text, you need to be inside insert mode.

So to change into insert mode you would use i on the keyboard.

Now look at the bottom left hand corner it doesn't say anything about me being in, in insert mode right now so I'm going to go ahead and hit i and you'll see right now I am indeed in insert mode. So let's go and type in some text. Say, Hello, world!

And there we go. That's my text. Now I want to save it. Now we're in insert mode and we need to change modes right now in order to save the file. So to get out of insert mode, we hit escape on the keyboard, and you can see at the bottom left hand corner, it doesn't say anything about me being in insert mode anymore. Now at this stage I need to go and change modes one more time. I'm inside command mode and I need to go into extended command mode. To change into extended command mode, I'm going to use a colon (:). So again have a look at the bottom left corner and at this stage I'm going to give it an extended command and the command is write because this file doesn't yet have a name. So now we need to go and give it a name and it's going to be called foo. So I'm writing foo and I'm referring to a to a relative path. 

So I'm writing foo to my current directory and at the bottom left hand corner you could see foo, it's a new file and it has been written right now. Now the next important skill that you need to be mindful of inside of Vim, is how to get out of Vim right now. So you've made your change, you've edited a file, and you need to exit right now. So the command is really easy. You would use colon (:) to go into extended command mode and you would make use of the command quit. Very often these commands may be abbreviated. So let's go and try that one more time.

So if you look inside the current directory I've got a file called foo and if you wanted to edit foo directly what you could do is use vim foo. So vim is the command, foo is the argument. and you can see right now that I've got my text Hello, world! that I've previously saved.

Now the command mode is very powerful because what I could do is that I can make use of certain vim specific commands, for example, I could say yy which is to yank a line or to copy a line.

I could then make use of p to paste it. Now it's not a literal P, I'm not putting p into the document, I'm simply pasting a word. Now I can also say something like 10p. And I could paste the line ten times. In command mode what I could do, is I could go and ohh how about this? I could go and delete a word.

So I'm just going to go and delete the word world so I'm putting my cursor over the w in world and I could say dw and you can see that I've deleted a word. I could move my cursor over to any character and hit x to delete it. So again these are not literal Xs and they are interpreted as vim specific commands.

So, so far I've spoken about how to copy and paste files but or how to copy and paste lines but how do you go about deleting lines so I could say dd. To delete a line.

Let's go and dd one more. And similarly to pasting a line multiple times what I could now do is that I could delete n number of lines. So let's go and delete the next 9 lines. And you can see right now nice and simple those lines have been deleted and I only now have 2 iterations of of Hello, world!

Now again if I wanted to go back into insert mode to inject text, I could hit I on the keyboard. Now what would happen at this stage is that it would take me into insert mode but now I would be editing text on the same line where my cursor is. And that's maybe not what I want to do. I want to put in a brand new line into this file. So, what I'm going to do is I'm going to get out of insert mode right now and I'm going to show you the o command. The o command will take you into insert mode however it's going to put your cursor on a blank line, on a new line and you can start editing text right away. And I can put in a brand new line that says, This is RH024. And now again I want to I want to save this file.

So let's go to hit escape and we are hitting escape to exit out of insert mode and we are now back into command mode. And I want to show you something that's really cool. I'm now going to save and quit Vim, without going into extended command mode and how I can do that is by using an uppercase ZZ.

So, shift ZZ on my keyboard and there you go. It's the same as save and quit and the reason why I was able to do that is because the file already had a name.

So, let's get back into the file foo and we're going to delete that duplicate iteration of hello world. So let's go and try that. So I've deleted the line and at this stage what I want to do is that I want to quit it. So, I've realized that I made a mistake,

I just want to get out of this right now, I want to abort editing this file in you know in all ways.

And I'm going to say escape colon and we're now going to extended command mode and I'm going to use the abbreviated form of quit which is q.

So this stage guys is saying No write since the last change add a ! or an exclamation mark to override so let's go and try that right now, q yes I'm really sure, this is what I wanted to do and now it quits. So guys that is the basic vim workflow insert mode to manipulate text, we have command mode where you can make use of special vim commands like yy to copy, p to paste, dd to delete there are many more as well and then I also showed you extended command mode where we could do things like use the w or the write command to give a file a name, we can also make use of quit and abort without saving any changes.

And I just want to encourage you that there's so much more to vim. It really is a powerful text editor and it's an essential skill that every Linux administrator should have.

Now one cool thing that I'm going to leave you with right now is the vimtutor. It's a true active tutorial on how to make use of Vim.

So on pretty much any Linux system and especially Red Hat Enterprise Linux type in vim tutor and you can start immediately at lesson 1-1 which will teach you about how to move around.

It's going to start with the very basics once again and then it's going to end up with some of the more complex lessons when it comes to using Vim. Now with that guys at this stage we are going to wrap up I will see you in the next video.

About the Author
Jeremy Cook
DevOps Content Lead
Learning Paths

Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.

Jeremy holds professional certifications for both the AWS and GCP cloud platforms.

Covered Topics