Jump to content

Zsh: Difference between revisions

2,259 bytes added ,  Monday at 06:36
m
Removed numbering
imported>Flexagoon
(Create a zsh page)
 
m (Removed numbering)
 
(34 intermediate revisions by 26 users not shown)
Line 1: Line 1:
[https://www.zsh.org/ 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 {{ic|emulate sh}}), it offers advantages such as improved [http://zsh.sourceforge.net/Guide/zshguide06.html tab completion] and [http://zsh.sourceforge.net/Doc/Release/Expansion.html globbing].
[https://www.zsh.org/ Zsh] is a powerful Unix [https://wiki.nixos.org/wiki/Command_Shell shell] that functions both as an interactive shell and a scripting language interpreter. It extends the Bourne Shell (sh) with features from bash, ksh, and tcsh, offering [http://zsh.sourceforge.net/Guide/zshguide06.html advanced tab completion], improved [http://zsh.sourceforge.net/Doc/Release/Expansion.html globbing], and extensive customization options. Though not POSIX sh-compatible by default, it can be configured to be so with <code>emulate sh</code>.
 
Key features include highly customizable prompts, enhanced command history, spelling correction, and robust job control. The Oh My Zsh framework simplifies managing plugins and themes. Zsh is cross-platform, available on Unix-like systems including Linux and macOS, and is popular among developers and system administrators for its advanced features and user-friendly enhancements.


The [http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4 Zsh FAQ] offers more reasons to use Zsh.
The [http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4 Zsh FAQ] offers more reasons to use Zsh.


== Installation ==
== Installation ==
See [[Command Shell]].


=== Globally ===
== Configuration ==
To set zsh as your default shell system-wide, add the following line to your config:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
==== Basic ====
users.defaultUserShell = pkgs.zsh;
<syntaxhighlight lang="nix">
</nowiki>}}
programs.zsh = {
then run {{ic|nixos-rebuild switch}} and reboot your system.
    enable = true;
=== For one user only ===
To only set zsh as a default shell for one of the users, add
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
shell = pkgs.zsh;
</nowiki>}}
to this user's section like so:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
users.users.yourname = {}
  ... # Your user configuration
  shell = pkgs.zsh;
};
};
</nowiki>}}
</syntaxhighlight>


== Configuration ==
==== Advanced ====
Zsh can be configured with [[Home Manager]].
The configuration below is using home manager, but a more limited version of it can be achieved if system-wide.<syntaxhighlight lang="nix">
programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;


Full list of home-manager options for zsh can be found [https://rycee.gitlab.io/home-manager/options.html#opt-wayland.windowManager.sway.enable here.]
    shellAliases = {
      ll = "ls -l";
      edit = "sudo -e";
      update = "sudo nixos-rebuild switch";
    };


=== Example Configuration ===
    history.size = 10000;
{{file|~/.config/nixpkgs/home.nix|nix|<nowiki>
    history.ignoreAllDups = true;
programs.zsh = {
     history.path = "$HOME/.zsh_history";
  enable = true;
     history.ignorePatterns = ["rm *" "pkill *" "cp *"];
  shellAliases = {
     ll = "ls -l";
     update = "sudo nixos-rebuild switch";
  };
  history = {
    size = 10000;
    path = "${config.xdg.dataHome}/zsh/history";
  };
};
};
</nowiki>}}
</syntaxhighlight>


=== Plugin Management ===
== Tips and Tricks ==
Home manager has three ways of managing plugins: Zplug, Oh-My-Zsh and manual.


==== Zplug ====
==== Where to see a list of options? ====
{{file|~/.config/nixpkgs/home.nix|nix|<nowiki>
The home manager options are defined in the following [https://nix-community.github.io/home-manager/options.xhtml#opt-programs.zsh.enable Home Manager Options Manual].
 
The system-wide options are listed on [https://mynixos.com/search?q=zsh MyNixOS].
 
==== How to use plugins? ====
Home manager has three ways of managing plugins: '''Zplug''', '''Oh-My-Zsh''' and '''Manual'''.<syntaxhighlight lang="nix">
programs.zsh = {
programs.zsh = {
   ... # Your zsh config
   enable = true;
 
   zplug = {
   zplug = {
     enable = true;
     enable = true;
     plugins = [
     plugins = [
       { name = "zsh-users/zsh-autosuggestions"; } # Simple plugin installation
       {name = "zsh-users/zsh-autosuggestions";} # Simple plugin installation
       { name = "romkatv/powerlevel10k"; tags = [ as:theme depth:1 ]; } # Installations with additional options. For the list of options, please refer to Zplug README.
       {
        name = "romkatv/powerlevel10k";
        tags = [as:theme depth:1];
      } # Installations with additional options. For the list of options, please refer to Zplug README.
     ];
     ];
   };
   };
};
 
</nowiki>}}
   ohMyZsh = {
==== Oh-My-Zsh ====
{{file|~/.config/nixpkgs/home.nix|nix|<nowiki>
programs.zsh = {
  ... # Your zsh config
   oh-my-zsh = {
     enable = true;
     enable = true;
     plugins = [ "git" "thefuck" ];
     plugins = ["git" "thefuck"];
     theme = "robbyrussel";
     theme = "robbyrussell";
   };
   };
};
 
</nowiki>}}
==== Manual ====
{{file|~/.config/nixpkgs/home.nix|nix|<nowiki>
programs.zsh = {
  ... # Your zsh config
   plugins = [
   plugins = [
     {
     {
      # will source zsh-autosuggestions.plugin.zsh
       name = "zsh-autocomplete";
       name = "zsh-autosuggestions";
       src = pkgs.fetchFromGitHub {
       src = pkgs.fetchFromGitHub {
         owner = "zsh-users";
         owner = "marlonrichert";
         repo = "zsh-autosuggestions";
         repo = "zsh-autocomplete";
         rev = "v0.4.0";
         rev = "23.07.13";
         sha256 = "0z6i9wjjklb4lvr7zjhbphibsyx51psv50gm07mbb0kj9058j6kc";
         sha256 = "sha256-/6V6IHwB5p0GT1u5SAiUa20LjFDSrMo731jFBq/bnpw=";
       };
       };
     }
     }
     {
     {
       name = "enhancd";
       name = "powerlevel10k";
       file = "init.sh";
      src = pkgs.zsh-powerlevel10k;
      file = "share/zsh-powerlevel10k/powerlevel10k.zsh-theme";
    }
    {
      name = "powerlevel10k-config";
      src = ./p10k-config;
       file = "p10k.zsh";
    }
    {
      name = "zsh-syntax-highlighting";
       src = pkgs.fetchFromGitHub {
       src = pkgs.fetchFromGitHub {
         owner = "b4b4r07";
         owner = "zsh-users";
         repo = "enhancd";
         repo = "zsh-syntax-highlighting";
         rev = "v2.2.1";
         rev = "0.8.0";
         sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g";
         sha256 = "sha256-iJdWopZwHpSyYl5/FQXEW7gl/SrKaYDEtTH9cGP7iPo=";
       };
       };
     }
     }
   ];
   ];
};
};
</nowiki>}}
</syntaxhighlight>
 
== Troubleshooting ==
 
==== Zsh-autocomplete not working ====
You may have some issues with the {{ic|marlonrichert/zsh-autocomplete}} plugin on NixOS. That's because the default NixOS configuration overrides keybinds for up and down arrow keys. To fix this issue, you need to add this somewhere in your .zshrc (either manually if your .zshrc is not managed by Nix, or with {{ic|packages.zsh.initExtra}})
<syntaxhighlight lang="bash">
bindkey "''${key[Up]}" up-line-or-search
</syntaxhighlight>
 
==== SHA Mismatch during manual plugin installation ====
If manual plugin installation fails with SHA mismatch, generating a valid hash as part of the error message can be achieved by temporarily switching to:
<syntaxhighlight lang="nix">
sha256 = lib.fakeSha256;
</syntaxhighlight>
 
This will print a valid SHA to the console and then can be used as final value for the sha256 field.
Redoing this is mandatory if one wants to update to a newer commit of the targeted plugin repository.
 
==== GDM does not show user when zsh is the default shell ====
GDM only shows users that have their default shell set to a shell listed in /etc/shells. Setting the default shell using the following does not update /etc/shells.
<syntaxhighlight lang="nix">
users.defaultUserShell = pkgs.zsh;
</syntaxhighlight>
To add the zsh package to /etc/shells you must update environment.shells.
<syntaxhighlight lang="nix">
environment.shells = with pkgs; [ zsh ];
</syntaxhighlight>
 
== References ==
 
# https://www.zsh.org/
# http://zsh.sourceforge.net/Guide/zshguide06.html
# http://zsh.sourceforge.net/Doc/Release/Expansion.html
# http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4
# https://nix-community.github.io/home-manager/options.xhtml#opt-programs.zsh.enable
# https://mynixos.com/search?q=zsh
 
[[Category:Shell]]
[[Category:NixOS Manual]]
48

edits