From 95eeb86221ce6741286b1b15b7e1ce353b04c9ad Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Fri, 22 Jul 2011 05:27:01 +0200 Subject: [PATCH] zshrc: provide help_zle_parse_keybindings through ctrl-x-z, add/change some keybinding comments accordingly. --- doc/grmlzshrc.t2t | 3 ++ etc/zsh/zshrc | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 126 insertions(+), 7 deletions(-) diff --git a/doc/grmlzshrc.t2t b/doc/grmlzshrc.t2t index 70573ca..09f4b7f 100644 --- a/doc/grmlzshrc.t2t +++ b/doc/grmlzshrc.t2t @@ -433,6 +433,9 @@ directory does not exist yet -> press **CTRL-xM** and problem solved. : **CTRL-x-p** Searches the last occurence of string before the cursor in the command history. +: **CTRL-x-z** +Display help on keybindings and zsh line editor. Press consecutively to page through content. + : **CTRL-z** Brings a job, which got suspended with CTRL-z back to foreground. diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 7c92810..3c472d6 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -605,7 +605,7 @@ bindkey '\e[B' down-line-or-search # - ## alt-backspace is already the default for backwards-delete-word ## let's also set alt-delete for deleting current word (right of cursor) -#k# Delete current word +#k# Kill right-side word bindkey "3~" delete-word ## use Ctrl-left-arrow and Ctrl-right-arrow for jumping to word-beginnings on the CL @@ -621,9 +621,13 @@ bindkey '^[[1;3D' backward-word # line up to the cursor and move the cursor to the end of the line then zle -N history-beginning-search-backward-end history-search-end zle -N history-beginning-search-forward-end history-search-end +#k# search history backward for entry beginning with typed text bindkey '^xp' history-beginning-search-backward-end +#k# search history forward for entry beginning with typed text bindkey '^xP' history-beginning-search-forward-end +#k# search history backward for entry beginning with typed text bindkey "\e[5~" history-beginning-search-backward-end # PageUp +#k# search history forward for entry beginning with typed text bindkey "\e[6~" history-beginning-search-forward-end # PageDown # bindkey -s '^L' "|less\n" # ctrl-L pipes to less @@ -677,11 +681,11 @@ slash-backward-kill-word() { } zle -N slash-backward-kill-word -#k# Kill everything in a word up to its last \kbd{/} +#k# Kill left-side word or everything up to next slash bindkey '\ev' slash-backward-kill-word -#k# Kill everything in a word up to its last \kbd{/} +#k# Kill left-side word or everything up to next slash bindkey '\e^h' slash-backward-kill-word -#k# Kill everything in a word up to its last \kbd{/} +#k# Kill left-side word or everything up to next slash bindkey '\e^?' slash-backward-kill-word # use the new *-pattern-* widgets for incremental history search @@ -927,7 +931,7 @@ if is4 ; then fi if is4 && zrcautoload insert-files && zle -N insert-files ; then - #k# Insert files + #k# Insert files and test globbing bindkey "^Xf" insert-files # C-x-f fi @@ -992,7 +996,7 @@ sudo-command-line() { } zle -N sudo-command-line -#k# Put the current command line into a \kbd{sudo} call +#k# prepend the current command with "sudo" bindkey "^Os" sudo-command-line ### jump behind the first word on the cmdline. @@ -1008,7 +1012,7 @@ function jump_after_first_word() { fi } zle -N jump_after_first_word - +#k# jump to after first word (for adding options) bindkey '^x1' jump_after_first_word # complete word from history with menu (from Book: ZSH, OpenSource-Press) @@ -2120,6 +2124,117 @@ H-Glob() { } alias help-zshglob=H-Glob +#v1# set number of lines to display per page +HELP_LINES_PER_PAGE=20 +#f1# helper function for help-zle, actually generates the help text +help_zle_parse_keybindings() +{ + emulate -L zsh + setopt extendedglob + unsetopt ksharrays #indexing starts at 1 + + #v1# choose files that help-zle will parse for keybindings + ((${+HELPZLE_KEYBINDING_FILES})) || HELPZLE_KEYBINDING_FILES=( /etc/zsh/zshrc ~/.zshrc.pre ~/.zshrc ~/.zshrc.local ) + + #fill with default keybindings, possibly to be overwriten in a file later + #Note that due to zsh inconsistency on escaping assoc array keys, we encase the key in '' which we will remove later + local -A help_zle_keybindings + help_zle_keybindings['@']="set MARK" + help_zle_keybindings['XJ']="vi-join lines" + help_zle_keybindings['XB']="jump to matching brace" + help_zle_keybindings['XU']="undo" + help_zle_keybindings['_']="undo" + help_zle_keybindings['XF']="find in cmdline" + help_zle_keybindings['A']="goto beginning of line" + help_zle_keybindings['E']="goto end of line" + help_zle_keybindings['t']="transpose charaters" + help_zle_keybindings['T']="transpose words" + help_zle_keybindings['s']="spellcheck word" + help_zle_keybindings['K']="backward kill buffer" + help_zle_keybindings['U']="forward kill buffer" + help_zle_keybindings['y']="insert previously killed word/string" + help_zle_keybindings["'"]="quote line" + help_zle_keybindings['"']="quote from mark to cursor" + help_zle_keybindings['']="repeat next cmd/char times (-10a -> -10 times 'a')" + help_zle_keybindings['U']="make next word Uppercase" + help_zle_keybindings['l']="make next word lowercase" + help_zle_keybindings['Xd']="preview expansion under cursor" + help_zle_keybindings['q']="push current CL into background, freeing it. Restore on next CL" + help_zle_keybindings['.']="insert (and interate through) last word from prev CLs" + help_zle_keybindings[',']="complete word from newer history (consecutive hits)" + help_zle_keybindings['m']="repeat last typed word on current CL" + help_zle_keybindings['V']="insert next keypress symbol literally (e.g. for bindkey)" + help_zle_keybindings['!!:n*']="insert last n arguments of last command" + help_zle_keybindings['!!:n-']="insert arguments n..N-2 of last command (e.g. mv s s d)" + help_zle_keybindings['H']="run help on current command" + + #init global variables + unset help_zle_lines help_zle_sln + typeset -g -a help_zle_lines + typeset -g help_zle_sln=1 + + local k v + local lastkeybind_desc contents #last description starting with #k# that we found + local num_lines_elapsed=0 #number of lines between last description and keybinding + #search config files in the order they a called (and thus the order in which they overwrite keybindings) + for f in $HELPZLE_KEYBINDING_FILES; do + [[ -r "$f" ]] || continue #not readable ? skip it + contents="$(<$f)" + for cline in "${(f)contents}"; do + #zsh pattern: matches lines like: #k# .............. + if [[ "$cline" == (#s)[[:space:]]#\#k\#[[:space:]]##(#b)(*)[[:space:]]#(#e) ]]; then + lastkeybind_desc="$match[*]" + num_lines_elapsed=0 + #zsh pattern: matches lines that set a keybinding using bindkey or compdef -k + # ignores lines that are commentend out + # grabs first in '' or "" enclosed string with length between 1 and 6 characters + elif [[ "$cline" == [^#]#(bindkey|compdef -k)[[:space:]](*)(#b)(\"((?)(#c1,6))\"|\'((?)(#c1,6))\')(#B)(*) ]]; then + #description prevously found ? description not more than 2 lines away ? keybinding not empty ? + if [[ -n $lastkeybind_desc && $num_lines_elapsed -lt 2 && -n $match[1] ]]; then + #substitute keybinding string with something readable + k=${${${${${${${match[1]/\\e\^h/}/\\e\^\?/}/\\e\[5~/}/\\e\[6~/}//(\\e|\^\[)/}//\^/}/3~/} + #put keybinding in assoc array, possibly overwriting defaults or stuff found in earlier files + #Note that we are extracting the keybinding-string including the quotes (see Note at beginning) + help_zle_keybindings[${k}]=$lastkeybind_desc + fi + lastkeybind_desc="" + else + ((num_lines_elapsed++)) + fi + done + done + unset contents + #calculate length of keybinding column + local kstrlen=0 + for k (${(k)help_zle_keybindings[@]}) ((kstrlen < ${#k})) && kstrlen=${#k} + #convert the assoc array into preformated lines, which we are able to sort + for k v in ${(kv)help_zle_keybindings[@]}; do + #pad keybinding-string to kstrlen chars and remove outermost characters (i.e. the quotes) + help_zle_lines+=("${(r:kstrlen:)k[2,-2]}${v}") + done + #sort lines alphabetically + help_zle_lines=("${(i)help_zle_lines[@]}") +} +typeset -g help_zle_sln +typeset -g -a help_zle_lines + +#f1# Provides (partially autogenerated) help on keybindings and the zsh line editor +help-zle() +{ + emulate -L zsh + unsetopt ksharrays #indexing starts at 1 + #help lines already generated ? no ? then do it + [[ ${+functions[help_zle_parse_keybindings]} -eq 1 ]] && {help_zle_parse_keybindings && unfunction help_zle_parse_keybindings} + #already displayed all lines ? go back to the start + [[ $help_zle_sln -gt ${#help_zle_lines} ]] && help_zle_sln=1 + local sln=$help_zle_sln + #note that help_zle_sln is a global var, meaning we remember the last page we viewed + help_zle_sln=$((help_zle_sln + HELP_LINES_PER_PAGE)) + zle -M "${(F)help_zle_lines[sln,help_zle_sln-1]}" +} +#k# display help for keybindings and ZLE (cycle pages with consecutive use) +zle -N help-zle && bindkey '^Xz' help-zle + check_com -c qma && alias ?='qma zshall' # grep for running process, like: 'any vim' @@ -2722,6 +2837,7 @@ inplaceMkDirs() { zle end-of-line fi } +#k# mkdir -p from string under cursor or marked area zle -N inplaceMkDirs && bindkey '^XM' inplaceMkDirs #f5# Memory overview -- 2.1.4