Example 01:
Let’s get started with our first example of creating a new filename in a vim editor. You need to open a new vim file using the “vim” command. We have been creating a vim file of the name “test” right now, as shown beneath. Execute the below-written instruction in the Ubuntu 20.04 operating system terminal.
The empty file has been opened in the vim editor. Now, the file is opened in the normal mode of Vim and is empty. If you are not in a normal mode, press “Esc” to go. To switch from one Vim mode to another, you must first switch to the normal mode. Let’s add some data to this file.
You need to press the key “I” from the keyword to open vim’s Insert mode. Now, while we are in an “Insert” mode, we have added 4 lines of numerical data in our file, as shown.
To save this data in the file, we need to launch the normal mode again by pressing the Esc key from the keyboard. After that, press “:” for command mode. After this, tap “w” from the keyboard followed by the Enter key to save your added input data to the file.
The data has been saved into the file, and vim has been showing it at the bottom line of the vim editor “test [New] 4L, 43C written” as per the below-shown image.
Now, the data has been saved to the already made vim file, i.e., “test”. It’s time to save this file data as a new filename. You can say that we have to change the filename to a new filename in vim. For that, you need to open up the normal mode once again by pressing the Esc key. Now, we have switched to the normal mode. It’s time to open up the command mode of Vim by pressing the “:” key. Now, press the “w” key from the keyword followed by the space key and the new name of a file. Tap “Enter” to execute this command on Vim.
Now, the exact copy of the same file has been created in the home folder with a new filename.
Let’s take a look at the home folder now. So, we have been using the list “ls” command to list all the home folder files and sub-folders. After listing, we have got to know that both the vim files are there in the folder. Execute the below-written instruction in the Ubuntu 20.04 operating system terminal.
Let’s see the content of both files using the cat command on the shell with the name of a file. So, we have listed the “new” and “test” files separately. Both the files contain the same data as we have saved the file “test” with a new name “new” in the Vim editor presented below. Execute the below-written instruction in the Ubuntu 20.04 operating system terminal.
$ cat test
Example 02:
Let’s take a look at another illustration to create a file with a new filename. For this, we need to use the terminal shell again. We have been opening the Vim editor of the Ubuntu 20.04 system using the simple one-word “vim” command, as shown in the attached screenshot below. Execute the below-written instruction in the Ubuntu 20.04 operating system terminal.
The empty vim editor has been opened on our terminal screen below. You need to add some data into it and then save it. For this, you have to go towards the normal mode of Vim using the “Esc” key and then press “I” to open its Insert mode.
After using the “Esc” and “I” keys from the keyboard, we have switched to the insert mode from Vim’s normal mode. We have added one line of string data in our vim file, as shown below.
Now, it’s time to save our data. For that, open the normal mode using the “Esc” again. When the normal mode got opened, tap “:” to open the vim’s command mode to add some instructions on it. So, we have to press “w” to save our data in the command mode as displayed.
When we pressed the “Enter” key after adding the “:w” command, we got the error “E32: No filename”. We have directly opened the vim editor without using any name for a file on the shell.
To avoid this error and save this vim data to a file, we need to give this no-name file a new name. For that, add the “:w” command and a new file name, i.e., “new.txt” in the command area and press Enter.
After listing the home folder contents, we have come to know that the vim no-name file is saved as a new file with a new name, “new.txt”. Execute the below-written instruction in the terminal of the Ubuntu 20.04 operating system.
Conclusion:
This guide is all about saving a vim file with a new filename. We have achieved this goal by implementing two different situations, i.e., saving an already named vim file as a new filename and saving the no-name vim file as a new filename.