How to Have Different Windows in Linux with a CLI

Posted on Updated on

One of the biggest (if not THE biggest) differences between Windows and a typical Linux server, is that the former typically runs with a GUI and a mouse, and the latter runs from the command line. Of course, you can install and use a graphical interface even with Linux, but that would be a waste of resources, and add unnecessary complexity to the installation. A full blown graphical interface uses up a lot of resources!

You might think that we can’t multitask using a Linux command line, but that’s nonsense. We can run our commands in the background if we want, since Linux fully supports multitasking. No, the real benefit of a GUI is the ability to have different windows and screens and being able to switch between them at any time.

Let’s say you’re editing a config file in Linux with the “vi” command, and you suddenly need to pop out and do something else, with the intention of returning to it once you’re done. Using the regular Linux tools, this is not possible. You have to close vi (without saving, because the changes might be incomplete), finish your other task, and open it again. In the meantime, you’ve lost your place in the document, and have to start your corrections all over again.

Luckily, there is a program called “screen” which is included in the basic Linux repos that replicates this functionality and allows us to have different windows and screens in Linux. Let’s show you how it works.

Installing Screen

Your Linux installation’s base repos should already have this incredibly useful program. On CentOS, install it via the following command:

yum install screen

Once the install is complete, it’s time to start creating new windows!

Starting Screen and Creating a new Window

Start the screen program using the command “screen”. This will bring you back to the command line like this:

You might not notice any difference, but you’ve just dropped out of the shell and are inside the screen program. If your shell has a bar like in the screenshot above, it should tell you that you’re currently in screen number “0”. Now, we fire up our text editor and start editing a file like here:

Now let’s say we suddenly need to perform some basic task urgently. Instead of quitting vi, we simply start up a new window using the following keystrokes:

ctrl+a c

So first press the keys Ctrl and “a” together. You won’t notice anything on the screen, but your keystrokes have been recorded. Then press “c”. This will create a new window and drop you back to the command prompt as shown here:

This looks similar to when you first started up screen, but the number has changed from “0”, to “1”. Your previous screen is still open in the background in screen 0. To navigate between screens, type the following:

ctrl+a n

This will move you to the next screen, whichever one it is. You can also specifically go to screen “0” by typing:

ctrl+a 0

You can also get a list of current screens by typing:

ctrl+a "

As shown here, this will display a list of open screens and you can select them using your cursor:

Have Different Windows in Linux with a CLI

So you can finish up your work in your new screen “1”, then kill the current window using this:

ctrl+a k

It’ll ask you for a confirmation. Hit “y”, and you’ll be dropped back into the previous screen – in this case, your text editor which is right where you left it with all your changes intact!

Exiting Screen

If you want to exit the “screen” program entirely, just type:

ctrl+a \

This will ask for a confirmation to kill all existing windows and drop you back to the shell. A good rule of thumb is that if you think you’re going to be interacting with a program for a while, like a process monitor, or editing a text file, it’s a good idea to start it up in a new screen window. This way, you can switch back and forth between different windows if needed and not lose your progress!

Leave a Reply

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