If you’re going to be using the command line in Linux, you have to know how to save in vim. Vim is a powerful text editor commonly used on Linux systems and always comes pre-installed, so you don’t have to do anything to access it. Moreover, it has many advantages compared to other file editors.
Another popular file editor is Nano, but Vim offers superior text searching that allows for regular expressions, for example. Nano is easier to understand for beginners, but if you’re planning on spending a lot of time in the Linux command line, I suggest you quickly become familiar with Vim.
This tutorial will get you started with how to save in Vim, and we’ll take it from there.
With Vim, you work entirely with the keyboard, and you can save files in less than a second using the built-in commands. This can be dangerous as well, but let’s see how it works.
Saving in Vim: a Quickstart
To quickly answer the question, here’s a quick way to save in vim.
First, press the “Esc” key. This will exit whatever mode Vim is currently in, and bring it to “normal” mode. Now type the following:
Press “Enter”. Include the colon as well. This will write the file to the disk. If you want to save your text and close Vim, then type the following:
After you press enter, the editor will close itself and return you to the command line. Here’s what it looks like when you’re entering the command:
Now that you know how to save in Vim, let’s look at what else you need to know to use Vim effectively.
How to Save in Vim Using a New Filename
Let’s say you open a sensitive configuration file for editing, and then realize that you forgot to take a backup. Remember – always take backups before making changes to a file! Normally, you would have to exit the application, copy the file with a new name, and re-open it for editing. In Vim, you can save a file with a new filename using the simple command:
Remember, always press Escape (Esc) before entering commands. This ensures that Vim is in the right mode to accept commands and you can start immediately by typing a colon (:). Here’s a screenshot of where I have backed up a file called “test.txt” by writing out the file to another one called “testbackup.txt”:
As you can see, a new file has been written with the name “testbackup.txt”. The edited file is still the same as before – namely test.txt. So you can use this method to quickly create a backup of a file before you begin editing it.
Advanced Saving Techniques in Vim
Vim has a number of clever techniques for saving that can improve your workflow. They rest on the concept of a “buffer”.
How Buffers Work in Vim – Saving Multiple Files
In Vim, when you start editing a file, the changes aren’t made to the disk, but to an in-memory representation called a “buffer”. This isn’t particularly difficult to understand. All text editors work like this, and indeed it would be stupid if they didn’t – no one wants to write incomplete files to disk!
With the buffer system, Vim allows you to open multiple files simultaneously in the same window, and save them all. There are three ways this can happen:
- Side by side vertically
- Side by side horizontally
To open several files simultaneously side by side vertically, use the following command:
vim -O file1.txt file2.txt
Using the two files we created above, here’s what it looks like:
Now if we want to save all the open files at once, we once again hit “Esc”, then type:
And if we want to save all our files and quit Vim, we type:
Opening Tabbed Files in Vim
Opening files side by side, either vertically or horizontally is useful if you have two, or a maximum of three files. However, if you need to work with more than that, there isn’t enough screen space for all of them. In such situations, opening files in a tabbed format is more efficient, as you can open dozens of files simultaneously. To open a bunch of files in Vim in a tabbed format, type the following:
vi -p test.txt testbackup.txt
This will open the files with tabs for each filename as shown here:
You can switch to the next tab by typing “gt” after pressing “Esc”. If you have multiple files open, then you can switch to a specific tab number by typing:
Where “N” is the tab number. There are also commands to go to the first and last tab. You can find the commands for tab navigation easily online.
Using the Vim “update” Command to Save Files
Some people have a habit of saving their files whenever they get the chance, whether there are changes or not. This isn’t a bad thing. Save early, and save often! And indeed, under normal circumstances, there are no downsides to this. Text files are typically too small to create any kind of impact on modern servers, and even if you were an obsessive saver, there’s no harm in saving as often as you can.
Problems, however, can arise in two cases. First, the location of the disk to which the file is saved might not be a local one. In the case of a remote disk, the process of saving can introduce some latency, and this could be a problem if you save too often, and the delay can become noticeable if it impacts your workflow. The second issue can arise when the files in question are truly large. We typically don’t see large text files in everyday life, but sometimes files like server logs can grow to truly gargantuan sizes, as they incorporate all kinds of events over a long period of time. In such situations, you might find that saving them frequently results in a lot of disk activity, particularly when combined with the first factor.
To solve this problem, Vim has the “update” command. To invoke it from normal mode, just type:
As before, you can always enter normal mode by pressing the “Esc” key. Instead of using :update, you can also just use:
Or, if you have multiple files open:
The “update” command is the same as the “write” command but with one difference. If there are no changes to the buffer, then it doesn’t do anything. If the buffer has changed, then it will write the changes to the disk as if you had issued the “:w” command. In this way, you won’t waste network and disk activity by issuing useless write commands that don’t change the contents of the file.
Vim, also invoked with the command “vi”, is a versatile and powerful text editor for the Linux command line. If you foresee yourself using text commands a lot, then it’s well worth the investment of time to learn how to use this tool. As you can see, there are many ways of saving a file using Vim, and you can use its features to become more productive with multiple files, just as if you were using a GUI.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!