zsh-dwim
attempts to predict what you will want to do next. It provides a key binding (control-u) that will replace the current (or previous) command line with the command you will want to run next.
apt-cache show zsh
becomessudo apt-get install zsh
tar ft RandomFile.tar.bz2
becomestar fx RandomFile.tar.bz2
sudo service apache stop
becomessudo service apache start
mkdir new_directory
becomescd new_directory
find . -size +5M
becomesfind . -size +5M -exec echo {} \;
becomesfind . -size +5M -exec {} \;
- If
ssh user@hostwithnewip
fails it becomesssh-keygen -R hostwithnewip
- If
mkdir more/than/one/path
fails it becomesmkdir -p more/than/one/path
- If
cd /some/path
failes it becomesmkdir /some/path
There are some short screencasts on my blog demonstrating zsh-dwim
:
- Demonstration of a few of zsh-dwim's oldest features
- Demonstration of zsh-dwim's remote filename completion
Since I am using Prezto to manage my zsh
configuration this repository is set up to work with it. zsh-dwim
is a simple enough script, though, so it should be easily loaded into any zsh
configuration.
Installing under Prezto
cd .zprezto
git submodule add https://github.com/oknowton/zsh-dwim.git modules/dwim
Add dwim
to your .zpreztorc
file:
# Set the Prezto modules to load (browse modules).
# The order matters.
zstyle ':prezto:load' pmodule \
'environment' \
'terminal' \
'editor' \
'history' \
'directory' \
'spectrum' \
'utility' \
'completion' \
'prompt' \
'dwim'
You should be able to install the zsh-dwim
script under oh-my-zsh
with the following command:
wget https://raw.github.com/oknowton/zsh-dwim/master/init.zsh -O $HOME/.oh-my-zsh/custom/zsh-dwim.zsh
It should start working the next time you open a new shell.
Installing using Antigen
If you use Antigen, I am told that adding the following line to your .zshrc
will work:
antigen-bundle oknowton/zsh-dwim
Anyone running zsh
should only need to source
the init.zsh
file (run source init.zsh
). If you want to permentantly "install" zsh-dwim
just add the proper source
command to your .zshrc
.
Type a command and hit control-u
and zsh-dwim
will attempt to transform your command. Typing control-u
at an empty command prompt will recall the previous command from your history and then attempt to transform it.
You can add your own transformations by calling the _dwim_add_transform
function. This function takes up to three parameters, the third being optional.
_dwim_add_transform <pattern to match> <code to run> [exit status of previous command]
Here's a simple example:
## tar ft -> tar fx (a.k.a. list -> extract)
_dwim_add_transform '^tar (ft|tf)' \
'_dwim_sed "s/^tar (ft|tf)/tar fx/"'
The _dwim_sed
function is a little helped function that applies a sed
regex to the current command.
The MIT License (MIT)
Copyright (c) 2013 Pat Regan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.