Zsh (Magyar)


Zsh (Magyar)

For an alternative using pacman's native functionality, see [[#pacman -F "command not found" handler]].

Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being compatible with the POSIX sh (not by default, only if issuing ), it offers advantages such as improved tab completion and globbing.

The Zsh FAQ offers more reasons to use Zsh.

Before starting, users may want to see what shell is currently being used:

Install the zsh package. For additional completion definitions, install the zsh-completions package as well.

Make sure that Zsh has been installed correctly by running the following in a terminal:

You should now see zsh-newuser-install, which will walk you through some basic configuration. If you want to skip this, press . If you did not see it, you can invoke it manually with:

Change your shell to . See Command-line shell#Changing your default shell.

When starting, Zsh will read commands from the following files in this order by default, provided they exist.

See the graphic representation.

Although Zsh is usable out of the box, it is almost certainly not set up the way most users would like to use it. But due to the sheer amount of customization available in Zsh, configuring Zsh can be a daunting and time-consuming experience. For automatic configuration, see #Third-party extensions.

Included below is a sample configuration file. It provides a decent set of default options as well as giving examples of many ways that Zsh can be customized. In order to use this configuration save it as a file named .

Perhaps the most compelling feature of Zsh is its advanced autocompletion abilities. At the very least, enable autocompletion in . To enable autocompletion, add the following to your :

The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work, users must not enable ssh's hostname hashing (i.e. option in ssh client configuration).

For autocompletion with an arrow-key driven interface, add the following to:

To activate the menu, press twice.

For enabling autocompletion of privileged environments in privileged commands (e.g. if you complete a command starting with sudo, completion scripts will also try to determine your completions with sudo), include:

You can write custom completions on your own. Should you do so, you can refer to the zshcompsys(1) man page.

Note that the official documentation can be hard to read. You can consider trying the simpler zsh-completion-howto tutorial for an easy start.

Key bindings are assigned by mapping an escape sequence matching a keypress to a ZLE widget. The available widgets, with descriptions of their actions and their default keybindings, are listed in zshzle(1) § STANDARD WIDGETS and zshcontrib(1) § ZLE FUNCTIONS.

The recommended way to set key bindings in Zsh is by using string capabilities from terminfo(5). For example[1][2]:

You need to set up the array and make sure that ZLE enters application mode to use the following instructions; see #Key bindings.

To enable history search add these lines to file:

By doing this, only the past commands matching the current line up to the current cursor position will be shown when or keys are pressed.

Zsh offers the options of using a prompt theme or, for users who are dissatisfied with the themes (or want to expand their usefulness), the possibility to build a custom prompt.

Prompt themes are a quick and easy way to set up a colored prompt in Zsh. See zshcontrib(1) § PROMPT THEMES for information about prompt themes and how to write your own theme.

To use a theme, make sure that prompt theme system is set to autoload in . This can be done by adding these lines to:

Available prompt themes are listed by running the command:

For example, to use the theme, enter:

To preview all available themes, use this command:

It is possible to install themes manually, without external configuration manager tools. For a local installation, first create a folder and add it to the array, eg:

Now create a symbolic link of your theme file in this folder:

If instead you wish to install a theme globally, do:

In addition to adding a prompt theme through its own file, it is possible to add themes from within another file (like your ), eg:

All prompts can be customized with prompt escapes. The available prompt escapes are listed in zshmisc(1) § EXPANSION OF PROMPT SEQUENCES.

Zsh sets colors differently than Bash; You do not need to use profuse ANSI escape sequences or terminal capabilities from terminfo(5). Zsh provides convenient prompt escapes to set the foreground color, background color and other visual effects; see zshmisc(1) § Visual effects for a list of them and their descriptions.

Colors can be specified using a decimal integer, the name of one of the eight most widely-supported colors or as a # followed by an RGB triplet in hexadecimal format. See the description of fg=colour in zshzle(1) § CHARACTER HIGHLIGHTING for more details.

Most terminals support the following colors by name:

Color numbers 0-255 for terminal emulators compatible with xterm 256 colors can be found in the xterm-256color chart.

This is an example of a two-sided prompt with color:

And here is how it will be displayed:

To use colors from the 16-255 range and 24-bit true color, you can use the number from 0 to 255 assigned to the wanted color and its hexadecimal color code, respectively:

Many programs change the terminal state, and often do not restore terminal settings on exiting abnormally (e.g. when crashing or encountering SIGINT).

This can typically be solved by executing reset(1):

The following sections describe ways to avoid the need to manually reset the terminal.

The ttyctl command can be used to "freeze/unfreeze" the terminal. To freeze the interactive shell on launch, use the following:

Alternate linedrawing character set can screw up the terminal in a way which ttyctl cannot prevent.

A simple solution is to output the escape sequences that reset the terminal from the hook function, so that they are executed every time before the prompt is drawn. For example, using the escape sequence :

To test if it works, run:

Zsh can be configured to remember the DIRSTACKSIZE last visited folders. This can then be used to cd them very quickly. You need to add some lines to your configuration file:

Now use

to print the dirstack. Use to go back to a visited folder. Use autocompletion after the dash. This proves very handy if using the autocompletion menu.

cdr allows you to change the working directory to a previous working directory from a list maintained automatically. It stores all entries in files that are maintained across sessions and (by default) between terminal emulators in the current session.

See zshcontrib(1) § REMEMBERING RECENT DIRECTORIES for setup instructions.

zoxide is a smarter cd command that lets you navigate anywhere in just a few keystrokes. It remembers your frequently used directories and uses a scoring mechanism to guess where you want to go.

Assistant functions have to be enabled separately:

For example, command will now open the man page git-commit(1) instead of git(1).

Typically, compinit will not automatically find new executables in the . For example, after you install a new package, the files in would not be immediately or automatically included in the completion. Thus, to have these new executables included, one would run:

This keeps the modification date of the file consistent with the last time a package was installed, upgraded or removed. Then, must be coaxed into rehashing its own command cache when it goes out of date, by adding to your :

An alternate method, that will keep everything you entered in the line before calling application:

Key binds like those used in graphic file managers may come handy. The first comes back in directory history (), the second let the user go to the parent directory (). They also display the directory content.

If your terminal emulator supports it, you can set its title from Zsh. This allows dynamically changing the title to display relevant information about the shell state, for example showing the user name and current directory or the currently executing command.

Some terminal emulators and multiplexers support setting the title of the tab. The escape sequences depend on the terminal:

See a repository about shell environment detection for tests to detect the shell environment. This includes login/interactive shell, Xorg session, TTY and SSH session.

pacman includes functionality to search for packages containing a file. The following command-not-found handler will use pacman directly to search for matching packages when an unknown command is executed.

For an alternative using pkgfile, see #pkgfile "command not found" handler.

By default, the clear screen keybinding will not clear the backbuffer (the part you need to scroll up for to see it) on most terminal emulators. A possible solution to this problem is the following.

Fish provides very powerful shell syntax highlighting and autosuggestions. To use both in Zsh, you can install zsh-syntax-highlighting, zsh-autosuggestions, and finally source one or both of the provided scripts from your zshrc:

pkgfile includes a Zsh script file that provides a function that will automatically search the pkgfile database when entering an unrecognized command.

You need to source the script to enable it. For example:

For an alternative using pacman's native functionality, see #pacman -F "command not found" handler.

Previous articleNext article

POPULAR CATEGORY

misc

16570

entertainment

17672

corporate

14673

research

8963

wellness

14512

athletics

18528