📄 ch12.htm
字号:
<HR>
</DL>
<P>The <TT>history</TT> command using the <TT>-c</TT> option clears the current history
list.</P>
<P>In addition to the <TT>history</TT> command and its options, <TT>tcsh</TT> also
contains many history navigation and editing commands. The following commands are
used to navigate through the history list:
<UL>
<LI><TT>!</TT>n re-executes the command with the history number of n.
<P>
<LI><TT>!-</TT>n re-executes the command that is n commands from the end of the history
list.
<P>
<LI><TT>!!</TT> re-executes the last command that was entered.
<P>
<LI><TT>!c</TT> re-executes the last command in the history list that begins with
the letter c.
<P>
<LI><TT>!?c?</TT> re-executes the last command in the history list that contains
the letter c.
</UL>
<P>The history editing commands enable you to replace words and letters in previously
entered commands as well as add words to the end of previously entered commands.
More information on these editing commands can be found by referring to the <TT>tcsh</TT>
man page. You can view this man page by entering the following command at the shell
prompt:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">man tcsh
</FONT></PRE>
<H3 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Aliases</FONT></H3>
<P>Command aliases are commands that you can specify and execute. Alias commands
are usually abbreviations of other Linux commands. You tell <TT>tcsh</TT> to execute
a Linux command whenever it encounters the alias. For example, if you entered the
following alias command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">alias ls `ls -F'
</FONT></PRE>
<P>the <TT>ls -F</TT> command would be substituted for the <TT>ls</TT> command each
time the <TT>ls</TT> command was used.</P>
<P>If you decide after you enter an alias that you don't need or want that alias
to exist any longer, you can use the <TT>tcsh unalias</TT> command to delete that
alias:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">unalias cd
</FONT></PRE>
<P>After you use the <TT>unalias</TT> command to remove an alias, the alias will
no longer exist, and trying to execute that alias will cause <TT>tcsh</TT> to return
a <TT>command not found</TT> error message.</P>
<P>Some aliases that you might want to define are
<UL>
<LI><TT>alias ll `ls -l'</TT>
<P>
<LI><TT>alias ls `ls -F'</TT>
</UL>
<P>If you are a DOS user and are accustomed to using DOS file commands, you might
also want to define the following aliases:
<UL>
<LI><TT>alias dir `ls'</TT>
<P>
<LI><TT>alias copy `cp'</TT>
<P>
<LI><TT>alias rename `mv'</TT>
<P>
<LI><TT>alias md `mkdir'</TT>
<P>
<LI><TT>alias rd `rmdir'</TT>
</UL>
<DL>
<DT><TT></TT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading10<FONT COLOR="#000077"><B>NOTE: </B></FONT>When you define
aliases, quotation marks are necessary only if the command within them contains spaces
or other special characters.
<HR>
</DL>
<P>If you enter the <TT>alias</TT> command without any arguments, it will print to
the screen all of the aliases that are already defined. The following listing illustrates
sample output from the <TT>alias</TT> command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">alias ls `ls -F'
alias dir `ls'
alias ll `ls -l'
alias md `mkdir'
alias rd `rmdir'
</FONT></PRE>
<H3 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">Input and Output
Redirection</FONT></H3>
<P>The standard input and output of a command can be redirected using the same syntax
that is used by <TT>bash</TT> and <TT>pdksh</TT>. The <TT><</TT> character is
used for input redirection, and the <TT>></TT> character is used for output redirection.
The following command redirects the standard input of the <TT>cat</TT> command to
the <TT>.cshrc</TT> file:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cat < .cshrc
</FONT></PRE>
<P>In practice, input redirection isn't used very often because most commands that
require input from a file support passing the filename as an argument to the command.</P>
<P>Output redirection is used much more frequently. The following command redirects
the standard output of the <TT>cat</TT> command to the file named <TT>cshenv</TT>
(which has the effect of storing the contents of the <TT>.cshrc</TT> and <TT>.login</TT>
files in one file named <TT>cshenv</TT>):<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cat .cshrc .login > cshenv
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading12<FONT COLOR="#000077"><B>CAUTION: </B></FONT>The file to
which output is being redirected is created if it does not exist and is overwritten
without warning if it already exists.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading13<FONT COLOR="#000077">Pipelines</FONT></H3>
<P><TT>tcsh</TT> pipelines, just like <TT>bash</TT> and <TT>pdksh</TT> pipelines,
are a way to string together a series of Linux commands. This means that the output
from the first command in the pipeline is used as the input to the second command
in the pipeline. The output from the second command in the pipeline is used as input
to the third command in the pipeline, and so on. The output from the last command
in the pipeline is the output that the user will actually see. This output will be
displayed to the screen (or put into a file if output redirection was specified on
the command line).</P>
<P>You can tell <TT>tcsh</TT> to create a pipeline by typing two or more commands
separated by the <TT>|</TT> character. The following command illustrates an example
of using a <TT>tcsh</TT> pipeline:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">cat file1 file2 | wc -l
</FONT></PRE>
<P>The <TT>cat</TT> command in this pipeline appends <TT>file2</TT> to the end of
<TT>file1</TT> and passes the resulting file to the <TT>wc</TT> command. The <TT>wc</TT>
command prints to the screen the total number of lines contained in the resulting
file.
<H3 ALIGN="CENTER"><A NAME="Heading14<FONT COLOR="#000077">Prompts</FONT></H3>
<P><TT>tcsh</TT> has three levels of user prompt. The first-level prompt is what
you see when <TT>tcsh</TT> is waiting for you to type a command. The default prompt
is the <TT>%</TT> character. This prompt can be customized by assigning a new value
to the prompt <TT>tcsh</TT> variable:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">set prompt="%t$"
</FONT></PRE>
<P>This example would change the first-level prompt to the current time followed
by a dollar sign.</P>
<P>The second-level prompt is displayed when <TT>tcsh</TT> is waiting for input when
in a <TT>while</TT> or <TT>for</TT> loop (used in shell programming, discussed in
Chapter 13, "Shell Programming"). The default for the second-level prompt
is <TT>%R?</TT>, where <TT>%R</TT> is a special character sequence that displays
the status of the parser. You can change the second-level prompt by setting the value
of the <TT>prompt2 tcsh</TT> variable. For example,<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">set prompt2="?"
</FONT></PRE>
<P>changes the second-level prompt to a question mark.</P>
<P>The third-level prompt is used when <TT>tcsh</TT> displays the corrected command
line when automatic spelling correction is in effect. This prompt is set using the
<TT>prompt3</TT> variable, and it has a default value of <TT>CORRECT>%R (y|n|e)?</TT>.
See the "Correcting Spelling Errors" section for more information on this
feature.</P>
<P><TT>tcsh</TT> supports special character codes in its prompt variables. These
codes are similar to the codes that <TT>bash</TT> supports in its prompts. The main
difference between the two is that the syntax for using them is different. Table
12.1 lists the most commonly used special character codes. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 12.1. tcsh prompt special character codes. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Character Code </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%/</TT> </TD>
<TD ALIGN="LEFT">Displays the current working directory. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%h, %!, !</TT> </TD>
<TD ALIGN="LEFT">These codes all display the current history number. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%t, %@</TT> </TD>
<TD ALIGN="LEFT">These codes both display the time of day. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%n</TT> </TD>
<TD ALIGN="LEFT">Displays the username. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%d</TT> </TD>
<TD ALIGN="LEFT">Displays the current day of the week. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%w</TT> </TD>
<TD ALIGN="LEFT">Displays the current month. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>%y</TT> </TD>
<TD ALIGN="LEFT">Displays the current year. </TD>
</TR>
</TABLE>
<BR>
</CENTER>
<P>The following is an example of setting the prompt variable:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">set prompt="%h %/"
</FONT></PRE>
<P>This command sets the prompt to display the history number of the current command,
followed by the current working directory.
<CENTER>
<H3><A NAME="Heading15<FONT COLOR="#000077">Job Control</FONT></H3>
</CENTER>
<P>Job control refers to the ability to control the execution behavior of a currently
running process. Specifically, you can suspend a running process and cause it to
resume running at a later time. <TT>tcsh</TT> keeps track of all the processes that
it starts as a result of user input. You can suspend a running process or restart
a suspended one at any time during the life of that process.</P>
<P>Pressing the Ctrl-Z key sequence suspends a running process. The <TT>bg</TT> command
restarts a suspended process in the background, and the <TT>fg</TT> command restarts
a process in the foreground.</P>
<P>These commands are most often used when you want to run a command in the background
but by accident start it in the foreground. When a command is started in the foreground,
it locks the shell from any further user interaction until the command completes
execution. This is usually fine because most commands take only a few seconds to
execute. If the command you're running is going to take a long time, you would typically
start the command in the background so that you could continue to use <TT>tcsh</TT>
to enter other commands.</P>
<P>For example, if you started a command that was going to take a long time in the
foreground, such as<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">find / -named "test" > find.out
</FONT></PRE>
<P>your shell will be tied up for several minutes. If you have done this and want
to cause the <TT>find</TT> command to continue executing in the background, you could
enter the following:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">control-z
bg
</FONT></PRE>
<P>This would suspend the <TT>find</TT> command and then restart it in the background.
The <TT>find</TT> command would continue to execute, and you would regain control
of <TT>tcsh</TT>.
<CENTER>
<H3><A NAME="Heading16<FONT COLOR="#000077">Key Bindings</FONT></H3>
</CENTER>
<P>Like the <TT>pdksh</TT>, <TT>tcsh</TT> provides the ability to change and add
key bindings. The <TT>tcsh</TT> implementation of key bindings is more powerful than
the way key bindings are done in <TT>pdksh</TT>.</P>
<P>With <TT>tcsh</TT> you can bind to things other than the built-in editor commands.
This means that you can bind a key to a UNIX command, for example. <TT>tcsh</TT>
also enables you to bind <TT>vi</TT> editing commands, whereas <TT>pdksh</TT> only
allows the binding of <TT>emacs</TT> editing commands.</P>
<P>Key bindings can be very useful, especially if you're using a favorite editor
other than <TT>emacs</TT> or <TT>vi</TT>. The basic syntax for defining key bindings
is<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">bindkey [option] <instring or keyname> <outstring or command>
</FONT></PRE>
<P>The options that <TT>bindkey</TT> supports are not discussed in this book. If
you want to learn about the <TT>bindkey</TT> options, refer to the <TT>tcsh</TT>
man page. The basic function of the <TT>bindkey</TT> command is to bind the key sequence
contained in the first argument to the command contained in the second argument.</P>
<P>The following list gives some of the most useful editing commands that you can
bind key sequences to, along with the default key binding for that command. You can
list all the bindings that are defined in <TT>tcsh</TT> by typing the <TT>bindkey</TT>
command without any arguments.
<UL>
<LI><TT>beginning-of-line</TT> (<TT>^A</TT>): Moves the cursor to the beginning of
the command line.
<P>
<LI><TT>backward-char</TT> (<TT>^B</TT>): Moves the cursor back one character.
<P>
<LI><TT>end-of-line</TT> (<TT>^E</TT>): Moves the cursor to the end of the command
line.
<P>
<LI><TT>forward-char</TT> (<TT>^F</TT>): Moves the cursor forward one character.
<P>
<LI><TT>backward-delete-char</TT> (<TT>^H</TT>): Deletes the character to the left
of the cursor.
<P>
<LI><TT>kill-line</TT> (<TT>^K</TT>): Deletes all of the characters to the right
of the cursor.
<P>
<LI><TT>clear-screen</TT> (<TT>^L</TT>): Removes all of the text from the shell window.
<P>
<LI><TT>down-history</TT> (<TT>^N</TT>): Moves down one command in the history list.
<P>
<LI><TT>up-history</TT> (<TT>^P</TT>): Moves up one command in the history list.
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -