You are on page 1of 11

IOS203_Ch4

Chapter 4: Command History and Vim Editor

Using the history Command ........................................................................ 3


The Vim Editor............................................................................................. 5
Command Line Expansion ................................................................................................... 7

1
IOS203_Ch4

Objectives
Upon completion of this chapter, the student should be able to:

 Use history and editing tricks;


 Use vim to navigate, search and save document;
 Use vim to change, delete, yank, and put text;
 Use command-line expansion.

Keywords
history, .bash_history, HISTSIZE, HISTFILE, HISTFILESIZE, vi, vim, Vim Modes.

2
IOS203_Ch4

The bash shell has many tools and facilities to help you type your commands quickly and correctly
without memorizing every command and argument. It stores a history of executed commands,
which can be used to repeat commands when needed. It has newer version of vi, the standard
Unix and easy text editor. Furthermore, the bash shell expands certain command line
metacharacters before interpreting the command.

1. Using the history Command


When using the bash shell, every command you type is stored in ~/.bash_history:

The history command (alone without options or arguments) displays all the shell command
history. Typing history followed by a number causes only that number of the latest commands to
appear. For example, to show only the last 10 commands, run the command line:

[student@StudentHost ~]$ history 10

You can also use the following command line to show the same result:

[student@StudentHost ~]$ history | tail

Typing history -c clears the history, which can be handy if you have recently typed commands
you’d rather not have discovered by others (such as commands that include passwords).

To pull the command out of the history, you should press the Up arrow key on your keyboard;
this brings up the previous command. Pressing the Up arrow key repeatedly moves through
multiple commands so you can find the one you want. If you miss the wanted command, press
the Down arrow key to move down the history. You can also use the Ctrl+P and Ctrl+N keystrokes
double for the Up and Down arrow keys, respectively.

3
IOS203_Ch4

HISTSIZE variable

The HISTSIZE variable determines the number of commands that will be remembered in your
current environment. Most distributions default this variable to 500 or 1000. You can show and
modify the value of HISTSIZE variable as follows:

In order to change permanently the value of HISTSIZE, you should modify the value in the
~/.bash_profile as follows:

[student@StudentHost ~]$ echo “export HISTSIZE=2000” >> .bash_profile

HISTFILE variable

The HISTFILE variable points to the file that contains your history. In the bash shell, the default
value of HISTFILE variable is ~/.bash_history.

[student@StudentHost ~]$ echo $HISTFILE


/home/student/.bash_history

HISTFILESIZE variable

The HISTFILESIZE variable determines the number of commands kept in your history file.

[student@StudentHost ~]$ echo $HISTFILESIZE


1000

The difference between HISTSIZE and HISTFILESIZE is that HISTSIZE limits the number of
commands shown by the command history while HISTFILESIZE limits the number of commands,
which can be saved in $HISTFILE.

When one exits the bash, if there are more than $HISTSIZE number of commands which have
been executed in the single bash session, the contents of $HISTFILE will be replaced by the
$HISTSIZE number of commands. If there are less than or equal to $HISTSIZE number of
commands in the bash session, these commands will be appended to $HISTFILE as long as
$HISTFILESIZE permits.

You can use ctrl-r to search in the history. In the example below I typed ctrl-r followed by two
characters hi and it finds the last command containing these two consecutive characters.

4
IOS203_Ch4

! Character

When you type ! followed by the number representing the command you want repeated, then
the shell will echo the command and execute it.

Some other advanced ways of retrieving commands from the history are shown in the following
table:

Trick Description
!! repeat last command
!char repeat last command that started with char
!-n repeat a command entered n commands back

2. The Vim Editor


Most Linux distributions is shipped with Vim text editor; Vim stands for “Vi improved”, where Vi
is the standard Unix text editor. The information presented here applies to both Vi and Vim. Most
distributions that ship with Vim support launching it by typing vi (vi is an alias to vim). To start
vim, you should type:

[student@StudentHost ~]$ vim

The vim edit will show up in the terminal window:

Figure 1: help message shown when opening Vim editor

To edit the file filename, type:

[student@StudentHost ~]$ vi filename

5
IOS203_Ch4

If the file exist, the file is opened and the content is displayed. If the file doesn’t exist, vi creates
it when you save it for the first time.

Vim modes

Vim has three modes: Insertion mode, Command mode and Ex mode (or last line mode).

Figure 2: Vim Modes

 Command mode: this mode accepts commands to move cursor and delete/paste text.
Usually, these commands are entered as single character. For instance, (i) enters Vim in Insert
mode;
 Insert mode: you use this mode to type new text. When you press Esc key, you returns Vim
to Command mode;
 Ex mode: you use this mode to manipulate files (including save and exit), and to search and
replace function. You enter Vim in Ex mode from Command mode by typing a colon (:).

If you are not sure what mode Vim is in, press the Esc key to return to Command mode, from
which you can reenter Insert mode. The following table shows the most common text-editing
commands:

command Description
Quitting
ZZ save and quit
:x save and quit
:q quit (unless changes)
:q! quit (force, even if unsaved)
:w save without exit
:w! forcing save changes without exit
:wq Save and quit
:e! toggle with last edited file
Inserting Text (insert mode)
i,I insert before cursor, before line
a,A append after cursor, after line
o,O open new line after, line before
r,R replace one char, many chars
Cursor Movement
h,j,k,l left, down, up, right
w,W next word, blank delimited word
b,B beginning of word, of blank delimited word

6
IOS203_Ch4

e,E end of word, of blank delimited word


0,$ beginning, end of line
Deleting Text
x,X character to right, left
D to end of line
dd line
:d line
Yanking (Copying) Text
yy line
:y line
Search for Strings
/string search forward
?string search backward
n,N repeat search in same, reverse direction
/^string forward search string at beginning of line
/string$ forward search string at end of line
/br[aeio]l search for bral, brel, bril and brol
/\<he\> search for the word he (and not for here or the)
Other
~ toggle upper/lower case
J join lines
. repeat last text-changing command
u,U undo last change, all changes on line
:w file write file (current file if no name given) :
:w >>file append file (current file if no name given)
rc replace a character with c
:r fname (read) file fname and paste contents
<Ctrl-r> redo last “undone” change
cw,dw,yw change word, delete word, yank word

3. Command Line Expansion


The bash shell supports several expansions on the command line. The following table lists some
types of bash expansions:

Expansion Syntax Expands To

History (explained above) ! Previously executed command

Brace {} Specified text

Tilde ~username A user's home directory


Variable $, ${...} Shell and environment variables

Arithmetic $((...)) Numeric calculation

7
IOS203_Ch4

Output from command run in


Command Substitution `...`, $(...)
subshell
*, ?, [...], Matched filenames in filesystem
File Globbing (explained in previous chapter)
[^...]

Brace expansion { }

For example, the expression {c,b,p}ut would expand into the three words cut but put. You can
quickly create a set of directories (or files) using one command:

[student@StudentHost ~]$ mkdir Dir{01,02,03}

In the mkdir command, the braced word is expanded to the three directories Dir01, Dir02 and
Dir03.

Tilde expansion ~

The tilde expansion merely expands a ~ (or ~username) to the user's (or the username's) home
directory, as listed in the /etc/passwd file.

Variable expansion $, ${…}

The bash shell will expands expressions of the form $VARNAME or ${VARNAME} to the value of
the shell or environment variable VARNAME.

Quoting and escaping


The escape character \ protects the next character from being interpreted by the shell.

The double quotes "..." protects enclosed characters from being interpreted by the shell, with the
exceptions of the $, !, and ` (backtick) characters. They are used when you would like to treat
most punctuation literally or to combine words into a single token, but still be able to make use
of variables, command substitution, and history substitution.

The single right quotes '...' protects all enclosed characters from being interpreted by the shell.
They are used in similar situations as double quotes when you want all punctuation, including
variables and command substitution, to be treated literally.

Single left quotes (Back Ticks) `...` are not used for quoting. Instead, back ticks are used to invoke
command substitution on the include text.

8
IOS203_Ch4

Arithmetic expansion
Arithmetic operators such as +, -, *, and / on the command line do not have their traditional
mathematical meaning. However, the bash shell treats them within a $((...)). First, variables are
treated as numeric integers where appropriate, and secondly, standard mathematical operators
such as +, -, *, and / are treated as such. The bash shell will "expand" the entire expression, and
replace it with the numeric result. Here are two examples:

Command substitution
Command substitution allows users to run arbitrary commands in a subshell and incorporate the
results into the command line. You can do that by using:

 `subcommand`: “back ticks", the left leaning single quote, found on the same key as ~,
next to the 1 key on most keyboards;
 $(subcommand): similar to arithmetic expansion, but with only one set of parentheses.

Tab completion
You can use <TAB> key to complete command line:

 For the command name, it will complete a command name;


 For an argument, it will complete a file name (or a directory name);

Try to type:

[student@StudentHost ~]$ ls ~/Des<TAB>

This will automatically be completed to:

[student@StudentHost ~]$ ls ~/Desktop/

If you down’t remember the spelling of a command (or a script name), you can type first two
letters and then type <TAB> twice. For example, try to type:

[student@StudentHost ~]$ wh <TAB><TAB>

9
IOS203_Ch4

The following command names appear on the terminal window:

Whatis whereis which while whilptail who whoami

and now, you can choose the command you want to use.

Command line editing


Linus supports a command line editor which is capable of treating written commands. The most
common ticks used for editing command lines are shown in the following table:

Trick Description

<Ctrl-a> or Home move cursor to the beginning of line


<Ctrl-e> or End move cursor to the end of line
<Esc> then f move cursor to the beginning of next word
<Esc> then b move cursor to the beginning of previous or current word
<Ctrl-u> delete the characters to the left of the cursor
<Ctrl-k> delete the characters to the right of the cursor
<Ctrl-b> or LeftArrow moving backward within a line
<Ctrl-f> or RightArrow moving forward within a line
<Ctrl-d> or Del delete the character under the cursor

10
IOS203_Ch4

Questions
1. Type the command: ls –ld, then repeat the previous command using only two characters
(there are more than one solution)
2. Display the last 6 commands you typed
3. Issue the command you typed in question 1 again, using the line numbers you received from
the command in question 3.
4. How many commands can be remembered in memory for your current shell session?
5. When exiting the shell, where are these commands stored?
6. When exiting your current shell session, how many commands can be written to the history
file?
7. How can you verify that your current bash shell remembers the next 5000 commands you
type.
8. Open more than one console with the same user account. When is command history written
to the history file?
9. Using vi(m) editor to answer the following questions:
a. What 3 key sequence in command mode will duplicate the current line.
b. What 3 key sequence in command mode will switch two lines' place (line five
becomes line six and line six becomes line five).
c. What 2 key sequence in command mode will switch a character's place with the
next one.
10. Create a directory and enter it. Then create the following files inside the directory using one
command: file1 file10 file11 file2 File2 File3 file33 fileAB filea fileA fileAAA file( file 2
(the last one “file 2” has 6 characters including a space)
11. List all files starting with file or File
12. List all files starting with file and ending with a letter
13. List all files starting with File and having a digit as fifth character and nothing else.
14. List all files starting with a letter and ending in a number.
15. List all files that have exactly five characters.
16. List all files that start with f or F and end with 3 or A.
17. List all files that start with f have i or R as second character and end in a number.
18. List all files that do not start with the letter F.
19. Can echo command replace ls command? How can you list the files in the current directory
with echo?
20. Is there another command besides cd to change directories?

References
[1] Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
[2] Paul Cobbaut, “Linux Fundamentals”, https://linux-training.be/funhtml/index.html. Updated
on 2015-05-24

11

You might also like