ex command in Linux with examples - GeeksforGeeks

15 May, 2019
ex(stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transfer the text from one file to another. A user can quickly edit blocks of text larger than a single screen and also can make substitutions throughout a file for a given pattern by using the global replacement.
Syntax:
% ex [filename] 

Note: The prompt for the ex command is the ‘:’ character.
Important ex Editor Commands:
File Manipulation: There are two ways to edit a file, either we can use an old file or we can create a new file.a) Using an old fileSyntax:% ex oldfile
Example:b) Inserting a new fileSyntax:% ex newfile 
Output:p Command : This is the one of the most basic command, p for print (to the screen). Suppose if you will type 1p at the prompt, then you will notice the first line of the file:Syntax:linenumber p
Example:To print more than one line: You can leave off the p because a line number by itself is equivalent to a print command for that line. Here, you can put a range of line numbers (e.g 1, 3) means the two numbers separated which are by commas. Here, the space are optional.Syntax:Starting LineNumber, Ending LineNumber
Example:Substitute Command : Allows you to substitute one word for another.Syntax::LineNumber s/CurrentWord/NewWord/
Example:Adding Text : To enter text in a new file we use a (short for append) command and the command i is used to inserting the text before the current line.Syntax:For Append

:a
This is the text
 

For inserting text before the current line.

:i
This is the text

Example:w Command : The command w (short for ‘write’) is used to save your data.Syntax:w [filename]
Example:d Command : This is used to delete the lines.Syntax:[line_range] d
Example:Note: If no line number is given the current line will be deleted. It is possible to give a range of lines. For example 1, $d will delete the entire file.To search a string: Searches are carried out by including the search string in slashes (‘/’):Note: The special characters ‘^’ and ‘$’ can be used to assist the search. For example /^This/ will find a line beginning and ‘This’/file$/ will find a line ending in ‘file’Syntax:/string/
Example:Reverse Searches: Generally, these are executed in question marks (‘?’) by including the search string. The search will start at the current line and search backward through the file.Syntax:?string?
Example:Quit Command: The command q (short for ‘quit’) is used to quit the editor.Syntax:quit! or q!
Example:It will navigate you back to the bashg command : The g command (for ‘global’) is used to make multiple substitutions on a line.Syntax:s/old/new/g
Example:help command: Print a brief help message.Syntax:h
Example:j command : The j command (for ‘join’) is used to joines the text. We can also add ! to prevent whitespaces.Syntax:[starting LineNumber, ending LineNumber]j!
Example 1:1, 5j!     Join first five lines, preserving whitespace
File Manipulation: There are two ways to edit a file, either we can use an old file or we can create a new file.a) Using an old fileSyntax:% ex oldfile
Example:b) Inserting a new fileSyntax:% ex newfile 
Output:
a) Using an old fileSyntax:% ex oldfile
Example:
Syntax:
% ex oldfile

Example:
b) Inserting a new fileSyntax:% ex newfile 
Output:
Syntax:
% ex newfile 

Output:
p Command : This is the one of the most basic command, p for print (to the screen). Suppose if you will type 1p at the prompt, then you will notice the first line of the file:Syntax:linenumber p
Example:To print more than one line: You can leave off the p because a line number by itself is equivalent to a print command for that line. Here, you can put a range of line numbers (e.g 1, 3) means the two numbers separated which are by commas. Here, the space are optional.Syntax:Starting LineNumber, Ending LineNumber
Example:
Syntax:
linenumber p

Example:
To print more than one line: You can leave off the p because a line number by itself is equivalent to a print command for that line. Here, you can put a range of line numbers (e.g 1, 3) means the two numbers separated which are by commas. Here, the space are optional.Syntax:Starting LineNumber, Ending LineNumber
Example:
Syntax:
Starting LineNumber, Ending LineNumber

Example:
Substitute Command : Allows you to substitute one word for another.Syntax::LineNumber s/CurrentWord/NewWord/
Example:
Syntax:
:LineNumber s/CurrentWord/NewWord/

Example:
Adding Text : To enter text in a new file we use a (short for append) command and the command i is used to inserting the text before the current line.Syntax:For Append

:a
This is the text
 

For inserting text before the current line.

:i
This is the text

Example:
Syntax:
For Append

:a
This is the text
 

For inserting text before the current line.

:i
This is the text


Example:
w Command : The command w (short for ‘write’) is used to save your data.Syntax:w [filename]
Example:
Syntax:
w [filename]

Example:
d Command : This is used to delete the lines.Syntax:[line_range] d
Example:Note: If no line number is given the current line will be deleted. It is possible to give a range of lines. For example 1, $d will delete the entire file.
Syntax:
[line_range] d

Example:
Note: If no line number is given the current line will be deleted. It is possible to give a range of lines. For example 1, $d will delete the entire file.
To search a string: Searches are carried out by including the search string in slashes (‘/’):Note: The special characters ‘^’ and ‘$’ can be used to assist the search. For example /^This/ will find a line beginning and ‘This’/file$/ will find a line ending in ‘file’Syntax:/string/
Example:
Note: The special characters ‘^’ and ‘$’ can be used to assist the search. For example /^This/ will find a line beginning and ‘This’/file$/ will find a line ending in ‘file’
Syntax:
/string/

Example:
Reverse Searches: Generally, these are executed in question marks (‘?’) by including the search string. The search will start at the current line and search backward through the file.Syntax:?string?
Example:
Syntax:
?string?

Example:
Quit Command: The command q (short for ‘quit’) is used to quit the editor.Syntax:quit! or q!
Example:It will navigate you back to the bash
Syntax:
quit! or q!

Example:
It will navigate you back to the bash
g command : The g command (for ‘global’) is used to make multiple substitutions on a line.Syntax:s/old/new/g
Example:
Syntax:
s/old/new/g

Example:
help command: Print a brief help message.Syntax:h
Example:
Syntax:
h

Example:
j command : The j command (for ‘join’) is used to joines the text. We can also add ! to prevent whitespaces.Syntax:[starting LineNumber, ending LineNumber]j!
Example 1:1, 5j!     Join first five lines, preserving whitespace
Syntax:
[starting LineNumber, ending LineNumber]j!

Example 1:
1, 5j!     Join first five lines, preserving whitespace
linux-command
Linux-text-processing-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
scp command in Linux with Examples
mv command in Linux with examples
Docker - COPY Instruction
SED command in Linux | Set 2
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
Thread functions in C/C++
uniq Command in LINUX with examples
Start/Stop/Restart Services Using Systemctl in Linux