From fb3fb4365e8148c63a100148f4f7faa5b370c232 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Tue, 31 Aug 2021 10:46:58 +0100 Subject: [PATCH] ZSH: adds ^n keybinding to expand current word's filename This is useful if I want to edit a script but I'm not in my scripts directory. It will look for a file with the name I have typed and replace it. for example: vim yt<^n> will be replaced with vim /home/jonathan/.bin/yt The script will first try `which `, searching the path. If that fails, it will do `locate "*/filename"` which will search for the file elsewhere --- shells/zsh/includes/keybindings.zsh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/shells/zsh/includes/keybindings.zsh b/shells/zsh/includes/keybindings.zsh index e1d1d123..bb3e48ee 100644 --- a/shells/zsh/includes/keybindings.zsh +++ b/shells/zsh/includes/keybindings.zsh @@ -153,4 +153,26 @@ zle -N swap_command bindkey '\ec' swap_command +find_current_file(){ + tokens=(${(z)LBUFFER}) + local lastWord="${tokens[-1]}" filepath + # First assume I'm trying to edit a script. If it's in my path, use it + filepath="$(which "$lastWord")" + if [ "$?" -eq 0 ]; then + tokens[-1]="$filepath" + LBUFFER="${tokens[@]}" + return 0 + fi + # Next try locate with an exact filename match + filepath="$(locate "*/$lastWord")" + if [ "$?" -eq 0 ]; then + tokens[-1]="$filepath" + LBUFFER="${tokens[@]}" + return 0 + fi +} +zle -N find_current_file +# ctrl + n +bindkey '^n' find_current_file +