<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_definitions}
\input{common_latex/common_latex_help_format}
%===================================================================

\begin{document}

\begin{labcover}
\labtitle{Help Topic: Command Line Aliases}
\vspace{0.1in}
\labdate{Spring 2024}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labline
\end{labcover}

\wikidoc{file=Help.CMDAliases}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{Augmenting the Power of the Command Line with Aliases}
%====================================================================

\noindent
Aliases may be the single most powerful, and simplest tool available
for command line users. They also allow you to personalize your work
flow.  They allow you to save time on simple things that you may do
very often.  For example, to change directories to the directory above
the current one, you normally type:

\vspace{0.15in}
\begin{consoleverb}
 $ cd ..
\end{consoleverb}
\vspace{0.15in}

\noindent
No problem, but if do this a lot, this can be simplified by making an
alias:

\vspace{0.15in}
\begin{consoleverb}
 $ alias cdd='cd ..'
\end{consoleverb}
\vspace{0.15in}
 
\noindent
Once this is defined, I can change directories a bit more efficiently
with:

\vspace{0.15in}
\begin{consoleverb}
 $ cdd
\end{consoleverb}
\vspace{0.15in}

%------------------------------------------------------------------------
\subsection*{Add aliases to your shell configuration to make them persistent}

Although aliases can be defined on the fly on the command line as above, the
drawback is that they do not carry over to other shell sessions in another
window or at a later date after logging back in. To make more persistent aliases,
add them to your shell configuration file. The above example alias can be
achieved with the following line in your \var{.bashrc} file as in:

\vspace{0.15in}
\begin{fileverb}
 alias cdd='cd ..'
\end{fileverb}
\vspace{0.05in}

%---------------------------------------------------------------------
\subsection*{Suggestions for useful aliases}

Below are some suggestions offered up here partly from firm belief of
best practices, and partly based on what I found to work for me over time.
In the category of best practices, I recommend altering your \cmd{rm}, 
\cmd{mv} and \cmd{cp} shell commands to work in the interactive mode:

\vspace{0.15in}
\begin{fileverb}
 alias rm='rm -i'
 alias cp='cp -i'
 alias mv='mv -i'
\end{fileverb}
\vspace{0.15in}

\noindent
In my opinion, any time you're about to potentially overwrite or
remove a file, a prompt should be given to the user. You can always
override this by using \var{-f} on the command line which will override the
\var{-i}. You might have these already if you took the class example shell
configuration files. Some others:

\vspace{0.15in}
\begin{fileverb}
 alias    cdd='cd ..'
 alias   cddd='cd ../..'
 alias  cdddd='cd ../../..'
 alias cddddd='cd ../../../..'
\end{fileverb}
\vspace{0.15in}

\noindent
The above should be self-explanatory. Try it. The need to change
up a directory or three comes up a lot!

\vspace{0.15in}
\begin{fileverb}
 alias hemacs='/Applications/Emacs.app/Contents/MacOS/Emacs -bg DarkOliveGreen -fg white'
\end{fileverb}
\vspace{0.05in}

\noindent
This emacs alias explicity invokes the graphical emacs (if you
installed it from \urlx{emacsformacosx.com}) rather than the one
in \var{/usr/bin/emacs}. It also sets my background and foreground
color the way I like them.

\vspace{0.15in}
\begin{fileverb}
 alias duh1='du -d 1 -h'               (in OS X)
 alias duh1='du --max-depth=1 -h'      (in GNU/Linux)
\end{fileverb}
\vspace{0.15in}

\noindent
The \cmd{du} command is super useful. It estimates the file space
usage for the current directory and each subdirectory. The command
line options are slightly different in GNU/Linux and OS X as shown
above. By setting the depth (OSX) or max-depth (GNU/Linux) to one,
this limits the depth of reporting subdirectories to just the present
directory and subdirectories.  Otherwise it will recursively report
everything, which I find is too much. The first time you fill up your
hard drive with a runaway log file, you will find this command
indespensible.

\vspace{0.15in}
\begin{fileverb}
 alias  cdmi='cd moos-ivp'
 alias cdmim='cd moos-ivp/missions'
 alias cdmis='cd moos-ivp/src'
\end{fileverb}
\vspace{0.15in}

\noindent
In the example above the moos-ivp aliases are not what's important.
The general idea is to give yourself meaningful aliases for jumping
through your own file structure. Probaly 70\% or more of my aliases
are of this type, and may account for most of why I think using the
command line is way more efficient than anything else. My "cd aliases"
all begin with "cd" and usually what follows is a single letter for
each directory in the path, or some heuristic along these lines. If I
look over a student's shoulder midway through a course and I see them
typing "cd ../../project12/missions" rather than something like
"cdp12m", then a feeling of failure rushes over me, like I have failed
to pass on something important. Embrace this capability.

\vspace{0.15in}
\begin{fileverb}
 alias runa='cd moos-ivp/missions/s1_alpha; pAntler alpha.moos'
 alias runb='cd moos-ivp/missions/s1_bravo; pAntler bravo.moos'
\end{fileverb}
\vspace{0.15in}

\noindent
As before, the important thing here is not the actual mission. The key point
is that an alias can include {\em multiple} shell commands separated by a 
semicolon. When you're working on a particular autonomy problem, you may find
yourself launching the same mission over and over again to test little changes.
With the above style alias, you can launch the mission from wherever your 
shell happens to be in the file system. You can even add a timewarp component
to launch the mission with time warp 12, for example, with an alias like
\cmd{runa12}.

%---------------------------------------------------------------------
\subsection*{Temporarily disabling an alias}

Sometimes you may want to temporarily disable an alias like:

\vspace{0.15in}
\begin{fileverb}
 alias rm='rm -i'
\end{fileverb}
\vspace{0.15in}

\noindent
and return the \cmd{rm} command to the way it was before, without prompting
for confirmation. You can do this by typing:

\vspace{0.15in}
\begin{consoleverb}
 $ alias rm='rm'
 . . .
\end{consoleverb}


%---------------------------------------------------------------------
\subsection*{Reminding yourself of previously defined aliases}

If the power of aliases strikes you as it did me, you may find at some point
that you have a lot of aliases defined. A quick check on my own computer just
now confirms that I have over 850 aliases defined. People ask me how I can
remember them all? Well, I don't. But I remember the important ones, and even
the lesser used ones are still pretty useful when I come back to working on 
a piece of code or a paper I haven't touched in a while. 

\vspace{0.1in}
\noindent
One way to remind yourself of previously defined aliases is to just
look in the file where they are defined, e.g., \var{.bashrc}. But you
know there has to be a better way. A slightly better way is to just
type the following:

\vspace{0.15in}
\begin{consoleverb}
 $ alias 
 alias cp='cp -i'
 alias mv='mv -i'
 alias rm='rm -i'
 . . .
\end{consoleverb}
\vspace{0.15in}

\noindent
This will dump all your defined aliases in alphabetical order. At
least you didn't have to open an editor to get this information, but
the alphabetical order may actually make it harder to find what you're
looking for. A better approach is to use \cmd{grep} to find what you're 
looking for. For example, you may remember that you had an alias related
to launching emacs, so you could do this on the command line:

\vspace{0.15in}
\begin{consoleverb}
 $ alias | grep emacs
 alias hemacs='emacs --geometry=162x83+735+0 -bg DarkOliveGreen -fg white'
 alias nem='emacs -nw'
\end{consoleverb}
\vspace{0.15in}

\noindent
This will find only only those aliases that have the word "emacs" as part
of their definition. This is such a useful trick that I actually have an
alias for that in my shell configuration file as in:

\vspace{0.15in}
\begin{fileverb}
 alias afind='alias | grep'
\end{fileverb}
\vspace{0.05in}

\noindent
This can now be used instead to find all aliases with the word "emacs" in 
the definition. The search list can be further pruned to say only show aliases
that have "emacs" and "geometry" in the definition:

\vspace{0.15in}
\begin{consoleverb}
 $ afind emacs | grep geometry
 alias hemacs='emacs --geometry=162x83+735+0 -bg DarkOliveGreen -fg white'
\end{consoleverb}
\vspace{0.15in}

\end{document}

</pre></body></html>