Welcome back. In this chapter, we'll look at creating, viewing, and modifying text files throughout the Linux operating system. By the end of this section, you should feel comfortable opening, editing, and saving the files you interact with throughout your operating system. As we begin to edit text files from the command prompt, we'll need to learn that there's a new way to edit files. Native to every Linux operating system is the editor vim or vi. Vim is the improved version of the original program vi and serves as a text-based file editor. As it's prevalent across all distributions, it's common to learn the skills used to wield vim to edit your files. It's not the only option available to Linux system administrators. However, it's the most commonly deployed and it has a bit of a learning curves. Let's take a look at how to use vim to edit text files throughout our operating system. Well, I've mentioned there's alternatives, why would you choose to learn vim? Well, I think the primary reason is that it's widely distributed across all Linux distributions. That means that when you connect to a Linux system, especially a remote system via SSH, you're likely to encounter the existence of vim and therefore, it would be a native toolkit across all distributions. Learning to use vim to edit the files and settings that you need to make changes across all the systems, equips a system administrator with a single text editor that they can rely on throughout all environments. Many Linux distributions only have the more simplified vi application installed. If you don't want to edit a file using vi, you would simply type the command vi followed by the filename. You can address this filename with absolute or relative pathing as we've seen in previous sections. Once you type the command vi to launch into the editor, followed by the filename, you'll now need to start using vi based commands in order to navigate and make the changes that you wish. vim, V-I-M, is a later version of the same application or vi improved. In more modern environments, you'll find that this is either an alternative or aliased into the command itself, meaning that when you type vi you may even be launching vi improved. Despite which when you're using, the command will be the same, the name of the editor followed by the filename you wish to edit. In this case, you can see an example of the command being used, "vim filename". This would open the given filename in the vim editor. Vim is known as what is called a modal editor, or that it has several different modes in which you can interact with the texts you are navigating through and editing. When you first open a file name with vim, you're simply displaying the contents of the file. It can navigate throughout them using your arrow keys. There are many shortcuts throughout vi that enhance your ability to navigate or edit the various texts that you wish to interact with. However, there's quite an extensive learning curve when you wish to expand your vocabulary there. Here we'll cover the basics of being able to open, edit, and save our files. The modes within vi allow us to interact with that text in several different ways. We can use the command mode to navigate or make command based changes to the text we see within the file. There's the extended command mode as well. The edit mode is probably the one most people require when they open a file using vim in order to make changes to the text they're seeing. Lastly is the visual mode. The visual mode allows you to highlight blocks of text and manipulate them based on the type of interactions you'd see as a copy and paste type command. As a new vim user, you'll need to be sure that you're always cognizant of the commands you're issuing as you may be switching between modes or having unintended consequences as you type through the characters. Make sure that you're familiar with the different modes, how to switch between them, and then how to perform the appropriate work you see. I think one of the most important commands you should learn first is how to exit a command without saving your changes. As the various commands you'll issue may have unintended consequences, knowing how to back out and start again can be really valuable. Here's a diagram that shows a way that we move between the modes within a vim open file. When we open a file, we're in command mode and we can issue several commands in order to change or alter the behavior of the file in vim editor. We typically enter these commands using the Colon key. As I stated, one of the most important commands can be how to exit the files safely without saving any changes you may have erroneously made. If we wish to issue this command, it's initiated with the Colon key, followed by Q to quit. When we're ready to make changes to a file, we'll enter edit mode by hitting the i key. This will now allow us to navigate around and directly change the text we see. Pressing escape always exits a mode and it returns us back to the command mode, where we can then perform commands such as save, exit or other commands we may wish to enter to manipulate the text. The V key enters visual mode where you can do block highlighting manipulations of the text within. Again, when you need to exit the mode and return to command mode, simply press escape. When you're ready to exit a file, the Colon key will be necessary from command mode to then save your changes or quit without saving. The most typical command we issue when we're ready to save and exit is the colon command to issue the commands, the letter W to write out our changes to the file, and Q to quit or exit the program. Once you've issued the command you wish to enter, you'll press the enter key to submit those commands.