Hack and / - Do the Splits

 in
Get two terminals in the space of one with split screens on a number of different command-line tools.

Even with the high-resolution flat-screen monitors we have these days, screen real estate still can be at a premium. Of course, if you spend a lot of your time in a terminal, this is even more true. When you want to compare two files at the same time or monitor two different sessions at once, you either carefully position terminal windows or rely on tabs.

Tabs can be fine, but when I use terminals, I like to stick to the keyboard as much as possible. Plus, I don't know about anyone else, but for me, there are four main programs I run in terminals: mutt, vim, screen and irssi. Luckily for me, all of these programs support some form of split screens—the ability to divide the terminal either vertically or horizontally. Although these features aren't necessarily anything new, if you don't use them every day, it can be hard to remember how to split the screen, navigate between the sections, and then go back to a single screen. In this column, I discuss the split-screen features in my four favorite terminal applications and provide a simple guide to help us all commit them to memory.

Mutt Pager Indexes

I suppose if you want to be technical, this isn't exactly the same as the split screens in the other tools, but while you are in the mutt pager (the part that lets you view the body of an e-mail message), by default, mutt fills the entire terminal with the e-mail. If you want, however, you can tell mutt to take a specified number of lines at the top and use them to display your index. This way, you can browse through the contents of an e-mail message but still be able to keep an eye on the other headers in your index. To use ten lines for this feature, simply add the following:

set pager_index_lines=10

to your ~/.muttrc.

Figure 1. Mutt with pager_index_lines Enabled

Vim Splits

Vim is my favorite text editor (I've used it for basically all of my writing), and its split-screen feature is especially useful for sysadmin work. I can't count how many times I've made a change in one configuration file or script that I've wanted to add to a second file. To enable split-screen mode for a horizontal split, type:

:split

And, for a vertical split, type:

:vsplit

By default, vim shows the same file in both panes. Press Ctrl-W, and then use the regular HJKL keys (or arrow keys if you aren't a home-row junkie like me) to navigate between panes. So, if I had made a horizontal split and wanted to open a new file in the bottom pane, I would press Ctrl-W J to move the cursor to that pane, and then I would type :open filename to open the new file. When you are finished with a particular pane, make sure the cursor is in that pane, and then save and close the file in the normal fashion.

Vim isn't limited only to two panes either—simply type the :split or :vsplit command again to add a third horizontal or vertical pane, respectively. You even can split the window horizontally and then type :vsplit to split that pane further into two vertical panes.

Figure 2. Vim with a Vertical Split

Split Screen

Screen is another one of those indispensable command-line tools. If you haven't used screen before, it essentially allows you to open multiple numbered shells, and you can switch to them with Ctrl-A <number>. Then, you can detach from your screen session and connect to it later, and in the meantime, all the shells you have opened within it keep their state. One way I commonly use screen is for irssi, a command-line-based IRC client. I open irssi within screen on a server that is always up. Then, no matter where I am, I can connect to the remote server and resume my irssi session, which always stays connected.

Beyond the standard screen features, screen also supports a horizontal split screen. This can be useful if you want to monitor IRC in one window and perform other commands in the second. Also, if you use a text editor or other tools that don't support split panes on their own, you can use screen's split feature as a supplement.

To split the pane within screen, press Ctrl-A Shift-S. Then, you can press Ctrl-A Tab to move your cursor between the two panes. You will notice that the bottom pane is empty at the beginning. Once you have moved the cursor to it, you either can switch to a currently open window with Ctrl-A <number>, or you can press Ctrl-A C to create a new window in that pane. Screen also supports multiple panes. Simply press Ctrl-A Shift-S a second time to split the session into three equally sized panes. When you are ready to close a pane, press Ctrl-A Tab until it has the cursor, and then press Ctrl-A Shift-X to close that pane.

______________________

Kyle Rankin is a systems architect; and the author of DevOps Troubleshooting, The Official Ubuntu Server Book, Knoppix Hacks, Knoppix Pocket Reference, Linux Multimedia Hacks, and Ubuntu Hacks.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState