Renaming the Current Buffer in Vim
- Save the current buffer with a new name and keep it open in the current window. You can use
:saveas
or:w
.
:saveas
Command
:saveas new_filename.txt
It can be shortened to :sav
.
:w
Command
:w new_filename.txt
The main difference between :saveas
and :w
is that :saveas
renames the current buffer and saves it with the new name, while :w
saves a copy of the buffer to a new file without renaming the buffer itself.
Delete the old file from within Vim using the
:!
command.:!rm old_filename.txt
If you have the old file still open in another buffer, switch to that buffer using the
:b
or:buffer
command.:b old_filename.txt
Close the old buffer using the
:bd
or:bdelete
command.:bd
Switch Between Files in Vim with the :e#
Command
Open a file in Vim.
vim file1.txt
Open another file using the
:edit
(or:e
) command.:edit file2.txt
Switch back to the previous file using the
:e#
command.:e#
Save the current file and switch to the alternate file using
:w
and:e#
commands combined.:w | e#
Differences Between :b
and :e
Commands in Vim
:b
(or:buffer
) is used to switch between already opened buffers in Vim by providing the buffer number or a unique part of the filename.:e
(or:edit
) is used to open a file in a new buffer or reload the contents of the current file from the disk.
Additional Tips and Commands
Switch Between Buffers with :bn
and :bp
Commands
Switch to the next buffer using the
:bn
or:bnext
command.:bn
Switch to the previous buffer using the
:bp
or:bprev
command.:bp
List Open Buffers with :ls
Command
List all open buffers and their buffer numbers using the :ls
or :buffers
command.
:ls
Jump to a Specific Buffer with :b
Command
Jump to a specific buffer using the :b
or :buffer
command followed by the buffer number.
:b <buffer_number>