User Tools

Site Tools


Sidebar

os_cp:tba

Editing the command line

Bash (as well as numerous other programs) uses the readline library to read commands from standard input.
Lots of software use for the same purpose the libedit library due to licensing reason.

The readline library is renown for providing comfortable, efficient and powerful editing capabilities within the input line as well as its built-in support for history of previously read lines.

A reasonable selection of keyboard shortcuts for line editing is available as a cheat sheet.
The full list of commands and key sequences bound to them is available in the system manual (man readline i man bash).
To write down the key sequences, the following syntax is used:

  • ^x or C-x stands for Ctrl+x (watch out for upper case letter:^X or C-X is Ctrl+Shift+x),
  • M-x usually maps to Alt-x (see https://en.wikipedia.org/wiki/Meta_key for more explanations),
  • a b stands for pressing key sequence a followed by key sequence b.

Bash by default appends the history of commands to the file ~/.bash_history upon exit.
History (read from the ~/.bash_history file and extended by history of commands executed in current shell) can be displayed with the history command. (Consult man bash_builtins to learn its switches.)

For a more convenient history browsing one can leverage the hstr command.

Exercise 1 What key combination clears the screen?

Exercise 2 What key combination search the history backwards and forwards?
(Be careful around the forward search key combination – it might overlap with flow control; if your terminal freezes, use ^q to unfreeze it and issue stty -ixon to disable flow control.)

Exercise 3 What key combination moves the caret by one word (forward and backward)?

Exercise 4 Which key sequence moves the caret by five words?

Exercise 5 What key combination inserts ('yanks') last word of the previous line in the history?

Exercise 6 Which key sequence inserts ('yanks') third word (counting from the end) of the previous line in the history?

Exercise 7 What key combination transposes this word with the one preceding it?

Console text editors

vi & vim

In many Linux distributions the only preinstalled text editor is the vi program.

vim is an improved version of vi.

The vim editor is accompanied with a tutorial. To start it, one has to run the vimtutor command.

vi is even a part of the POSIX standard.
It is a part of minimalistic toolboxes such as busybox and toybox.

Exercise 8 Complete the first lesson of the vimtutor.

Exercise 9 Complete all lessons of the vimtutor.

nano

Nano is a text editor with basic functions that still suffice to accomplish most tasks painlessly.

Basic keyboard shortcuts for nano are always displayed on the bottom of the screen.

Fore fill documentation visit: https://www.nano-editor.org/docs.php

Emacs

Emacs is an eternal rival of vim in the aspect of editing text files in terminal.

After starting emacs one can start a tutorial using a key sequence Ctrl+h t.

Other

os_cp/tba.txt · Last modified: 2023/04/02 11:24 by jkonczak