Navigating between multiple files and managing them effectively is a common requirement when working with the VI editor. This article will provide a detailed walkthrough on how to open and switch between multiple files in VI, thus enhancing the user's multitasking abilities and overall efficiency.
Creating Multiple Sample Text Files
Before we dive in, let’s create multiple sample files to work with. Open the terminal and type the following commands:
echo "This is file one." > file1.txt
echo "This is file two." > file2.txt
echo "This is file three." > file3.txt
These commands will create three text files:
file3.txt, each containing a simple string.
Opening Multiple Files in VI
To open multiple files simultaneously in VI, you can use the following command:
vi file1.txt file2.txt file3.txt
This command will open
file1.txt initially. However,
file3.txt are also loaded in the background.
Navigating Between Multiple Files
Once you have multiple files open in VI, you can navigate between them using the following commands:
:next- Moves to the next file in the list.
:previous- Moves to the previous file in the list.
:rewind- Goes back to the first file in the list.
:last- Goes to the last file in the list.
:args- Displays the list of files currently opened.
Ctrl + ^- Switches between the current and the last accessed file.
- Open the first file (
file1.txt) and type
:nextto move to the next file (
- To navigate back to
:rewto rewind back to the first file in the list if you are at a different file.
:lastto navigate directly to the last file in the list (
Editing Multiple Files
When navigating between multiple files, you can edit each file independently.
Insert mode by pressing
i and make your modifications. Press
to go back to command mode and type
:w to save the changes before moving to
the next file.
Opening Additional Files
Once you have multiple files open in VI, you might want to open additional files
without closing the current ones. This can be achieved with the
command followed by the filename. For example, if you wish to open a file
file4.txt, you can use the following command:
This will open
file4.txt in the same VI session, allowing you to edit it along
with the previously opened files. To navigate between
file4.txt and the last
accessed file, you can use
Ctrl + ^.
Closing One of the Opened Files
If you need to close one of the files you've opened, you can use the
buffer delete) command. This command will close the current file you are viewing
but will keep the other files open. For instance, if you are currently
file2.txt and want to close it, type the following command:
This command will close
file2.txt and automatically switch to the next file in
the buffer list. If you need to close a specific file without switching to it,
you can specify the buffer number with
:bd [buffer number].
Opening All Files in a Directory
If you are working in a directory with multiple files and you wish to open all
the files in that directory, you can use wildcards with the
vi command. For
example, to open all text files in the current directory, you can use the
This will open all
.txt files in the current directory in VI. You can navigate
between them using the navigation commands mentioned in Step 3.
Mastering the art of managing multiple files in VI is essential for anyone looking to elevate their VI user experience. The capability to swiftly navigate and modify between different files not only improves productivity but also offers an organized approach to handling diverse sets of files concurrently. By understanding these aspects and utilizing the multifile handling capabilities of VI, users can ensure a more seamless and efficient coding or editing experience.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.