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
:saveasor: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.txtIf you have the old file still open in another buffer, switch to that buffer using the
:bor:buffercommand.:b old_filename.txtClose the old buffer using the
:bdor:bdeletecommand.:bd
Switch Between Files in Vim with the :e# Command
Open a file in Vim.
vim file1.txtOpen another file using the
:edit(or:e) command.:edit file2.txtSwitch back to the previous file using the
:e#command.:e#Save the current file and switch to the alternate file using
:wand: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
:bnor:bnextcommand.:bnSwitch to the previous buffer using the
:bpor:bprevcommand.: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>