How to rerun last command in Zsh shell


If you want to execute the last command that you ran in your Zsh shell, the easiest way would be by pressing the up arrow key on your keyboard, the last command is pulled up your on your prompt and you press enter. But do you know there is another way in which you can run the previous command using a command? Simply type to exclamation marks !! - this will execute the last executed command on your Zsh shell. Let's see some examples,

Zsh command to execute previous command
Zsh command to execute previous command

As you can see, when I typed !! the pervious command that was clear was executed again. In the next example it was the who command. Also notice that when you run the double exclamation command it displays the command as text in the first line outputted and then the result of the command,

code2care@mac ~ % !!
ls -ltrh
total 0
drwxr-xr-x+  4 code2care  staff   128B Jan  2 23:03 Public
drwx------+  4 code2care  staff   128B Jan  2 23:28 Pictures
drwxr-xr-x   4 code2care  staff   128B Jan 17 16:16 ClassRoom
drwx------@ 75 code2care  staff   2.3K Feb 13 16:04 Library
drwx------@  3 code2care  staff    96B Feb 13 16:04 Applications
drwxr-xr-x   5 code2care  staff   160B Feb 15 22:32 IdeaProjects
drwx------@ 13 code2care  staff   416B Mar  6 18:01 Documents
drwx------+  7 code2care  staff   224B Mar 13 22:42 Music
drwx------   6 code2care  staff   192B Mar 20 16:54 Movies
drwx------+ 18 code2care  staff   576B Apr  3 12:41 Downloads
drwx------@  8 code2care  staff   256B Apr  3 13:27 Desktop
code2care@mac ~ % 





Hello there!,

You are using AdBlocker!

I am an independent developer trying to help fellow developers and students to resolve issues faced on a day-to-day basis, we 'Code to Care' . There is a lot of expenses involved in hosting and managing a website that's the reason we have ads.

A humble request to you to disable adBlocker on Code2care.org and support us pay our bills.

Thanks,
Cod2care - Line's of code for change.

To see contents reload page after unblocking
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities. Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, Tips-n-tricks, How Tos, Blogs, Articles, etc. on various platforms like Windows, Mac, Linux, Mobile platforms, etc. Technologies/Languages like Java, Objective-C, PHP, .Net, Android, SharePoint, jQuery, HTML, CSS, etc.