Viewing Session History in the zsh Shell
The Z-shell also lets you easily reuse commands from your session history, which is the list of commands you've used during a session or in previous sessions (Code Listing 3.11). The history functions are handy for reviewing your Unix session, reusing previous commands (instead of retyping), and modifying (rather than completely redoing) long or complex commands.
Code Listing 3.11. In this example, we typed the first command, and then pressed the to reuse the previous command. !40 recycled the 40th command from the listing.
[ejr@hobbes clean]$ ls background.htm info.htm logo.gif [ejr@hobbes clean]$ ls background.htm info.htm logo.gif [ejr@hobbes clean]$ history 1 free 2 id deb 3 id ejr 4 uname -a 5 ls ... 40 cd 41 cp .bash_history oldhistory 42 vi .bash_history 43 elm 44 ls -la 45 ls -la .e* 46 elm 47 lynx 48 history 49 vi .bash*his* 50 history 51 cd clean 52 ls 53 ls 54 history [ejr@hobbes clean]$ !40 cd [ejr@hobbes ejr]$
To view session history in the zsh Shell:
Use zsh as you usually would, changing directories, redirecting output, or doing other tasks. For example, review the previous chapter and practice the commands you've learned so far.
Press one time.
Note that the last (previous) command you used appears on the command line, as shown in Code Listing 3.11. To reissue the command, just press .
Continue to press or to scroll back or forward through your history. When you reach a command you want to use, press .
If you see a command that's close, but not exactly what you want to use, you can edit it. Just use and to move across the line. Then, insert text by typing it in or using or to delete text. When you've modified the command, press (you don't have to be at the end of the line to do so).
Type history at the shell prompt to see a numbered list of previous commands you've entered.
If you have just a minor change to a command, you can edit it quickly and easily. For example, if you just used ls /home/ users/e/eric and wanted to issue cd /home/users/e/eric next, you could just type ^cd^ls to tell the system to replace cd from the previous command with ls and then reissue the command.
You can use and while editing a command line to move to the beginning and end of the line, respectively.
Commands from the current session are kept in memory to scroll through, while commands from previous sessions are kept in the ~/.zsh_history file. You can edit zsh_history with any editor to delete unneeded commands or simply delete the file to get rid of the whole history file, which will then be re-created with the next command you issue.
Reviewing session history is a great way to identify your work patterns and needs. If you find yourself repeatedly using the same series of commands, consider writing a script to do the commands automatically, as Chapter 10 describes.
Most of the command completion options from bash also work in zsh. Give it a try!