Vim Viewports (Windowing tips for a Text Editor)

If you didn’t know it about me, I’m a very big fan of using text as my default method of storing information.  Whether it’s a note about the specific hex colors for a website, my daily tasks, projects or  todo lists, I keep them all in plaintext files.  I made the transition to text-only years ago when I ran into issues trying to move my palm-pilot memos to a new platform.  It took quite a bit of manipulation before finally being able to migrate my information from a proprietary format into one that is ubiquitous.  Plaintext is ultimately portable and can be taken everywhere.  I will always be able to read my plaintext files.  No, they’re not formatted all nice and pretty, but I can use html or some other markup language if I want to make them look nice.  No, my primary concern is that I can read them.  Twenty years from now, I am confident that whatever office suite word processor is in vogue, it will be able to read my meager text files.   Those old Wambulator 5 files?  I’m probably gonna be out of luck with those.

Emacs

Emacs

Text editors are varied and many-flavored.  In the unix world, there are two primary editors: Vim and Emacs.  Both have their strengths and weaknesses.  (please, no need to get all huffy about it, I use both.  It’s true and I admit it: I am ambi-textrous)   For the quick editing of small scripts, notes, or log files I tend to favor the use of Vim.   I have a handful of basic Vim commands that I use constantly to edit and save files; a few commands such as search and replace functions that get used occasionally; and then there is a whole world of commands that I am completely unfamiliar with.  Vim Viewports is one such category of commands.

Did you know that the text editor Vim (which stands for Visual editor IMproved) is capable of providing a windowed editing page?   In Vim-land, these windows are called ‘viewports’   When you first invoke Vim from the unix command line you start out with a single viewport.

Vim - Single viewport

Vim - Single viewport

This is the default view that most users of Vim are used to seeing.   I’ve been using Vim (or it’s predecessor Vi) for well over a decade now and this single-viewport view is the only one I’ve used until very recently.   There are times when I want to be able to edit one file, while referencing another.  Before utilizing viewports, that meant opening another terminal, setting up another connection, and displaying the reference file using cat, less or more (depending on the system)

That all changed when I re-discovered the fact that Vim supports multiple viewports!  From the command mode of Vim (there are two modes; command mode and edit mode.  If you are unfamiliar with these concepts then this article is most likely not for you. Go learn some Vim basics over at the Vim homepage: http://www.vim.org/ then please come back!)

::ahem:: Where were we?

Vim - Three Viewports

Vim - Three Viewports

Ah yes, from the command mode of Vim enter :split to split the current viewport horizontally.  Entering :vsp will split the window vertically.  Different text can then be loaded into each viewport using :e <filename>  Moving from one viewport to the next is accomplished using Ctrl-w Ctrl-w.  (that’s Ctrl-w twice, not a typo)   Or the standard Vim cursor movement keys modified with a Ctrl-w will also move you between viewports.  So Ctrl-w j will move you one viewport down, Ctrl-w k will move you one viewport up, and so on.  (for h j k l )

The content of each viewport can be swapped back and forth or rotated by using Ctrl-w r to move the current viewport contents to the right or Ctrl-w R to move the contents to the left.

It’s easy to open a bunch of viewports and have them all different sizes, making the contents difficult to read.  Issuing Ctrl-w = will resize all of the viewports, making them equal sizes (or as close as possible)

Some Vim commands for manipulating viewports

:sp|:split will split the Vim window horizontally.

:vsp|:vsplit will split the Vim window vertically.

Ctrl-w Ctrl-w moves between Vim viewports.

Ctrl-w j moves one viewport down.

Ctrl-w k moves one viewport up.

Ctrl-w h moves one viewport to the left.

Ctrl-w l moves one viewport to the right.

Ctrl-w = tells Vim to resize viewports to be of equal size.

Ctrl-w – reduce active viewport by one line.

Ctrl-w + increase active viewport by one line.

Ctrl-w q will close the active window.

Ctrl-w r will rotate windows to the right.

Ctrl-w R will rotate windows to the left.

Do you have a great Vim tip?  Or an <editor of your choice> tip?  Then be sure to comment below, I’d love to learn something new!

2 Comments

  1. My only thought was, how could you live with an editor that didn’t do that?

  2. Jeff says:

    The version of VI that is supplied by Solaris does not support viewports. Tested in Solaris 9 and 10. Vim is included on the Solaris Companion CD (http://wwws.sun.com/software/solaris/freeware/) or from Sunfreeware.com (http://www.sunfreeware.com)

Leave a Reply