Ova

How do I open two files in the VI editor side by side?

Published in Vim File Management 3 mins read

To open two files side by side in the VI or Vim editor, you can use the vsplit command or a simple keyboard shortcut, which splits your screen vertically.

Opening Files Side by Side in Vim

There are several straightforward methods to open and manage two files concurrently in a side-by-side view within the Vim editor, greatly enhancing productivity when comparing or cross-referencing code and text.

1. Opening Files Side by Side from the Command Line

The most direct way to open two files side by side when you initially launch Vim is by using the vim -O command followed by the filenames.

Example:

vim -O file1.txt file2.txt

This command automatically opens file1.txt and file2.txt in a vertical split, placing them next to each other in separate panes.

2. Splitting an Already Open File Vertically

If you already have one file open in Vim and wish to open another file alongside it, you can use the :vsplit command (or its shorter form, :vs).

  • To split the current window and open a new, empty buffer:

    :vsplit

    This will create a new vertical split. You can then open a file in the new pane using :e filename.

  • To split the current window and immediately open another file:

    :vsplit filename.txt

    Example:
    If you have document.md open and want to open notes.txt next to it:

    1. Enter normal mode (press Esc).
    2. Type :vsplit notes.txt
    3. Press Enter.

3. Using Keyboard Shortcuts for Vertical Split

For a quick and efficient way to split your screen vertically from any active pane, Vim offers a convenient keyboard shortcut.

  1. While in Normal mode (press Esc), press the keyboard combination Ctrl + w.
  2. Immediately after, press v (for vertical split).

This action will split your current screen into two sections vertically, placing the new pane to the right. The new pane will initially display the same file as the original pane. You can then open a different file in the new pane by typing :e filename.txt and pressing Enter.

Navigating Between Split Panes

Once you have multiple files open side by side, you'll need to move your cursor between the different panes (also known as windows or workspaces).

  • Switching Panes Directly:
    The easiest way to toggle between the active workspaces is by using the keyboard shortcut:

    • Press Ctrl + w
    • Immediately after, press w again.
      This will cycle your cursor through the open panes.
  • Moving to Specific Panes:
    For more precise control, you can use Ctrl + w followed by a directional key:

    • Ctrl + w h: Move cursor to the pane on the left.
    • Ctrl + w l: Move cursor to the pane on the right.
    • Ctrl + w j: Move cursor to the pane below (useful if you also have horizontal splits).
    • Ctrl + w k: Move cursor to the pane above (useful if you also have horizontal splits).

Resizing and Closing Panes

Managing your split windows effectively is key to a smooth workflow.

Resizing Panes

You can adjust the width of your vertical splits:

  • Ctrl + w >: Increase the width of the current window.
  • Ctrl + w <: Decrease the width of the current window.
  • Ctrl + w =: Make all windows equally sized.

Closing Panes

To close a single split pane without exiting Vim entirely:

  • :q (quit): Closes the current pane. If it's the last pane for a buffer, it will ask to save if changes were made.
  • :wq (write and quit): Saves changes in the current pane's file and then closes the pane.
  • :q! (quit forcefully): Closes the current pane without saving changes.
  • Ctrl + w c: Closes the current window.
  • Ctrl + w o: Closes all other windows, keeping only the current one open.

Summary of Key Commands

Here's a quick reference for the most common commands for side-by-side editing:

Command Description
vim -O file1.txt file2.txt Open two files side-by-side from the command line.
:vsplit [filename] Split current window vertically; optionally open filename in new split.
Ctrl + w v Keyboard shortcut to create a new vertical split.
Ctrl + w w Toggle cursor between split panes.
Ctrl + w h / Ctrl + w l Move cursor to the left / right pane.
Ctrl + w = Make all split windows equally sized.
:q or Ctrl + w c Close the current split pane.
:e filename.txt Open filename.txt in the current pane.

Mastering these commands will allow you to efficiently compare and edit multiple files simultaneously in Vim, greatly enhancing your editing experience.