How to use the command tput (with examples)

How to use the command tput (with examples)

The tput command allows users to view and modify terminal settings and capabilities. It is useful for interacting with the terminal in various ways such as moving the cursor, changing colors, and obtaining terminal information.

Use case 1: Move the cursor to a screen location

Code:

tput cup row column

Motivation: Moving the cursor to a specific screen location can be useful for command-line applications that require precise positioning or for creating complex ASCII art.

Explanation: The cup capability of tput is used to move the cursor to the specified row and column on the screen. The row argument specifies the row number (starting from 0), and the column argument specifies the column number (starting from 0).

Example output: Suppose we want to move the cursor to the 10th row and the 20th column. We can use the following command:

tput cup 10 20

This will move the cursor to the specified location on the screen.

Use case 2: Set foreground or background color

Code:

tput setaf|setab ansi_color_code

Motivation: Changing the foreground or background color of the terminal can be useful for highlighting specific text or creating visually appealing interfaces.

Explanation: The setaf capability of tput is used to set the foreground color, while the setab capability is used to set the background color. The ansi_color_code argument specifies the desired color. ANSI color codes range from 0 to 255, where the lower numbers represent basic colors and the higher numbers represent more complex colors.

Example output: Suppose we want to set the foreground color to red. We can use the following command:

tput setaf 1

This will set the foreground color to red.

Use case 3: Show number of columns, lines, or colors

Code:

tput cols|lines|colors

Motivation: Knowing the number of columns, lines, or colors supported by the terminal can be useful for designing applications that adapt to the terminal’s capabilities.

Explanation: The cols capability of tput is used to display the number of columns in the terminal. The lines capability is used to display the number of lines, and the colors capability is used to display the number of colors supported by the terminal.

Example output: Suppose we want to know the number of columns in the terminal. We can use the following command:

tput cols

This will display the number of columns in the terminal.

Use case 4: Ring the terminal bell

Code:

tput bel

Motivation: Ringing the terminal bell can be useful for capturing the user’s attention or signaling the completion of a task.

Explanation: The bel capability of tput is used to ring the terminal bell.

Example output: When the following command is executed:

tput bel

It will ring the terminal bell, producing an audible alert.

Use case 5: Reset all terminal attributes

Code:

tput sgr0

Motivation: Resetting all terminal attributes can be useful for ensuring that the terminal is in a consistent state after making changes.

Explanation: The sgr0 capability of tput is used to reset all terminal attributes.

Example output: When the following command is executed:

tput sgr0

It will reset all terminal attributes, reverting the terminal to its default state.

Use case 6: Enable or disable word wrap

Code:

tput smam|rmam

Motivation: Enabling or disabling word wrap can be useful for controlling how text is displayed in the terminal.

Explanation: The smam capability of tput is used to enable automatic margins and enable word wrap, while the rmam capability is used to disable automatic margins and disable word wrap.

Example output: To enable word wrap, we can use the following command:

tput smam

This will enable word wrap in the terminal.

Related Posts

How to use the command "mk" (with examples)

How to use the command "mk" (with examples)

The “mk” command is a task runner that is primarily used to control the compilation of an executable from source code.

Read More
Using the "eget" Command to Easily Install Prebuilt Binaries (with examples)

Using the "eget" Command to Easily Install Prebuilt Binaries (with examples)

1: Downloading a prebuilt binary for the current system from a repository on GitHub eget zyedidia/micro Motivation: You want to quickly download a prebuilt binary for the popular text editor Micro from its GitHub repository, without the need to compile it yourself.

Read More
How to use the command git worktree (with examples)

How to use the command git worktree (with examples)

The git worktree command is used to manage multiple working trees attached to the same repository.

Read More