Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

161 Jane Doh

I understand that Up/Down will give you command history. But how do you look at past results by scrolling up and down?

I've used Shift + Page Up / Page Down , Alt + Shift + Up / Down and Page Up / Page Down , but none of these seem to work.

This is Redhat Linux box.

linux terminal rhel

14 responses

SHIFT + Page Up and SHIFT + Page Down . If that doesn't work try this and then it should:

Go to your terminal program and make sure Edit/Profile Preferences/Scrolling/Scrollback/Unlimited
checked.

The exact location of this option may be somewhere else, but I see that you are using Redhat.

SHIFT+Page Up and SHIFT+Page Down are the correct keys to work on virtual console, but vmware console doesn't have those terminal settings. The virtual console has a fixed scroll back size, it looks like it is limited by the size of the video memory according to this virtual Linux console Scroll documentation.

ALTERNATIVE FOR LINE-TO-LINE PROCESSING

Ctrl + Shift + Up Arrow or Down Arrow

Unlike Shift + Page Up or Page Down , which scrolls the entire page, this will help with smooth linear scrolling, which is exactly what I was looking for.

Another alternative that may already be installed on your system is to use GNU shield:

# This starts screen which adds basic window management in terminals screen # This starts the copy mode you can use to scroll [ # Now use the arrows to scroll # To exit copy mode, do

See the man screen for more useful options (multiple windows,...)...

It doesn't seem to be easily possible: the Arch Linux Wiki doesn't list ways to do this on a console (although it's easily possible on a virtual terminal).

Ctrl - b , then [ , then you can use normal navigation keys to scroll (like Up Arrow or PgDn). Press q to exit scroll mode.

The topic of useful methods of work in Linux terminal inexhaustible. It would seem that everything is arranged very simply: a shell prompt, and commands entered from the keyboard. However, in this simplicity lies an abyss of non-obvious, but useful possibilities. That is why we regularly publish materials on the features of working in the Linux command line. In particular, today it will be a translation of an article whose author is passionate about saving time through increasing productivity.

If you are interested in working on the Linux command line - here are some of our materials on this topic:

  • [ +114 ] Most Useful Linux Command Line Techniques
  • [ +70 ] 10 Linux Terminal Tricks Few People Know About
  • [+65 ] Surprisingly useful tool: lsof
  • [ +32 ] Linux: redirect
  • [ +29 ] PDF version of articles about Bash scripts

Linux command line tricks and time savings

Have you ever seen a colleague use some simple Linux commands to solve problems that take you several lines to solve? It must be that when something like this happened, all you could say to yourself was, “Wow! I didn't know it could be done so easily."

In this article, I'll show you some professional Linux command line tricks that will save you a lot of time and, in some cases, get rid of a bad mood. Not only that, your friends or colleagues will say: “Well, wow!”, Looking at you. These tricks will also help you increase your personal productivity, as you will need to type fewer commands and even make fewer mouse clicks.

This is not to say that these Linux tips are just for beginners. It is quite possible that even advanced users find something useful here that they didn't know about despite having used Linux for many years. Linux is learned by experience. This is where your own experience and the experience of other people come into play.

Before we begin, I would like to note that some of the methods presented here depend on the configuration of the shell.

0. AutoComplete Commands Using the Tab Key

I'll start with something very obvious, but very important: autocomplete commands using the Tab key.

When you start typing in the Linux terminal, you can press the Tab key, which will bring up a tooltip containing a set of options to continue typing, starting with the characters you just typed.

For example, if you are going to copy a file named my_best_file_1.txt , you can simply type cp m and press Tab to see possible options command continuation.


Using the Tab key to auto-complete input

The Tab key can also be used to autocomplete commands.

1. Change to the last working directory

Imagine that you were working in a directory that is not so easy to get to, and then moved to another directory located in a completely different place. Then you realized that you need to return to the previous directory. In this case, just use the following command:

CD-
It will allow you to be in the last working directory. Now, in situations like this, you no longer need to enter long paths, or use copy and paste.


Easy switching between directories

2. Return to home directory

This approach is also very obvious. You can use the following command to return to your home directory from anywhere:

CD ~
However, you can return to your home directory even faster with the cd command.

In most modern Linux distributions, the shell is preconfigured to treat the cd command in this way. This will save you a couple of keystrokes on your keyboard.


Most fast way change to home directory

3. Displaying the contents of a directory

You may be wondering what is the subtlety of using a command to list the contents of a directory. Everyone knows that the ls -l command serves this purpose.

But that's the whole point. Almost everyone uses the ls -l command to list the contents of directories, while the same can be done with the following command:

Ll
The functionality of this command depends on the Linux distribution and on your shell setup, but it is highly likely that you will be able to use it on most distributions.


Using the ll command instead of the ls -l command

4. Calling multiple commands in one line

Imagine that you need to execute several commands in sequence. Probably, you enter one command, then wait for it to complete, then enter the next one?

In such a situation, a command separator will be useful; (semicolon). With this approach, you can enter multiple commands on the same line. In this case, unlike the usual input of commands, to execute the next command, you do not have to wait for the completion of the previous one.

Command_1; command_2; command_3

5. Execution of several commands in one line and a condition for the successful completion of the previous command

We just looked at a way to call multiple commands on a single line. This saves time. But what if you want, say, when you call two commands, so that the next command is executed only if the previous one completes without errors?

Imagine that you want to build the code and then, if the build was successful, call make ?

In such a situation, you can use the delimiter && . This delimiter ensures that the next command will only be executed if the previous one succeeds.

Command_1 && command_2
Here good example using && :

sudo apt update&& sudo apt upgrade

6. Easy search and use of previously entered commands

Imagine that you, a couple of minutes or a couple of hours ago, entered a long command, and you need this command again. Moreover, the problem is that you cannot remember exactly this command.

In such a situation, a reverse search will save you. This technique allows you to search the command history for a keyword. Here it is enough to use the key combination Ctrl + R to launch a reverse search and enter something related to the command. The system will look through the history of commands and show the commands that match the entered query.

Ctrl+R search_term
By default, only one result will be shown. In order to see more results that match your query, you will need to use the keyboard shortcut Ctrl + R again and again. To exit reverse lookup mode, press Ctrl + C .


Reverse command history search

Please note that in some bash shells You can use the Page Up and Page Down keys to scroll through the search results.

7. Unlocking the terminal after accidentally pressing Ctrl + S

You may be used to using the keyboard shortcut Ctrl + S to save files. But if you press these keys in the Linux terminal, you will lock it.

If earlier you, in order to correct the situation, were forced to close and restart the terminal - now you can breathe easy, you won't have to do this anymore. In order to bring the terminal to a working state, simply use the key combination Ctrl + Q .

8. Move to the beginning or end of a line

Imagine that you enter a long command and somewhere in the middle you realize that you have to change something at the beginning of it. You are probably using the arrow keys to first move to the beginning of the line and then back to the end.

Of course, in such a situation, you can use the Home and End keys, but, alternatively, using the key combination Ctrl + A you can go to the beginning of the line, and using the combination Ctrl + E - to the end.


Move to the beginning or end of a line

I find this way more convenient than using the Home and End keys, especially when I'm working on a laptop.

9. Read log files in real time

When you need to parse log files that some running application writes to during parsing, you can use the tail command with the -f option.

Tail -f path_to_Log
Alternatively, you can use the grep command in its normal form to display only the lines you are interested in:

Tail -f path_to_log | grep search_term
You can also use the F option here. This will cause the tail utility to continue running even if the log file being viewed is deleted. With this approach, if this file is created again, tail will continue to print data from it to the screen.

10. Reading compressed log files without decompressing them

Server logs are usually compressed with gzip to save disk space. This can lead to some inconvenience when analyzing logs by developers or system administrators. You may need to use the scp command to copy the file to your local directory and then extract the file from the archive in order to view it, as you may not have write permission to where the file you are interested in is stored.

Here we come to the aid of a group of commands whose name begins with the letter "z". These commands are an alternative to the usual commands that are used to work with log files. Among them are less , cat , grep , and others.

So, using the commands zless , zcat , zgrep and the like, you don't have to explicitly unpack compressed files. Details about these commands can be viewed.

By the way, when I told a colleague about these “secret” commands, I got a cup of coffee.

11. Using the less Command to Read Files

If you need to view a file, especially a large one, you can try cat , but it's much better to look elsewhere. The fact is that cat will display the entire file on the screen, which is not so convenient.

You can use an editor like Vi or Vim in a terminal to view files, but if you just need to read a file, the less command comes in handy.

Less path_to_file
During a session with less, you can search for the desired fragments of text by keywords, navigate through pages, output data with line numbers, and so on.

12. Reusing the last element from the previous command with !$

In many situations, it is very useful to be able to use the argument of the previous command.

Suppose you need to create a directory, and then go to it. Here you can use the !$ option by entering the jump command after the directory creation command.


Use!$ to use last command argument

Even better, the same is done using the keyboard shortcut Alt +. (dot). The dot can be used multiple times to iterate over the options of the last command.

13. Using the previous command in the current command with!!

By using!! you can call the entire previous command. This trick is especially useful when you need to execute a command and it turns out that you need superuser privileges to execute it. For example, the figure below shows a situation in which the sudo !! allows you to save a lot of time.


Take advantage!!! to substitute the last command as an argument

14. Using aliases to fix typing errors

You may already be familiar with the alias command. It can be used to correct errors in entered commands.

For example, it may happen that you frequently type gerp instead of grep . If you can't stop the bad habit, add an alias to your bashrc file like this:

Alias ​​gerp=grep
Now you will not have to retype this command if you enter its name incorrectly.

15. Copy and paste in Linux terminal

This advice is somewhat ambiguous as it depends on the Linux distribution and on the terminal.

But usually the copy and paste commands can be invoked like this:

  • Select the text you want to copy and click right click mouse to paste it (works in Putty and other SSH clients for Windows).
  • Select the text you want to copy, then click central button mouse (scroll wheel) to paste it.
  • Use the keyboard shortcuts Ctrl + Shift + C to copy, and Ctrl + Shift + V to paste.

16. Terminate a team or process

Perhaps what I want to advise here is quite obvious, however, I’ll tell you about it anyway. If a command is running in the foreground and you want to exit it, you can press Ctrl + C to stop it.

17. Using the yes Command to Automate Interactive Commands or Scripts

If you have to work with some commands or scripts that involve user interaction, and you know that you will be typing Y for every question in the program, you can automate this process with the yes command. It is done like this:

Yes | command_or_script

18. Clearing the contents of a file without deleting the file itself

If you want to clear the content text file without deleting the file itself, you can use the following command:

> filename

19. Finding out if a directory contains files containing certain text

The Linux command line supports many ways to find information. However, if you just want to know if there are files in a directory that contain certain text, you can use this command:

Grep -Pri Search_Term path_to_directory
However, I advise anyone who needs to search in Linux to understand properly with find command.

20. Calling help for any command

I want to end this material with one very obvious, but nevertheless very important "trick", which is to call the help for a command or tool command line.

Almost all command line tools contain background information, revealing the features of their use. Usually, reading help helps you understand the basics of a command. You can call help like this:

Command_tool --help

I intentionally didn't include commands like fuck in this material, as they are not the kind of standard means which can be found literally everywhere. What I have said here can be useful when working with almost all Linux distributions and command-line shells without the need to install new programs.
Free translation: Alexey Dmitriev
Transfer date November 19, 2007

We're talking about the GNU/Linux text console here - not what appears in the terminal window, or X-window manager, or desktop. There is so much confusion about this that I have to trivially say: - "If you press "Alt-Ctrl-F1" (or "F2", etc.) while in graphics mode, then you get into the text console" .

0. What is it about?

The computer is loading. You are in a text console, watching the text scroll down quickly. You want to view a piece of this text that has already flashed by. You press "Shift-PgUp" or "Shift-PgDn", and if you have already changed the default keyboard shortcuts (no, no, not the X keyboard layout), then some other keys. With these keys you make the text move up and down the screen. IN this moment you are using a scrollback buffer (let's call it the scrollback buffer for short).

When you start using this buffer with might and main, two things become clear very soon:
Only a portion of the text accompanying the download may be returned.
The file "/var/log/messages" captures much less than what you wanted to see.

You start scouring the web, and soon preliminary results appear: the postings on scrollback in Linux are very ancient and can be traced back to the early 90s. However, everything before the 2.6.x kernel should be forgotten.

Indeed, you should be selective in what you read: most of the advice that applies to versions prior to 2.6.x is likely to confuse you and lead you astray. Trust me, I've wasted a lot of time on this case. Scrollback behavior is defined in the file "vc.c", an otherwise small file in the kernel sources, with "vc" standing for "virtual console".

Note that when switching consoles, the scroll buffer is lost - so the full amount of this buffer is available in the current console. (There is especially a lot of information garbage here from the time before the 2.6.x kernel).

By default, the buffer size is 32K. This is enough for 4 reverse scroll key presses - each press scrolls half the screen, 25 lines per screen. It comes out to about 50 lines. And you won't get more, even if you have 30 or 50 lines per screen. I must say that 50 lines is a small fraction of what flies across the screen during loading. In connection with the above, two questions arise:

How to increase the scroll buffer?

Why don't the logs capture some of the messages you saw during the download?

1. How to increase the scrollback buffer?

The first solution is to increase the default (default) buffer size in the kernel sources and recompile it. Let me assume that you are as reluctant to do this as I am and look for a more flexible medium.

And there is such a tool, but it is called "framebuffer console", for short "fbcon". This device has a documentation file "fbcon.txt"; if you installed the documentation for the kernel, then you have it. Look for it somewhere in the "/usr/share" branch (I can't give the exact path due to distribution differences). You can always download it as a single file.

At this point, I apologize: we have to digress a bit and talk a little about the framebuffer.

The video buffer is a buffer between the display and the video adapter. Its beauty is that it can be manipulated: it allows tricks that would not work if the adapter was connected directly to the display.

One such trick is related to the scroll buffer; turns out you can "ask" the video buffer to allocate more memory to the scroll buffer. This is achieved through the boot parameters of the kernel. First you require a framebuffer (video buffer); Then you request a larger scroll buffer.

The following example is for GRUB but can be easily adapted to LILO. In file GRUB settings- "menu.lst" - find the line corresponding to the kernel, and then:
Remove the "vga=xxx" option if present.
Add the option "video=vesabf" or whatever matches your hardware.
Add the "fbcon=scrollback:128" option.
After this procedure, the kernel parameter line should look something like this:
"kernel /vmlinuz root=/dev/sdb5 video=radeonfb fbcon=scrollback:128".

The question is, why remove the "vga=xxx" option? Due to possible conflicts with the video option. On my ATI adapter, I can't change the scroll buffer if "vga=xxx" is listed. Perhaps in your case this is not the case. If the above options work - good; but what if you want to increase the number of lines, or set a smaller font on the screen? You always did it with the "vga=xxx" option - and that's what disappeared. Don't worry - the same can be achieved by changing the fbcon parameters as described in the "fbcon.txt" file (but not described in this article). Ubuntu and Debian have more convenient way: "dpkg-reconfigure console-setup". This command is also worth using because it also updates "initrd" to your new settings.

2. Pushing limits

The "fbcon=scrollback:128" option gives you the ability to navigate the scrollback buffer 12-13 screens. That's about 150 lines - not bad, but probably not enough to see all the boot messages. Is it possible to bet more than 128, say 256? The limit of 128 is in "vc.c". If you want more, edit and compile. For myself, I decided that the game is not worth the candle.

Just when I decided that I had reached the highest limit - I quote from "fbcon.txt":
fbcon=scrollback: [k]
...
The "k" suffix is ​​optional, and will multiply the "value" by 1024.
(The "k" suffix is ​​optional, and will multiply "value" by 1024 times).
I rushed to check ... and no matter what the author thought about this, he was convinced that 128 and 128k give exactly the same result. The default limit is 128KB of memory, that's the whole story.

Finally, note that using the extended scroll buffer uses both graphics adapter memory and a buffer in random access memory. If you're content with the default 32KB, you're only using the video adapter's memory. In practice, however, I did not notice the slightest slowdown.

3. What is missing in the logs?

On Ubuntu and Debian and other distributions, all system messages are stored in the "/var/log/messages" file. This applies even if the logging daemon is not the old "syslog" but "syslog-ng" ("ng": "new generation").

In both cases, you can view the messages by typing "dmesg" at the command line. It doesn't help: you clearly saw messages while loading that are not in the output of the "dmesg" command. How so?

This is a feature, not a bug! The messages logged to the file come from various OS subsystems. The subsystems that issue messages to the log file are referred to in syslog terminology as "facilities" ("means" or "services"), there are only 8 of them. During the download process, programs and scripts work that may not belong to any of the 8 "tools". We see their messages scroll across the screen, but nothing will be recorded in the log file!

For example, you will not see messages from "loadkeys" (openSUSE) or "consolechars" (Ubuntu and Debian) programs when loading a keyboard layout during system boot. Another example: when using the console text editor, you can scroll the display backwards (including colors) while using the scroll buffer. But what the editor produces will never get into any system protocol. The reason, of course, is that neither "loadkeys", nor "consolechars", nor the editor belong to any of the 8 "tools".

Is it possible to change the situation? Of course - "just" remake and recompile those applications that you want to log. Add or rewrite boot scripts. Or have the system capture messages from outside the 8 system "tools".

I bet you won't do it. And I even know your probable answer:
"Nothing is missing from my logs." Yes of course...

If you liked this article, please share it with your friends:

Screen - very handy program, if you often work in the console by connecting to the server via SSH. In this case screen allows you to keep several different screens in one console, conveniently switching between them.

Screen allows you to disconnect from SSH while saving tasks running on a remote server.

To install on Ubuntu you need to run the command

$ sudo apt-get installscreen

After that, you need to run screen:

$ screen

After starting, you will see either a command line prompt or a "welcome screen" by pressing SPACE or ENTER in which you will also be taken to the shell. In this case, all commands will be launched already “inside” the screen.

In order to exit the screen (exit, not disconnect), it is enough to exit all shells open in it, or, alternatively, press the key combination Ctl-a \, and answer "y" to the question "do you really want to exit" .

You can detach from the current screen without closing or ending the session by pressing Ctrl-a d.

Connecting to screen

If there is already a screen session running on the machine and you want to connect to it, then

  1. if there is only one session, a simple command is enough: $ screen –x
  2. if there are several sessions, then: you can see the list of running screens with the screen -ls command: $ screen -ls There are screens on: 2762.pts-0.debian (Detached) 2743.pts-0.debian (Detached) 2 Sockets in /var/run/screen/S-diesel.

    We select the screen we need, and join it:

    Screen -x 2762.pts-0.debian

    The names of the sessions by default are not very informative - just process numbers, if you want to run and use several screen sessions for different tasks, you can come up with more sane names. We come up with a name, then we start a new screen like this:

    $ screen -S "job1"

    where job1 is the name of our "named" session. Now in -ls we will see much clearer:

    $ screen -ls There are screens on: 2762.pts-0.debian (Detached) 2795.job1 (Detached) 2743.pts-0.debian (Detached) 3 Sockets in /var/run/screen/S-diesel.

    and we can connect to the job1 session by simply specifying its name:

    $ screen -x job1

Switching between screens

As you already understood, sessions are saved even when you are disconnected from the server, and everything that is running in them continues to be executed. However, the possibilities of screen are not limited to this. Sometimes, when working via ssh, I really want to have several terminals open at once, while connecting several times is a bummer - especially if you need to connect through several servers. Screen can help with this too. Just press Ctrl - a c and another shell will open. You can move between open shells either through Ctrl - a<Цифра>, Where<Цифра>- open shell number, starting from zero, or Ctrl - a n / Ctrl - a p - forward backwards. You can get a list of open "windows" - consoles (and switch to the desired one), using the key combination Ctrl - a "; to give a normal name (and not just boring "bash") for a thread of the window, go to it and press Ctrl - a A (case sensitive), then at the prompt that appears, enter a name.

Command execution monitoring

If you have multiple windows open, it can be useful to monitor activity in one of them. For example, if you run a "long-running" command and are interested in knowing when it will complete. We go to the window with this command, press Ctrl - a m, we see an inscription like this: "Window 0 (bash) is now being monitored for all activity". When something changes in a window, screen will beep and the window list will show an "@" after the number.

Screen scrolling in screen

To view the output of a command that has gone above the window borders, you need to press Ctrl-a [

This will switch screen to Copy mode. To exit this mode, simply press Esc.

screen configuration

Screen is configured using the ~/.screenrc file in your home directory. For example, you can write there:

Caption always "%(= 45)%(+b w)Screen: %n | %h %=%t %c" hardstatus alwayslastline "%-Lw%(= BW)%50>%n%f* %t%( -)%+Lw%<"

This will give you an all time visible list open windows, and other useful information at the bottom of the screen.

Also, when Screen is launched, by default, commands from the /etc/screenrc file and the .screenrc file from the user's home directory are executed. Many of the commands that can be used in configuration files have been discussed in the tables above in the "Text command" field. Below is an example Screen configuration from a .screenrc file:

# Disable displaying license information at startup Screen startup_message off # Open a shell to run chdir screen -t Work # Open a shell to manage configuration chdir /etc screen -t Configuration # Open a shell to view logs chdir /var/log screen -t Logs # Select first window after start select 0

This fairly simple configuration file opens three windows named Work, Configuration, Logs, respectively in the user's home directory, in the /etc directory, and in the /var/log directory. After launch, the first window named Work will be on the screen. Figure 1 shows an example of how Screen works with this configuration file.

Completion Screen

If for some reason the screen session stops responding, you can end it. To do this, you need to connect to the desired session, then press Ctrl - a and enter the command ":quit".

Screen will end this session, forcibly closing all windows.

Working with Screen

One of Screen's main features is the creation and management of multiple console windows, or text-based applications, in a single terminal window. At one time, only one of the windows can be on the terminal screen (there can be several of them only if they divide the terminal screen into parts). Screen allows you to quickly switch between windows, save and later return to a text session and share it with other users. Plus, Screen allows you to give windows titles, keep track of activity in each window, and share the contents of windows using copy and paste. And, in addition, Screen allows you to remap keys, manage terminal emulation settings, and do many other small text box tasks that may not be a constant need, but which can help perform administrative work at certain points in time.

The figure shows a Screen with three named windows (Work, Configuration, Logs) and the text editor Vim opened in it, with an example configuration file Screen.

First start

Screen can be launched with the screen command, but first check if Screen is installed on your system with the following command:

$ which screen /usr/bin/screen

If the command returns the path as in the example above, you can start using it right away, otherwise Screen can be installed using the standard package support tools of your operating system, or downloaded from the official website http://www.gnu.org/software/screen/ .

Now we can run Screen by simply typing:

$ screen

After launch, information about the version and license may appear on the screen (when considering the configuration, we will consider how you can cancel the display of this message) and in this case you need to press the "Space" or "Enter" key, after which we get into, at first glance, the usual , a text wrapper. It should be noted that on the command line you can immediately specify a command to launch a specific application, for example, to launch top, you need to type:

$ screen top

Okay, now Screen is running, but at first glance, there are no external changes - we can work in a running shell, or use top. The trick is that, while running, Screen passes all keystrokes directly to the application, but (by default) leaves for itself control commands that begin with the key combination Ctrl + a .

This control key combination can be changed through the configuration files (which we will discuss below), but in all examples only the default configuration will be considered. Thus, all Screen keyboard commands consist of two key combinations - the control key Ctrl + a and the key itself, or a key combination representing the command. Almost every command also has its text counterpart, which can be executed either through a configuration file or by running the following command:

In this case, we have to execute two keyboard shortcuts by first pressing Ctrl + a and then the colon key. In this case, Screen will display a command prompt in the form of a colon at the bottom of the screen. The same command that we just ran with a keyboard shortcut can be run through the configuration file using the colon text command, which may, for example, be needed to display a command prompt at startup, or remap the key combination that causes this prompt. Below, as in the example above, for each key combination I will write the keywords corresponding to them, which can be used in configuration files, or entered using Ctrl + a and:.

A legitimate question may arise here - what if the application needs to directly pass the key combination Ctrl + a , which is already used in Screen as a service key? For example, the same combination is used by the readline command-line editing library to jump to the beginning of a line. For this case, Screen provides the following command:

Window management

In the previous section, we learned how to launch Screen and learned the basic template for all key commands. It's time to deal with the commands for managing windows. When Screen starts, it creates a single window (unless the configuration specifies to create multiple windows at once) and assigns it the number 0. A new command shell window can be created with the command below:

KEYBOARD TEXT COMMAND DESCRIPTION
ctrl+a c screen Create a new window

In this case, Screen will create a new window and assign it the next number in order. In our case, the new window will be window number 1.

Now we already have two text windows, of which the newly created window number 1 is the current one, and window number 0 is in the background. But, it should be noted that even if the window is in the background, it can perceive the output of the application running in it, as if it were the current one. For example, we could start a process in window 0 that outputs additional information at regular intervals (for example, this could be the tail -f command) and then switch to window 1 to work in a text editor. After a while, if we want to see what is happening in window 0, we can switch to this window and see all the output of the process running there. You can then switch back to the editing window.

In the previous paragraph, I described an example with switching between windows, but did not say yet what commands are used for this. Let's take a closer look at the most commonly used commands for managing windows:

KEYBOARD TEXT COMMAND DESCRIPTION
ctrl+a w windows Lists the numbers and titles of all windows on the bottom line of the screen
ctrl+a « windowlist Displays an interactive list of numbers and titles of all windows from which you can select a window to switch
ctrl+a window number select Select window by number. For example, the command Ctrl + a and 0 makes the current window number 0
Ctlr+a n next Switch to the next window in order, or to the first window if we were in the last window
ctrl+a p prev Switch to the previous window in order, or to the last window if we were in the first window
ctrl+a select Display a prompt for entering the window number to switch
ctrl+a ctrl+a other Switch to last used window
ctrl+a N number Show current window number and title
ctrl+a A title Set the title of the current window
ctrl+a k kill Force close the window, for example, if the application cannot be closed in the usual way

When one of the windows is closed, the numbers of other windows do not change, and "holes" appear in the numbering, which can later be filled with new windows. Although Screen has a command to change the window number, it is not considered here and is left for independent study. If all windows are closed, Screen exits and displays a message:

Work with text

Above, we learned how to launch Screen and manage windows - create them, switch from window to window, assign names and close them. Now let's look at working with the contents of windows - copying and pasting the text content of one window into another, monitoring windows, saving a text copy of the screen, and other similar tasks:

KEYBOARD TEXT COMMAND DESCRIPTION
ctrl+a M monitor Enable/disable activity monitoring for the current window. When monitoring is enabled and text is displayed in a window in the background, information about activity is displayed in the service line.
silence Enable/disable inactivity monitoring for the current window. When monitoring is enabled and no text is displayed in a window that is in the background for a certain period of time, information about the lack of activity is displayed in the service line.
ctrl+a h hardcopy Write a text image of the current window to the file hardcopy.window_number in the Screen directory by default.
ctrl+a H log Enable/disable tracing of the text of the current window to the screenlog.window_number file in the Sccreen directory by default.
ctrl+a [ copy Enter copy mode. Additional commands for this mode are described below.
ctrl+a ] paste Paste text into the window from the default register (where it was previously copied using the command above).

In copy mode, Screen launches a text editor similar to the Vi editor. The most commonly used editor commands are:

  • The cursor keys move the cursor across the screen buffer;
  • The spacebar marks the start and end of the copy region. Pressing Space a second time, the marked text is copied into the default case, and Screen exits its copy mode;

Multiple windows on the screen

By default, Screen leaves only one logical window visible on the screen, but if desired, there is an additional option to divide the terminal screen into horizontal regions. Thus, it is possible to display several windows at once on one terminal screen. Let's take a closer look at the commands for working with screen regions:

Figure 2 shows a Screen with three named windows (Work, Configuration, Logs) and a screen divided into two regions. In the upper region, the Work window is open with the Vim text editor and an example Screen configuration file. In the lower region, the Configuration window with part of the general configuration file /etc/screenrc.

Console sessions

Finally, we'll look at one of the most popular features of Screen. Let's imagine the following situation: a system administrator, while at home, logs into a remote server, opens several windows, starts the necessary tasks, then closes the connection and goes to work. At work, he again goes to the same server and restores the windows open at home with the results of the tasks performed. Sounds a little fantastic? In no case! The Screen console sessions are designed to do just that. They allow you to disconnect the current Screen session with all windows and running applications from the terminal and then reattach it at any time. At the same time, all applications running under Screen will continue to work and use their windows to display information. Plus, there can be several console sessions, but in this article we will not dwell on this in detail.

Consider the commands for disconnecting a console session from a terminal:

KEYBOARD TEXT COMMAND DESCRIPTION
ctrl+a d detach Detach the console session and leave it running in the background. At the same time, Screen writes:
ctrl+a D D pow_detach Detach the console session, leave it running in the background, and send the parent Screen process a HANGUP signal. When running Screen from a remote terminal, this means disconnecting Screen and then closing the connection. At the same time, Screen writes:
ctrl+a z suspend Detach the console session and leave it running as a background process of the parent shell. In this case, in addition to the commands described below, standard commands for managing background processes of the shell can be used to restore the session.
ctrl+a ctrl + quit Close all session windows and exit Screen. It should be noted that many distributions redefine the key combination for this command, but the text command can be used anyway

To reattach to a disconnected session, or to view a list of available sessions, you need to launch Screen with one of the following switches:

What's next?

Screen has a huge number of different features and it is almost impossible to describe them all in one article, so some of the functionality was left behind the scenes. Here are some Screen features/settings that weren't covered above and are left for independent study:

  • Commands for setting up terminal emulation;
  • Multiuser console sessions;
  • Assignment or reassignment of keyboard shortcuts to commands;
  • Auxiliary options for text commands. For example, the -t option of the screen command, which is used in the sample configuration file;
  • Rarely used commands, for example, commands that can be used when setting up the launch of Screen - echo and sleep;
  • Environment variables that can be useful when configuring Screen;
  • Additional launch keys Screen;

screen alternatives

Screen is a program with a long history, it should be noted that the first version appeared in 1987. At the moment, almost no new features are added to Screen (one of the reasons may be the presence of a large number of them at the moment) and, basically, only bug fixes are made. If someone does not like the relative "old age" of Screen, or does not need such a large number of features, then you can turn to one of the following programs:

  • dtach () - the main emphasis in the development of this program is on compactness. Similar to Screen, it supports the ability to work with console sessions, but does not have other Screen features.
  • SplitVT (http://www.devolution.com/~slouken/projects/splitvt/) - allows you to split the terminal screen into two halves and work with two software shells at once.
  • Tmux() - Tmux uses a client-server model to work with console sessions.
  • Twin (http://linuz.sns.it/~max/twin/) - an attempt to create a text-based window interface similar to graphic counterparts.

If you notice an error, select a piece of text and press Ctrl + Enter
SHARE: