Skip to main content

Managing Multiple Files in VI Editor: A Comprehensive Guide

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: file1.txt, file2.txt, and 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, file2.txt and file3.txt are also loaded in the background.

Once you have multiple files open in VI, you can navigate between them using the following commands:

  • :n or :next - Moves to the next file in the list.
  • :prev or :previous - Moves to the previous file in the list.
  • :rew or :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.
  • :e# or Ctrl + ^ - Switches between the current and the last accessed file.

Example

  1. Open the first file (file1.txt) and type :n or :next to move to the next file (file2.txt).
  2. To navigate back to file1.txt, type :prev or :previous.
  3. Use :rew to rewind back to the first file in the list if you are at a different file.
  4. Use :last to navigate directly to the last file in the list (file3.txt).

Editing Multiple Files

When navigating between multiple files, you can edit each file independently. Enter the Insert mode by pressing i and make your modifications. Press Esc 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 :e (edit) command followed by the filename. For example, if you wish to open a file named file4.txt, you can use the following command:

:e file4.txt

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 :e# or Ctrl + ^.

Closing One of the Opened Files

If you need to close one of the files you've opened, you can use the :bd ( 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 viewing file2.txt and want to close it, type the following command:

:bd

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 following command:

vi *.txt

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.

Conclusion

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.

YouTube @cloudaffle