0% found this document useful (0 votes)
10 views

chapter5_OperatingSystems

The document discusses process management in Unix, detailing the use of system calls like nice(), fork(), exec(), and wait() for process creation and management. It explains the significance of commands such as ps, bg, fg, and kill in controlling process execution and priority. Additionally, it covers the differences between parent and child processes, and the use of commands like find, chown, bc, and wc for file and process management.

Uploaded by

Manisha Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

chapter5_OperatingSystems

The document discusses process management in Unix, detailing the use of system calls like nice(), fork(), exec(), and wait() for process creation and management. It explains the significance of commands such as ps, bg, fg, and kill in controlling process execution and priority. Additionally, it covers the differences between parent and child processes, and the use of commands like find, chown, bc, and wc for file and process management.

Uploaded by

Manisha Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Operating systems – managing processes in Unix

a)comment: ”no process can preempt another process executing in the kernel”
Ans:
1.what is the use of nice()system call? Explain it with proper syntax.
Ans: Processes are sequentially assigned resources for execution.
The kernel assigns the CPU to a process for a duration of time (called time slice).
When the time elapses, the process is placed in a queue.
The execution of processes is scheduled depending on the priority assigned to the process.
The nice command is used to control background process dispatch priority.
The nice command is used with & operator to reduce the priority of processes.
The main idea of nice: background processes must demand less attention from the system than interactive
processes.
nice values are system dependent and typically range from 1 to 19.
A high nice value implies a lower priority.
The lower the nice number, the more important a job is and the more resources it will take without sharing
them.
Example:
$ nice wc –l hugefile.txt Changing priority of Process (-n)
The user can specify the nice value explicitly with –n (number) option where number is an offset to the
default.
The priority is increased by that number up to a limit of 20.
Example:
$ nice –n 5 wc –l hugefile.txt & ;nice value increased by 5 unit
2.what is process status? Explain Ps with options.
Ans: This command used to display some process attributes. Ps can be seen as the process counterpart
of the file system’s ls command. The command reads through the kernel’s data structures and process
tables to fetch the characteristics of processes. By default, ps displays the processes owned by the user
running the command. If user execute the command immediately after logging in, we see following
Example:
$ ps
PID TTY TIME CMD
5314 pts/7 00:00:00 bash
5314 pts/7 00:00:00 ps
The header includes the following information:
PID – Process Identification number
TTY – terminal with which the process is associated Time - CPU time taken by the process
CMD – Process name.
Ps is a highly variant command; its actual varies across different unix flavors. BSD and system V are at
war here; there are hardly any options common to both systems. For instance, ps -e on sytem V
approximates to ps aux on BSD. Full listing(-f) to get detailed listing the parent of every process, use the
-f (full) option
$ps -f
UID PID PPID C STIME TTY TIME CMD
Sumit 367 291 0 12:35:16 console 0:00 vi create_user.sh
Sumit 291 1 0 10:24:58 console 0:00 -bash
Sumit 368 367 0 12:35:18 console 0:00 /usr/bin/bash -i
UID – Login name of the user
PID – Process ID
PPID – Parent process ID
C – An index of recent processor utilization, used by kernel for scheduling
STIME – Starting time of the process in hours, minutes and seconds
TTY – Terminal ID number
TIME – Cumulative CPU time consumed by the process
CMD – The name of the command being executed
Displaying processes of a user(-u) :the system administrator needs to use the -u(user) option to know
the activities of any user.
$ ps -u sumit
PID TTY TIME CMD
375 ? 0:05 Xsun
403 ? 0:00 Xsession
339 pts/3 0:00 bash
Displaying all user processes (-a) :the -a(all) option lists processes of all users but doesn’t display the
system processes:
$ ps -a
PID TTY TIME CMD
662 pts/01 00:00:00 sh
705 pts/04 00:00:04 vi
System Processes(-e or -A) : In addition to the user processes, a no. of system-processes keep running
all the time in the system. Most system-processes are not associated with any controlling terminal (Ex:
pts/01). System-processes are created during system start-up or when the system goes into multiuser
mode. System-processes are known as daemons because they are called w/o a specific request from a
user. –e (every) option can be used to display all processes including both user & system processes.
$ps -e
PID TTY TIME CMD
170 ? 0:01 init
2908 pts/6 0:00 bash
3.what is process? Explain the mechanisms of process creation
Ans: User running: Process executes in user mode
Kernel running: Process executes in kernel mode
Ready to run in memory: process is waiting to be scheduled
Asleep in memory: waiting for an event
Ready to run swapped: ready to run but requires swapping in
Preempted: Process is returning from kernel to user-mode but the system has scheduled another process
instead
Created: Process is newly created and not ready to run
Zombie: Process no longer exists, but it leaves a record for its parent process to collect.
1.The parent process uses fork() :to create a child process.
After fork() executes, the parent & child have different PIDs and PPIDs.
When fork is invoked, the kernel replicates address space of the parent process to the child process.
When a process is forked, the child inherits following attributes of the parent.
User name of the real and effective user (RUID and EUID).
Real and effective group owner (RGID and EGID).
The current directory from where the process was run.
The file descriptors of all files opened by the parent process.
Environment variables like HOME, PATH.
'.' child runs in its own address space, changes made to these parameters don't affect the parent.
Forking process is responsible for the multiplication of processes in the system.
At this point, both parent & child continue execution at the statement following fork
2.exec :forking only creates a process but it doesn’t execute a new program.
The child process uses exec() to executes its own new program.
This operation replaces the entire address space with that of the new program.
The PID and PPID of the child remain unchanged.
At the end of the execution, exit() is called to terminated.
3.Wait :when the child process starts its execution, the parent process can do 2 things. 1)wait to gather the
child’s exit status 2)continue execution without waiting for the child.
In first case, the parent uses wait() to wait for the child to complete its task.
Finally, parent picks up the exit status of the child and continues with other tasks.
4.explain any four commands related to process in Unix OS
Ans:
5.explain the commands bg, fg, and kill.
Ans: bg: This command can be used to move currently running foreground process to run in background.
Syntax:
bg [PID...] OR bg %jobname
example: fg brings most recent background process
fg %1 brings first job to foreground
fg %sort brings sort job to foreground
fg this command can be used to move currently running background process to run in foreground.
fg %jobno OR fg %jobname
example : bg %2 sends second job to background
bg %?perm sends to background job containing string perm
kill Terminating a process prematurely is called killing.
The kill command can be used to terminate a background process.
<ctrl-c> can be used to terminate a foreground process.
In order to communicate with an executing process from the shell, you must send a signal to that process.
The kill command sends a signal to the process.
You can direct the signal to a particular process by using its pid
The kill command has the following format:
kill [options] pid
-l lists all the signals you can send
-p (on some systems prints process information)
-signal is a signal number
Syntax:
kill PIDs OR kill –s NUMBER
Example:
$ kill 123 // To kill a process whose PID is 123
$ kill 123 342 73 // To kill several processes whose PIDs are 123, 342, and 73
Kill command uses SIGTERM (15) as default signal for terminating a process.
The programs can send/receive more than 20 signals, each of which is represented by a number.
6.what is the parent and child process? Explain briefly.
Ans: A process that creates another process is called the parent process.
A newly created process is called the child process.
When user enters a command at the prompt($), the shell becomes the parent process, which in turn
creates a child process.
For example:
1)When the command cat emp.lst is run from the keyboard, the shell process creates a child process for
running the cat program.
2) When commands like "cat sample.lst | grep MH student.lst" is run from the keyboard, the shell process
creates two child processes simultaneously:
1) one for running the cat program and
2) another for running the grep program.
The shell is said to be the parent of cat & grep.
7.what is the difference between a process run with & and nohup?
Ans:
8.how is a process created? What are the system calls associated to create a process?
Ans: In UNIX, a new process is created by means of the fork() - system call.
The OS performs the following functions:
It allocates a slot in the process table for the new process
It assigns a unique ID to the new process
It makes a copy of process image of the parent (except shared memory)
It assigns the child process to the Ready to Run State
It returns the ID of the child to the parent process, and 0 to the child.
Note, the fork() call actually is called once but returns twice - namely in the parent and the child process.
The system calls associated to create a process are
1.Exec :Forking only creates a process but it doesn't execute a new program.
The child process uses exec() to execute its own new program.
This operation replaces the entire address space with that of the new program.
The PID and PPID of the child remains unchanged.
At the end of the execution, exit() is called to terminate the child.
2. The exit() call is used to terminate a process.
Its prototype is: void exit(int status), where status is used as the return value of the process.
exit(i) can be used to announce success and failure to the calling process.
3.The wait() call is used to temporarily suspend the parent process until one of the child processes
terminates.
The prototype is: pid_t wait(int *status), where status is a pointer to an integer to which the child’s status
information is being assigned.
wait() will return with a pid when any one of the children terminates or with -1 when no children exist.
4. To wait for a particular child process to terminate, we can use the waitpid() call.
Prototype: pid_t waitpid(pid_t pid, int *status, int opt)
9.with suitable examples explain about the commands a)find b)chown c)bc d)wc
Ans: 1.This command show recursively examines a directory tree to look for files matching some criteria
and then takes some action on the selected files.
Syntax:
find path_listselecton_criteria action
Here, path_list consists of one or more directories to be examined selection-criteria contains conditions
used for matching a file action specifies some action to be taken on the selected files
Example:
$find / -name a.out -print // locates all files named a.out
/home/kumar/a.out
/home/user/a.out
Here,
path_list / indicates that the search should start from root directory.
Then, each file in the list is matched against the selection criteria.
The selection criteria always consists of an expression in the form –operator argument.
If the expression matches the file, the file is selected.
Finally, a display selected files on the terminal.
2.chmod command can be used to change the ownership of a file.
Syntax : chown USERNAME FILENAME
The value of the user can be the name or uid(user id) of a user on the system.
Ex : $ ls -l note
-rwxr----x 1 kumar ISE 347 may 10 20:30 note
$ chown sharma note
Is -l note //now kumar sharma becomes new owner of the file
-rwxr----x sharma ISE 347 may 10 20:30 note
Now, new owner will have same file permission as that of old owner
Old owner cannot edit “note” since there is no write privilege for group and others
Old super user can change the ownership of any file
But normal users can change the ownership of only those files that they own.
3. bc command is used for command line calculator. It is similar to basic calculator by using which we
can do basic mathematical calculations.
Arithmetic operations are the most basic in any kind of programming language. Linux or Unix
operating system provides the bc command and expr command for doing arithmetic calculations. You
can use these commands in bash or shell script also for evaluating arithmetic expressions.
Syntax : bc [ -hlwsqv ] [long-options] [ file ... ]
4.wc command can be used to get a count of the total number of lines, words, and characters contained
in a file
Syntax : wc FILENAME
Example $ cat P1.c
Welcome
To UNIX
LINE WORD CHARACTER FILENAME
3 3 15 P1.c
Header includes 1.line : this represents the total number of lines in the files. 2.word : this represents the
total number of words in the file(excluding space, tab and newline) 3.character: this represents the
total number of character in the file(excluding space, tab and newline) 4.filename :this represents the
name of the file
This commands can also accept more than one filename as arguments
Example : $wc P1.c P2.c
3 3 15 P1.c
3 3 15 P2.c
10.Explain the following i)ps ii)tr iii)cut iv)kill v)nohup
Ans: ps: This command used to display some process attributes. Ps can be seen as the process counterpart
of the file system’s ls command. The command reads through the kernel’s data structures and process
tables to fetch the characteristics of processes. By default, ps displays the processes owned by the user
running the command. If user execute the command immediately after logging in, we see following
Example:
$ ps
PID TTY TIME CMD
5315 pts/7 00:00:00 bash
5315 pts/7 00:00:00 ps
The header includes the following information:
PID – Process Identification number
TTY – terminal with which the process is associated Time - CPU time taken by the process
CMD – Process name.
tr: The tr command in UNIX is a command line utility for translating or deleting characters. It supports
a range of transformations including uppercase to lowercase, squeezing repeating characters,
deleting specific characters and basic find and replace. It can be used with UNIX pipes to support
more complex translation. tr stands for translate.
Syntax : $ tr [OPTION] SET1 [SET2]
The cut command in UNIX is a command for cutting out the sections from each line of files and writing
the result to standard output. It can be used to cut parts of a line by byte position, character and field.
Basically the cut command slices a line and extracts the text. It is necessary to specify option with
command otherwise it gives error. If more than one file name is provided then data from each file is not
precedes by its file name.
Syntax : cut OPTION... [FILE]...
Kill : Terminating a process prematurely is called killing.
The kill command can be used to terminate a background process.
<ctrl-c> can be used to terminate a foreground process.
In order to communicate with an executing process from the shell, you must send a signal to that process.
The kill command sends a signal to the process.
You can direct the signal to a particular process by using its pid
The kill command has the following format:
kill [options] pid
-l lists all the signals you can send
-p (on some systems prints process information)
-signal is a signal number
Syntax:
kill PIDs OR kill –s NUMBER
Example:
$ kill 123 // To kill a process whose PID is 123
$ kill 123 342 73 // To kill several processes whose PIDs are 123, 342, and 73
Kill command uses SIGTERM (15) as default signal for terminating a process.
The programs can send/receive more than 20 signals, each of which is represented by a number.
nohup: nohup statement can be prefixed to a command to permit execution of the process even after the
user has logged out.
Here also, the shell operator & must be used.
Syntax:
nohup command-string [input-file] output-file &
Example:
$ nohup sort emp.lst &
1252 Sending output to nohup.out
Here, the shell immediately returns a PID(1252) of the invoked command.
nohup sends the standard output(or error) of the command to the file nohup.out.
When the user logs out, the child becomes an orphan and the init process becomes parent of orphan.
In this case, the kernel reassigns the PPID of the orphan to the system’s init process (PID 1).
In this way, the user can terminate a parent (the shell) without terminating its child.
11.what is a process. Mention briefly the role of fork, exec mechanism of process creation.
Ans: A process is a program in execution.
The process is said to be born when the program starts execution.
The process remains alive as long as the program is active.
The process is said to be dead when the program completes execution.
There are three distinct phases in the creation of a process and uses three imp. System calls or functions
fork, exec and wait 1.fork : a process in unix is created with the fork system call, which creates a copy of
the process that invokes it. The process image is practically identical to that of the calling process, gets a
new PID. The forking mechanism is responsible for the multiplication of processes in the system.
2.exec: forking creates a process but it is not enough to run a new program. To do that, the forked child
needs to overwrite its own image with the code and data of the new program. This mechanism is called
exec, and the child process is said to exec a new program. No new process is created here, the PID and
PPID of the exec’d process remain unchanged.
3.wait: the parent then executes the wait system call to wait for the child process to complete. It picks up
the exit status of the child and then continues with its other functions. A parent may not decided to wait
for the death of its child
12.explain the following command with example i)running jobs in background(& and nohup)
ii)execute later.(at and batch)
Ans: &: No logging out -The & is the shell’s operator used to run a process in the background. The
parent in this case doesn’t wait for the child’s death. Terminate the command line with and &; the
command will run in the background:
$ sort –o emp.dat emp.dat &
[1] 1413 The job’s PID
Here, the shell immediately returns a PID(1413) of the invoked command.
The prompt is returned($).
The shell is ready to accept the next command even though the previous command has not been
terminated yet.
The shell remains the parent of the background process
nohup: log out safely -background jobs cease to run, however, when a user logs out(the C shell and
Bash excepted) That happens because her shell is killed. And when the parent is killed, its children are
also normally killed. The UNIX system permits a variation in this default behaviour. When prefix to a
command, permits execution of the process even after the user has logged out.
Syntax:
nohup command-string [input-file] output-file &
Example:
$ nohup sort emp.lst &
1252 Sending output to nohup.out
Here, the shell immediately returns a PID(1252) of the invoked command.
nohup sends the standard output(or error) of the command to the file nohup.out.
When the user logs out, the child becomes an orphan and the init process becomes parent of orphan.
In this case, the kernel reassigns the PPID of the orphan to the system’s init process (PID 1).
In this way, the user can terminate a parent (the shell) without terminating its child.
13.write a short note on find command
Ans: This command show recursively examines a directory tree to look for files matching some criteria
and then takes some action on the selected files.
Syntax:
find path_listselecton_criteria action
Here, path_list consists of one or more directories to be examined selection-criteria contains
conditions used for matching a file action specifies some action to be taken on the selected files
Example:
$find / -name a.out -print // locates all files named a.out
/home/kumar/a.out
/home/user/a.out
Here,
path_list / indicates that the search should start from root directory.
Then, each file in the list is matched against the selection criteria.
The selection criteria always consists of an expression in the form –operator argument.
If the expression matches the file, the file is selected.
Finally, a display selected files on the terminal.
14.how to execute commands in foreground and background? Give example.
Ans: Foreground Processes :By default, every process that you start runs in the foreground. It gets its input
from the keyboard and sends its output to the screen.
Background Processes : A background process runs without being connected to your keyboard.
If the background process requires any keyboard input, it waits.
The advantage of running a process in the background is that you can run other commands; you do not
have to wait until it completes to start another!
There can be only one job in the foreground, the remaining jobs have to run in the background.
There are two ways of starting a job in the background:
with the shell’s & operator
nohup command.
& : No Logging out :The & is the shell’s operator used to run a process in the background.
The user can direct the shell to execute the command in the background.
In this case, the parent doesn’t wait for the child’s termination.
Example:
$ sort –o emp.dat emp.dat &
[1] 1413 The job’s PID
Here, the shell immediately returns a PID(1413) of the invoked command.
The prompt is returned($).
The shell is ready to accept the next command even though the previous command has not been
terminated yet.
The shell remains the parent of the background process.
nohup :nohup statement can be prefixed to a command to permit execution of the process even after the
user has logged out.
Here also, the shell operator & must be used.
Syntax:
nohup command-string [input-file] output-file &
Example:
$ nohup sort emp.lst &
1252 Sending output to nohup.out
Here, the shell immediately returns a PID(1252) of the invoked command.
nohup sends the standard output(or error) of the command to the file nohup.out.
When the user logs out, the child becomes an orphan and the init process becomes parent of orphan.
In this case, the kernel reassigns the PPID of the orphan to the system’s init process (PID 1).
In this way, the user can terminate a parent (the shell) without terminating its child.

Chapter 7 : Using Vi Editor


a) What is vi editor?
Ans : The default editor that comes with the UNIX operating system is called vi (visual editor). Using vi editor,
we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file.
b) List the different modes of vi editor.
Ans : Command mode – this is the mode can be treated as default, where you start in using the VI editor.
Input/edit mode – this mode allows you to do text editing, i.e. edit your file's content.
Ex mode – this mode is used to interact with vi and it contains instructions to process a file. basically
execute file with different parameters.
c) Explain What Does The /text Command Do?
Ans : Text command allow user to the add/insertion of text
B. Long Answer Questions
a) Write short note on vi editor.
Ans : The default editor that comes with the UNIX operating system is called vi (visual editor). Using vi
editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just
read a text file.
Syntax : vi [filename]
Command mode, one of the three modes used by vi. This is the mode where you can pass commands to
act on text, using most of the keys of the keyboard. Pressing a key doesn’t show it on screen but may
perform a function like moving the cursor to the next line, or deleting a line.
For text editing, vi uses 24 of the 25 lines that are normally available in a terminal. To enter text you must
switch to the input mode. First press the key marked i, and you are in this mode read to input text, each
line followed by [enter]
After text entry is complete, the cursor is positioned on the last character of the last line. this is known as
the current line and the character where the cursor is stationed is the current cursor position. Now press
the [Esc] key to revert to command mode. Invoke the ex mode from the command mode by entering a:
(colon), which shows up in the last line. Enter an x and press[Enter]
:x[Enter]
“sometext” 6 lines, 232 characters
$_
b) What are different modes of vi editor? Explain
Ans :1. command mode : the default mode of the editor where every key pressed is interpreted as a
command to run on text. You’ll have to be in this mode to copy and delete text. Unnecessary pressing of
/Esc/ in this mode sounds a beep but also confirms that you are in this mode.
2.input mode :every key pressed after switching to this mode actually shown up as text. This mode is
invoked by pressing one of the keys.
When a key of the input mode is pressed, its doesn’t appear on screen but subsequent key depression do.
We’ll consider the following commands:
Insert and append (i,a,I and A) :-
Replace(r,R,s and S)
Open a line(o and O)
1.insertion of text(i and a):-the simplest type of input is insertion of text I command is invoked with cursor
positioned on existing text, text on its right will be shifted further without being overwritten.
There are other methods of inputting text. To append text to be right of the cursor position, use existing
text will also be shifted right followed by the text you wish to key. After you have finished editing, press
[Esc] with i and a, you can append several lines of text in this way

the vi editor the vi full-screen editor

ifull-screen [Esc] a, a link of ex[Esc]

the vi full-screen editor the vi full-screen editor, a link of ex

2.insertion of text at line extremes(I and A):- I and A behave somewhat like i and a except that key work at
line extremes by performing the necessary navigation to move there:
I insert text at beginning of line
A Appends text at end of line
Vi and ex are one and the same editor
The following code forks a process
I/* oIt is due to William Joy[Esc]
/* The following code forks a process vi and ex are one and the same editor
A */[Esc] It is due to William Joy
/* The following code forks a process */

3.opening a new line(o and O):-in vi, you use o and O to open a line below from anywhere in a line, simply
press o //opens new line below current line
4.replacing text(r, s, R and S) :- to change existing text, vi provides mainly four commands. To replace a
single character with another, you should use
r //No[Esc] required
when you want to replace the letter d with 10f in a printf statement in C, you need to replace one
character with three. In that case, press

This is the vi full-screen editor

rT

this is the vi full-screen editor Replacing a Single character with r


s //replaces one character with many
R and S act in a similar manner compared to their lowercase ones except that they act on a larger group of
characters
R //Replaces all text on the right of the cursor position
S //Replaces the entire irrespective of cursor position.

Vi is a link of ex Vi is one of the links of the ex program


Sone[Esc] Rmodes[Esc]
Vi is one link of ex vi is one of the modes of the ex program

3.ex mode : when you edit a file using vi----- or for that matter, any editor----the original file isn’t disturbed
as such, but only a copy of it that is placed in a buffer. From time to time, you should save your work by
writing the buffer contents to disk to keep the disk file current. When we talk of saving a file, we actually
mean saving this buffer file. You may also need to quit vi after or without saving the buffer. These are
adequately handled by the ex mode.
Saving your work(:w) :-we have already used the ex mode command :x, to save the buffer and exit the
editor extended sessions with vi, you must able to save the buffer and remain in the editor. From time to
time, you must use the :w command to write the buffer to disk. Enter a :,which appears on the last line of
the screen, then w and finally[Enter]
:w[Enter]
“sometext”, 8 lines, 275 characters
Saving and quitting(:x and :wq):- the previous command returns you to the command mode so you can
continue editing. However, to save and quit the editor
:x[Enter]
“sometext”, 8 lines, 303 characters
$_
You can also use :wq to save and quit the editor
c) With neat diagram explain about the different modes of operation of VI editor and
commands in each mode.
Ans : refer question b
d) Explain any 3 Execute Mode Commands
Ans :1.Saving your work(:w) :-we have already used the ex mode command :x, to save the buffer and exit
the editor extended sessions with vi, you must able to save the buffer and remain in the editor. From time
to time, you must use the :w command to write the buffer to disk. Enter a :,which appears on the last line
of the screen, then w and finally[Enter]
:w[Enter]
“sometext”, 8 lines, 275 characters
You can now continue your editing work normally, but make sure that you execute this command
regularly. With the :w command you can optionally specify a filename as well. In that case, the contents
are separately written to another file
2.Saving and quitting(:x and :wq):- the previous command returns you to the command mode so you can
continue editing. However, to save and quit the editor
:x[Enter]
“sometext”, 8 lines, 303 characters
$_
You can also use :wq to save and quit the editor
3.absorting editing (:q) :-it’s also possible to abort the editing process and quit the editing mode without
saving the buffer. The :q(quit) command does that job:
:q[Enter] //won’t work if buffer is unsaved
$_
Vi also has a safety mechanism that prevents you from aborting accidentally if you have modified the file
(buffer) in any way. The following message is typical when you try to do so:
No write since last change(:q! overrides)
If the buffer has been changed and you still want to abandon the changes, then use
:q! //ingnores all changes made and quites
e) Illustrate with examples Navigation commands of vi editor.
Ans : this is mode you come to when you have finished entering or changing your text. A command mode
doesn’t show up on screen but simply performs a function. We begin with navigation. Don’t forget to avoid
the cursor control keys for navigation
Movement in the four directions(h,j,k,l) :vi provides the keys h,j,k, and l to move the cursor in the four
directions. These keys are placed adjacent to one another in the middle row of the keyboard. Without a
repeat factor, they move the cursor by one position. Use these keys for moving the cursor vertically.
k moves cursor up
j moves cursor down
to move the cursor along a line, use these commands
l takes you right and h takes you left
h moves cursor left
31
l moves cursor right
2k
the repeat factor can be used as a command prefix
k takes you up
with all these four commands. Thus, 4k moves the
cursor 4 lines up and 20h takes it 20 characters to the
j takes you down
left. Navigation with the four keys is shown in fig.
motion is relative; you can’t move to a specific line
number with these keys.
Word navigation(b, e and w):-moving by one character is not always enough; you’ll often to move faster a
line. Vi understands a word as a navigation unit which can be defined in two ways, depending on the key
pressed. If your cursor is a number of words away from your desired position, you can use the word-
navigation commands to go there directly. There are three basic commands
b //moves back to beginning of word
e //moves forward to end of word
w //moves forward to beginning of word
a repeat factor speeds up cursor movement along a line example, 5b takes the cursor words back, while 3w
takes the cursor three words forward. A word here is simply a string of alphanumeric characters and the
_(underscore). Bash is one word; so is sh_profile.tcp-ip is three words, the hyphen by definition becomes a
word too.
Moving to line extremes(0, | and $) : moving to the beginning or end of a line is a common requirement.
This is handled by the keys 0, | and $. To move to the first character of a line, use
0(zero) or | 30|moves cursor to column 30
The | takes a repeat factor and using that, you can position the cursor on a certain column. To position the
cursor on column 30, use 30|
We used $ as the line address in the ex mode to represent the last line of the file. The same symbol in the
command mode represents the end of line. To move to the end of the current line, use
$ moves to end of line

You might also like