Sample Header Ad - 728x90

How to configure ZSH to behave more like BASH

5 votes
1 answer
119 views
I recently upgraded KDE Plasma to v6, got ZSH instead of BASH in it and because I like some of its smart completion features and such, I decided to try to learn working with it. But I have a hard time getting used to it as a decades long BASH user. Maybe I'm searching wrong but I can't figure out how to configure some basic stuff that I find convenient in BASH. I'm having the following troubles: 1. ` (ALT+period) in BASH invokes the last word from the previous command. E.g., I do ls file.txt , then cat and I get cat file.txt`. The caret (cursor) is after the word inserted by the key combo. This doesn't work for me in ZSH. I can do !$ instead of ` but that's not too convenient. I also have working solution written by GPT but it throws me an error even tho it works. See my .zshrc` below. I have also found this question—[vi mode - How to use Alt + . in zsh with Vim bindings - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/685008/how-to-use-alt-in-zsh-with-vim-bindings)—where I learned I can use +_ but that's not exactly comfortable either for two reasons: Firstly, underscore is bound to +- on US and CZ keyboards which I use (so it's actually uncomfortable ++- combo which has the potential to change keyboard layout with +), and secondly, it produces the same error "No such shell function insert-last-word" as the binding I already have. I tried append-last-word instead which doesn't work at all. 1. ` and ` to jump to the start/end of the current line. I can do / to do the start/end jumps but that's not exactly convenient either. 1. I have there working binding to use +LEFT/WRITE_ARROW to jump by words. However, this doesn't seem to respect $WORDCHARS, e.g., if there's a path (includes /), it jumps over it as a whole. What may I be doing wrong? My $WORDCHARS:
$ echo $WORDCHARS 
*?_-.[]~=/&;!#$%^(){}
4. Edit: one more thing: I just installed a package (sudo pacman -S hdparm successfully. And right after that, when I try to invoke the command, I type hdpar, expecting to get the hdparm auto-completed. Nope, I get various completely random suggestions (cdparanoia gdparttopng), none of them being hdparm. When I type the command in full, though, it works, so that means it _is_ a valid command. Do I need to run some completion update? :o (sorry, confused and slightly disillusioned). I found that bindkey -e # Use emacs key bindings brings some things to life, such as `` for reverse search. But not all of them, see my biggest pains above. Here's the config I have in ~/.zshrc: (I'm posting it in its entirety but everything below line 52 is out of this question's scope, it works fine and is unrelated.) Please see the comments in the file for explanations of what doesn't work and how
# The following lines were added by compinstall

zstyle ':completion:*' completer _expand _complete _ignored _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' 'r:|[._-]=** r:|=** l:|=*'
zstyle ':completion:*' menu select=2
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl true
zstyle :compinstall filename '/home/edison23/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall
# Lines configured by zsh-newuser-install
HISTFILE=~/.bash_history
HISTSIZE=1000000
SAVEHIST=1000000
unsetopt autocd
bindkey -v
# End of lines configured by zsh-newuser-install

#PS1='%d $ '

autoload -U colors && colors
PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}$ "

# Set key bindings to emulate Bash behavior
bindkey -e  # Use emacs key bindings

# Should bind  to inserting last word but it works only for the last word of *the same command*,
# e.g. cat foo; echo bar; cat  gives me cat foo instead of cat bar. Not desired.
# bindkey '^[.' history-beginning-search-backward

# This works but throws "No such shell function `insert-last-word'" error at the same time which is annoying, to say the least.
# Define the widget
zle -N insert-last-word
# Bind the widget to Alt + .
bindkey '^[.' insert-last-word

# Move to the beginning and end of the line using Home and End keys
# Doesn't work at all
bindkey 'Home' beginning-of-line  # Home key to go to the start of the line
bindkey 'End' end-of-line          # End key to go to the end of the line

# Move by words using  (works)
# TODO: Any option to customize word delimiters?
bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word

# Everything below is fine and of no relation to the question posted on SO
# ls aliases
alias ll='ls -la -v --time-style=posix-long-iso --group-directories-first --color=always'
alias la='ls -A'
alias l='ls -CF'
#ls hidden files, no sorting
alias ls='ls -v --color="auto" --time-style=posix-long-iso --group-directories-first'


# CRC32 into filename
# rhash required, I think
alias embedcrc32='rhash -e --embed-crc-delimiter="."'

# Copy node path
# Use | xsel -bi on X11
fpath() {
	echo -n readlink -f "${1}" | wl-copy
}

# color diff
alias dwdiff_rw='dwdiff -P -cred,green -A best -w [- -x -] -y {+ -z +}'
Thank you very much for your time, help, and patience with a ZSH newbie. Any bonus useful tips and tricks appreciated as well :)
Asked by edison23 (155 rep)
Jul 13, 2025, 01:10 PM
Last activity: Jul 31, 2025, 08:14 PM