You can use the following
git clone http://repo_url.git && cd "!$:t:r"
Imp: Do not forget the double quotes in the cd
command, else it won't work in some other shells or Git Bash in Windows.
How does it work?
The first command is the obvious git clone
command. The second cd
command is intriguing.
Now there is something called Word Designators for command history
!$
is the last part of the last command run
Here the last command run would be git clone http://repo_url.git
. This command consists of three parts. 1. git
, 2. clone
and 3. http://repo_url.git
. And http://repo_url.git
is the last part. Hence !$
==> http://repo_url.git
Then there is something called Word Modifiers, which modify the string preceding it.
:t
removes all leading file name components, leaving the tail
So here !$:t
would be read like (!$):t
. Hence !$:t
==> repo_url.git
:r
removes the trailing suffix from filenames like abcd.xyz
, leaving abcd
So here !$:t:r
would be read like {(!$):t}:r
. Hence !$:t:r
==> repo_url
So it would cd
to repo_url
To debug this yourself, use :p
which just prints the command preceding it without executing it. Equivalent would be echo
Run the following in the exact sequence
git clone http://repo_url.git
!$:p
==> http://repo_url.git
(or echo !$
)
!$:t:p
==> repo_url.git
(or echo !$:t
)
!$:t:r:p
==> repo_url
(or echo !$:t:r
)
.bash_profile
arbitrarily namedgitclone
- in a similar way to this answer. For example: 1) Run the following command to append thegitclone
function to your.bash_profile
file:echo $'\n'"function gitclone() { git clone \"\$1\" && cd \"\$(basename \"\$_\" .git)\" || return; }" >> ~/.bash_profile
. 2) Create a new session and run the simplified command in the future:gitclone "http://repo_url.git"
– Despot