Mastering Vim Split Window: How to Split Vertically and Horizontally?

TL;DR

To Vim split window, you can try these methods:

Horizontal Splits

  1. Creating a Horizontal Split: Use :split to divide the current window into two stacked windows with the same file.
  2. Alternative Method: Use :new to create a horizontal split with a new, empty buffer.

Vertical Splits

  1. Creating a Vertical Split: Use :vsplit to divide the current window into two side-by-side windows with the same file.
  2. Alternative Method: Use :vnew to create a vertical split with a new, empty buffer.

Continue reading the guide below to explore different methods to Vim split window. Also, learn tips for better window management, and how to resize split windows.

Are you finding it tough to manage multiple files and sections in Vim? I know it can be frustrating to keep switching back and forth between files. But don’t worry, I’ve got a solution for you! This post will walk you through how to split your Vim window vertically and horizontally, so you can view and edit multiple files side by side. I’ll show you simple commands and shortcuts, explain how to combine splits for a customized layout, and share tips for resizing and navigating your splits efficiently. By the end, you’ll be working more smoothly and productively in Vim.

Vim Split Vertical: How to Do It?

Vertical splits are ideal for viewing and comparing different files side by side. Vertical splits provide the benefit of easily comparing code or referring to multiple files simultaneously, enhancing your productivity and workflow efficiency. Here are two easy methods for vertical split Vim:

1. Vim Vertical Split Using Shortcut

  1. Open your Vim editor.
opening vim editor
  1. Press Ctrl+W v to split the Vim window vertically.
pressing keys to split window vertically
  1. Navigate between vertical splits by pressing Ctrl+W followed by h or l.
  2. The output will be:
vertical split view in vim

2. Vertical Split Using Command

  1. Launch Vim by typing vim in your terminal and pressing Enter.
  2. Open the file you want to work on. Execute:
 :e filename
opening a file in vim editor
  1. Now type to vim split vertically:
:vsplit

This command will split your current window vertically, creating a new window with the same file.

splitting file vertically using vsplit command
  1. Alternatively, type:
:vnew

It create a vertical split with a new, empty buffer.

creating vertical empty window using vnew command

Navigate Between Splits:

  1. Use Ctrl-w w to switch between the windows.
switching between vertical windows
  1. To move to the window on the right, press Ctrl-w l.
switching to right side window

Adjust the Size of the Splits:

  1. Use Ctrl-w > to increase the width of the current window.
increasing size of vertical split window
  1. Use Ctrl-w < to decrease the width of the current window.
decreasing size of vertical split window

Vim Split Horizontal: How to Do It?

Horizontal splits allow you to work on multiple sections of code simultaneously. Horizontal splits give you a broader view of code sections and supporting tasks such as simultaneous editing or comparing different file parts. Here are two simple methods for horizontal split Vim:

1. Vim Horizontal Split Using Shortcut

  1. Enter the Vim editor and press Ctrl+W s to split the Vim window horizontally.
pressing keys to split window horizontally
  1. Move between horizontal splits effortlessly by pressing Ctrl+W followed by j or k.
  2. The horizontal split will look like this:
horizontal split view in vim

2. Vim Horizontal Split Using Command

  1. Launch Vim by typing vim in your terminal and pressing Enter.
  2. Open the file you want to work on. For example, type :e filename and press Enter.
  3. Type following command and press Enter:
:split 

This command will split your current window horizontally, creating a new window with the same file.

splitting file horizontally using split command
  1. Alternatively, run the following command:
:new 

It will create a horizontal split with a new, empty buffer.

creating horizontal empty window using new command

Navigate Between Splits:

  1. Use Ctrl-w w to switch between the windows.
navigating between windows
  1. To move to the window below, press Ctrl-w j.
moving to the below window

Adjust the Size of the Splits:

  1. Use Ctrl-w + to increase the height of the current window.
increasing height of the window
  1. Use Ctrl-w – to decrease the height of the current window.
decreasing the height of window

Combining Vertical and Horizontal Splits:

Vim offers the flexibility to combine vertical and horizontal splits, creating complex window layouts tailored to your needs. This feature empowers you to arrange your workspace according to the specific requirements of your coding project, providing a highly customizable and efficient environment. Follow these steps to combine Vim split screen:

  1. Enter the Vim editor and create two vertical splits using the aforementioned methods.
created two vertical splits
  1. Within each vertical split, create multiple horizontal splits.
combined vertical or horizontal split windows

How to Resize and Adjust Vim Split Window

In the world of Vim, efficiently managing Vim split windows is essential for maximizing your productivity and creating a comfortable coding environment. This article explores two key techniques to resize and adjust split windows, allowing you to optimize your screen space and enhance your overall workflow.

  1. Dynamic Resizing: Easily adjust the size of the current split window vertically and horizontally by pressing Ctrl+W followed by + to increase or - to decrease its size. This dynamic resizing capability enables you to adapt the split window dimensions on the fly, ensuring an optimal view of your code and content.
resizing the split windows of vim
  1. Equalizing Split Sizes: Achieve a harmonious layout by pressing Ctrl+W followed by = to equalize the sizes of all split windows. This ensures a balanced distribution of screen space among multiple splits, promoting clarity and ease of navigation within your workspace.
equally resizing split windows in vim

3 Tips for Effective Window Management

Maximize your productivity with expert tips for efficient window management in Vim. Implementing these tips will help you organize your windows efficiently, maintain focus on critical tasks, and optimize your workspace for a smoother Vim experience. Here are three tips:

  • 📊 Organize Your Windows: Divide your workspace logically by placing related files or code sections in specific windows. For example, you can dedicate one window for the main file you’re working on and use others for references or documentation. This strategic organization helps you quickly locate and switch between windows, streamlining your coding process.
  • Maintain Focus: Vim offers powerful commands to effortlessly shift focus between split windows. Utilize shortcuts like Ctrl+W w to cycle through windows in a loop, or Ctrl+W H, Ctrl+W J, Ctrl+W K, and Ctrl+W L to move to adjacent windows in different directions. These commands enable swift navigation and allow you to stay focused on the task at hand.
  • 🖥 Optimize Screen Space: Make the most of your screen real estate by dynamically resizing split windows. Use commands like Ctrl+W + and Ctrl+W - to adjust the height of the current window, or Ctrl+W > and Ctrl+W < to modify its width. Experiment with different sizes and find the optimal arrangement that suits your workflow, reducing unnecessary scrolling or window switching.

To Sum Up

In this article, I’ve explored how to split windows vertically and horizontally in Vim. Vertical splits (Ctrl-W v or :vsplit) and horizontal splits (Ctrl-W s or :split) help you compare files and work on multiple sections simultaneously. I also shared tips for navigating and resizing splits to keep your workflow efficient.

For further learning, check out these articles:

  • Learn how to show hidden special characters in Vim. This will help you see and manage non-printing characters that might affect your code formatting.
  • Explore how to use Vim/Vi save and exit commands. Master the essential commands for saving and exiting in Vim.
  • Find out how to delete all lines in a file using Vim. Save time when you need to clear out large sections of text quickly.

Frequently Asked Questions

How can I rearrange the order of splits in Vim?

Vim provides a simple way to rearrange the order of splits. By using the Ctrl+W R command, you can rotate the splits in a clockwise direction. This action shifts the focus to the next split and brings it to the window’s top-left corner. For example, if you have three splits arranged vertically, pressing Ctrl+W R will change their order to a clockwise rotation, with the top split moving to the bottom and the bottom split moving to the middle position. This feature allows you to easily adjust the arrangement of splits according to your preferences or workflow needs.

Can I save and restore specific window layouts in Vim?

Yes, Vim can save and restore specific window layouts using the :mksession command. This command saves the current session, including window splits, buffer contents, open files, and various settings, into a session file. To restore the saved session, use the :source command followed by the path to the session file. Vim will then recreate the exact window layout and restore all the saved settings, allowing you to resume your work exactly where you left off. This feature is particularly useful when you want to preserve complex window arrangements or continue your work at a later time.

Can I close all splits in Vim and return to a single window view?

Yes, Vim provides a convenient way to close all splits and return to a single-window view. By using the :only command or pressing Ctrl+W o, you can close all splits except the currently focused one. This action maximizes the current split, allowing it to occupy the entire window. Whether you have multiple splits arranged horizontally or vertically, executing :only or Ctrl+W o ensures that only the active split remains visible, providing a clutter-free workspace. This feature is beneficial when you want to focus temporarily on a single file or when you prefer a single window view for specific tasks or scenarios.

What is an Nvim split window?

An Nvim split window allows you to divide the Neovim (Nvim) editor into multiple sections, either vertically or horizontally, so you can view and edit multiple files or different parts of the same file simultaneously.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

3 Simple Methods to Check Linux Login History

Next Post

How to Download and Install iTunes for Ubuntu [14 Easy Steps]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next