Z shell (zsh)

The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.

Zsh was created by Paul Falstad in 1990 while he was a student at Princeton University. It combines features from both ksh and tcsh, offering functionality such as programmable command-line completion, extended file globbing, improved variable/array handling, and themeable prompts.
— Wikipedia

Shell is the interface between a user and the system itself. Which main layers it consists of, first layer is program:

+----------------+
|  Program/User  |
+----------------+
|  Shell         |
+----------------+
|  Kernel        |
+----------------+
|  Hardware      |
+----------------+

Learning path

Basic keyboard bindings

bindkey -L: List current shortcuts

  • C-r: Reverse search history
  • C-p: Previous command in history
  • C-n: Next command in history
  • /: Search backward in history
  • C-i: command completion like Tab
  • C-l: Clear the screen (same as clear command)
  • C-c: kill whatever is running
  • C-d: Exit shell (same as exit command when cursor line is empty)
  • C-z: Place current process in background

ZSH vi mode

=command - Locate path of executable or command, can be used to run some aliased program itself, =watch

Use ESC or CTRL-[ to enter Normal mode. In Normal mode you can use vv to edit current command line in an editor (e.g. vi/vim/nvim…), because it is bound to the Visual mode.

You can change the editor by ZVM_VI_EDITOR option, by default it is $EDITOR.

Movement

  • $: To the end of the line
  • ^: To the first non-blank character of the line
  • 0: To the first character of the line
  • w: [count] words forward
  • W: [count] WORDS forward
  • e: Forward to the end of word [count] inclusive
  • E: Forward to the end of WORD [count] inclusive
  • b: [count] words backward
  • B: [count] WORDS backward
  • t{char}: Till before [count]‘th occurrence of {char} to the right
  • T{char}: Till before [count]‘th occurrence of {char} to the left
  • f{char}: To [count]‘th occurrence of {char} to the right
  • F{char}: To [count]‘th occurrence of {char} to the left
  • ;: Repeat latest f, t, F or T [count] times
  • ,: Repeat latest f, t, F or T in opposite direction

Insertion

  • i: Insert text before the cursor
  • I: Insert text before the first character in the line
  • a: Append text after the cursor
  • A: Append text at the end of the line
  • o: Insert new command line below the current one
  • O: Insert new command line above the current one

Surround

There are 2 kinds of keybinding mode for surround operating, default is classic mode, you can choose the mode by setting ZVM_VI_SURROUND_BINDKEY option.

  1. Classic mode (verbssurround)
  • S": Add " for visual selection
  • ys": Add " for visual selection
  • cs"': Change " to '
  • ds": Delete "
  1. S-prefix mode (sverbsurround)
  • sa": Add " for visual selection
  • sd": Delete "
  • sr"': Change " to '

Note that key sequences must be pressed in fairly quick succession to avoid a timeout. You may extend this timeout with the ZVM_KEYTIMEOUT option

How to select surround text object?

  • vi": Select the text object inside the quotes
  • va(: Select the text object including the brackets

Then you can do any operation for the selection:

  1. Add surrounds for text object
  • vi" S[ or sa[ "object" "[object]"
  • va" S[ or sa[ "object" ["object"]
  1. Delete/Yank/Change text object
  • di( or vi( d
  • ca( or va( c
  • yi( or vi( y

Search in current line

To search across the line, you can use f and F to search for the next and previous occurrence of a character on the current line.

You can combine this with ; and , to repeat the search in the same and the opposite direction and 0 to jump to the beginning of the line and then do search.

0f{char} - search for the first occurrence of {char} on the current line 5f{char} - search for the fifth occurrence of {char} on the current line 0f{char}; - search for the first occurrence of {char} on the current line and then repeat the search in the same direction.