1. Introduction

Vim is one of the most commonly used text editors in the Linux community. In terms of efficiency and versatility, Vim often stands way ahead of other text editors. File renaming is a very common yet crucial task when manipulating files.

In this tutorial, we’ll learn different ways to rename the current file in Vim. We can do this while either keeping or discarding the old file and name. We’ll explore several ways from both perspectives.

2. Sample Dataset

Before moving forward, let’s create a file to work with throughout this article:

$ vim sample.txt

The above command opens Vim with a new file, sample.txt. Here, we’re assuming that we don’t have any file with the same name in our working directory.

Let’s type Baeldung is best and save it in the sample.txt file:

:w

Thus, we created the file sample.txt with the specified content. Now, we can try renaming this file using different commands.

3. Using the :saveas Command

As we open a file in Vim, the editor creates a buffer, i.e., a temporary copy of the file content. We can save this buffer under a new name using the :saveas command:

:saveas new_saved.txt

Thus, we save the current buffer to the new_saved.txt file.

4. Using the :write Command

We can also save the current buffer in a newly-specified file by name with the :write command:

:write new_written.txt

As with most commands, we can also use shorthands like :w to rename the current file. Moreover, in Vim, we can combine commands together:

:wq newly_written.txt

This command renames the current file to newly_written.txt and quits the Vim editor.

5. Using the :file Command

The :file command changes the name of the current file to the newly specified file name:

$ :file new_file.txt

Unlike other commands, the :file command doesn’t automatically save the file after renaming. Therefore, we have to save the file before quitting Vim:

$ :wq

In the above command, w saves the file and q quits Vim.

6. Using the :Explore Command

The :Explore command opens the Vim built-in file explorer, Netrw. Netrw provides several options for file manipulation, including renaming.

To enter Netrw, we use :Explore in the Normal mode:

$ :Explore

The above command shows the Netrw interface:

$
" ============================================================================
" Netrw Directory Listing                                        (netrw v171)
"   /home/users/Baeldung/
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:special
" ==============================================================================
../                                                                                                                                     
./
new_file.txt
new_saved.txt
new_written.txt
newly_written.txt
sample.txt
~                                                                                                                                       
~

To manipulate a file, we first navigate to the file name via the arrow keys (and Return, if we need to enter or exit directories). Once over the file we want to rename, we press R, enter the new name, and press Return to confirm.

7. Using Shell Commands

We can also run shell commands in the Vim text editor. Thus, we can perform a file move or copy operation.

To do so, we prepend an exclamation mark to commands in the Normal mode:

$ :!mv sample.txt file_moved.txt

In this case, we use mv to move sample.txt to file_moved.txt. Even though the old file won’t exist anymore, Vim keeps the buffer under the old filename. So, if we save it, we’ll have both the old and new files.

In the same way, we can use the cp (copy) command instead of mv:

$ :!cp sample.txt file_copied.txt

After this, we can press Return and continue working on the current file, as it still exists.

8. Conclusion

In this article, we’ve learned different ways to rename the current file in Vim. We’ve discussed approaches that preserve or discard the old file and name. Of course, we can select any method depending on our preferences.