Showing posts with label zsh. Show all posts
Showing posts with label zsh. Show all posts

May 13, 2022

History in ZSH

I switched to zsh from bash many years ago. 

Today, I like to share a few tips on using zsh, and those are included in my dotfiles.


How to SKIP a cmdline from .zsh_history

$ echo "This cmdline saved in history."

$  echo "This cmdline will not be saved in history (bcos of leading space. Try it."


Show datetime and elapsed times in history

I would like to show the datetime and the elapsed time of my cmdline in my history. Thus I modified the alias in my .zshrc:

alias history='history -i -D 0'

Where -i is for datetime in iso-format, -D is for showing elapsed times spent, and 0 is for showing all the history.


To show history from all the login terminals

I do open multiple terminals for work, and I would like to see the history of cmdline from all the terminals. Thus I enable the setopt in my .zshrc:

setopt share_history

 

Feb 8, 2014

Tetris in zsh

Add the following 3 lines into your .zshrc:
autoload -U tetris 
zle -N tetris 
bindkey  "^T" tetris  # Ctrl-t to play

Jan 16, 2014

Shows Exit Code of Previous Command in zsh

To display the exit code of the previous command is very useful in zsh.
PS1='%n%m %~ %(?..[%?] )%# '

%(?..[%?] )
It's a conditional expression. The part before the 1st dot is the expression; the part between the two dots is output if it's TRUE (or 0); the part after the second dot is output if it's FALSE (or any number but 0).

See my full customization on .zshrc file.

Jan 9, 2014

Some Tricks in using zsh

These are part of the tricks that I like most about zsh:

ESC-. [ Escape period ]
Inserts the last argument of the previous history line, repeat to go back in history.

ESC-'  [ Escape single-quote ]
To quotes the whole line. (Useful for su -c or ssh).

ESC-q [ Escape q ]
To clears the line and inserts it again on the next prompt, allowing you to issue an interim command.

ESC-RETURN [ Escape ENTER ]
To inserts a literal newline, so you can edit longer commands easily.

References:

  • http://chneukirchen.org/blog/archive/2008/02/10-zsh-tricks-you-may-not-know.html
  • http://www.rayninfo.co.uk/tips/zshtips.html
  • http://grml.org/zsh/zsh-lovers.html

Jan 8, 2014

AutoCorrection in zsh

By default, zsh comes with autocorrection.

If you found that it is very annoying, you can turn it off separately. To switch it off completely:
unsetopt correct  # newer version
unsetopt correct-all # older version

To switch it off for certain command, such as 'vim', do this in your .zshrc
alias vim='nocorrect vim' 

To switch it off for another copy of zsh:
NOCOR=1 zsh

Jan 6, 2014

Customize zsh for PiBang

By default, PiBang comes with zsh as the default shell.

After few days using zsh, I found that I have key binding issue. This reminds me that I need to setup key sequence and binding manually for certain keys: HOME, END, and DELETE.

So refer to setup key sequence and bindings manually.

Sep 10, 2013

Setup KeySequences and Bindings Manually

Recently, I switch from bash to zsh, and face some keys binding issue. The [Home] and [End] keys are not working like bash.

Then I learn a manual way to setup the correct key-sequences and binding them manually that will always work. For example, here's how I test the 3 key-sequences, [Home] and [End]:
$ cat > /dev/null
^[[1~
^[[4~
^C
$ 
Then, I setup the key bindings in my $HOME/.zshrc file:
bindkey '^[[1~' beginning-of-line      # Home
bindkey '^[[4~' end-of-line            # End
bindkey '^[[2~' beep                   # Insert
bindkey '^[[3~' delete-char            # Del
bindkey '^[[5~' vi-backward-blank-word # Page Up
bindkey '^[[6~' vi-forward-blank-word  # Page Down