-
-
Save premek/6e70446cfc913d3c929d7cdbfe896fef to your computer and use it in GitHub Desktop.
# Put this function to your .bashrc file. | |
# Usage: mv oldfilename | |
# If you call mv without the second parameter it will prompt you to edit the filename on command line. | |
# Original mv is called when it's called with more than one argument. | |
# It's useful when you want to change just a few letters in a long name. | |
# | |
# Also see: | |
# - imv from renameutils | |
# - Ctrl-W Ctrl-Y Ctrl-Y (cut last word, paste, paste) | |
function mv() { | |
if [ "$#" -ne 1 ] || [ ! -e "$1" ]; then | |
command mv "$@" | |
return | |
fi | |
read -ei "$1" newfilename | |
command mv -v -- "$1" "$newfilename" | |
} |
@rohan-mo
-bash: emacs: command not found
Meaning you don't have emacs installed or the bin is not in your $PATH
Easiest way is to apt install emacs or brew install emacs
Other way will be to do something like that:
export EDITOR=vim
if you have vim or replace vim with what you have installed
Firstly, thank you - this is nice™.
zsh equivalent:
function mv() {
if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
command mv "$@"
return
fi
newfilename="$1"
vared newfilename
command mv -v -- "$1" "$newfilename"
}
Kudos to Gilles 'SO- stop being evil' for StackExchange answer.
BTW, fish also has vared, so this should probably work for fish, too, and be lighter-weight than Humandoodlebug's excellent bash subshell solution.
@rileytg
man readline
(sorry for the terse answer but that's the library bash uses and where you will find, in long form, what options are available)
thank you! i didnt realize readline was so foundational to a lot of what i use every day
I like it, it's especially useful if you have spaces in the file (or directory) name (btw the bash cut last word would not work then). Probably I would also flip the conditional and add directory checking:
if [ "$#" -eq 1 ] && ( [ -f "$1" ] || [ -d "$1" ] ); then
read -ei "$1" newfilename; command mv -v "${1}" "${newfilename}"; return $?
fi
command mv "$@"; return $?
You can also do Ctrl-W followed by Ctrl-Y after typing the path, if you are using readline style keybindings. Mainstream shells come with readline style bindings by default.
Ctrl-W deletes the previous word for me
In zsh you need compdef mv=ls
, it seems, or the completion breaks.
@premek your snippet is great, short, concise and to the point. Thanks for sharing it.
There are even more flexible ways (in bash, at least), for example (here renaming .txt to .json):
$ touch config.txt $ mv config.txt !#:1:s/txt/json
This one eludes me, care to explain how it works?
I would recommend adding --interactive to the mv commands in the script. As it is now, it is possible to overwrite existing files with no confirmation using this script.
With the
fish
shell, you can typeAlt-E
to edit any command line in your favorite editor where you likely already know shortcuts for copying, pasting and modifying. The great thing is that is a general solution that works for any command line edits.
In bash it is C-x C-e
You think like this?
Anway, be aware that «history of arguments» are found by <ALT><.> (<ALT><Punctation>) so this:
~ $ touch zoo
~ $ mv <ALT><.><SPACE><ALT><.>
... will type out:
~ $ mv zoo zoo
... and by the way, continue <ALT><.> repeated times to get older «arguments»