www-gem words

2 keybindings to save 80% of your typing time

Published on

zsh Over the years I realized that CLI tools and the terminal offered me more customization and were more efficient to complete most of my tasks even when compared to the counterpart GUI of the same applications. For this reason, I’ve now reached the point where using the commandline to manipulate files and folders is faster for me with the terminal rather than using a (GUI or CLI) file explorer. When I started moving that way I had to face one major limitation though: re-typing the argument used in a previous or in the current command was far from efficient and wasted so much time in the process.

Hopefully, the 2 keybindings below are the solution. Just add these lines to your .zshrc:

Note that these keybindings are valid when using the vi mode in zsh (with bindkey -v)

╭── Paste the last command argument

bindkey -M vicmd '^[P' insert-last-word
bindkey -M viins '^[P' insert-last-word

In visual and insert mode, using Alt+P will paste the last command argument in your current command. If you need to use an argument we used in 3 commands earlier, just type Alt+P three times.

Example:

# cd into a directory

% cd folder1/subfolder1/subfolder2/subfolder3/

# copying a file from this directory to a new one

% cp filename folder2/subfolder1/subfolder2/

# copying a file from directory2 to directory1

% cp Alt+P filename Alt+P Alt+P

Here you don’t have to retype folder2/subfolder1/subfolder2/subfolder3 as first argument nor folder1/subfolder1/subfolder2/ for the second argument. What a time saving (and you’re sure the path are correct as well)!

╭── Paste the current command argument

zle -N copy-earlier-word
bindkey -M vicmd '^[p' copy-earlier-word
bindkey -M viins '^[p' copy-earlier-word

In visual and insert mode, using Alt+p will paste the current command argument again.

Example:

# renaming a file

% mv folder/subfolder1/subfolder2/subfolder3/very_long_filename Alt+p new_name

Typing Alt+p save you from retyping the entire folder/subfolder1/subfolder2/subfolder3/very_long_filename


Thanks for your read. Hope it's been useful to you.


Interact with this post using Mastodon or

Comment on wwwgem's post

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈

More food for thoughts? Check other posts about: #Zsh