I often find myself using mv to rename a file. E.g.
mv app/models/keywords_builder.rb app/models/keywords_generator.rb
Doing so I need to write (ok, tab complete) the path for the second parameter. In this example it isn't too bad but sometimes the path is deeply nested and it seems like quite a bit of extra typing.
Is there a more efficient way to do this?
Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name. Attention: The mv command can overwrite many existing files unless you specify the -i flag.
If you specify a single file as SOURCE , and a single file as DESTINATION target then you're renaming the file . When the SOURCE is a directory and DESTINATION doesn't exist, SOURCE will be renamed to DESTINATION . Otherwise if DESTINATION exist, it be moved inside the DESTINATION directory.
This bash command moves files and folders. mv source target mv source ... directory. The first argument is the file you want to move, and the second is the location to move it to.
You can use history expansion like this:
mv app/modules/keywords_builder.rb !#^:h/keywords_generator.rb
!
introduces history expansion.#
refers to the command currently being typed^
means the first argument:h
is a modifier to get the "head", i.e. the directory without the file partIt's supported in bash
and zsh
.
Docs:
One way is to type the first file name and a space, then press Ctrl
+w
to delete it. Then press Ctrl
+y
twice to get two copies of the file name. Then edit the second copy.
For example,
mv app/models/keywords_builder.rb <Ctrl+W><Ctrl+Y><Ctrl+Y><edit as needed>
or cd apps/models && mv keywords_builder.rb keywords_generator.rb && cd -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With