- Unix Commands Reference
- Unix Commands - Home
ex Command in Linux
The ex command in Linux is a line editor that is used to edit files directly from the command-line. It is an extended version of ed editor, and offers more advanced features. The ex command operates in two modes; command mode and input mode. In command mode, you enter commands to manipulate text, while in input mode, you insert text into the file.
The ex command is pretty useful for automation and scripting and can be a powerful tool for batching processing and editing large files efficiently.
Table of Contents
Here is a comprehensive guide to the options available with the ex command −
- Syntax for ex Command in Linux
- Different Options Available for ex Command
- Examples of ex Command in Linux
Syntax for ex Command in Linux
The basic syntax to use the ex command on Linux is as follows −
ex [options] [filename]
Here,
- [options] are various options to customize the behavior of ex.
- [filename] is the name of the file you want to edit.
Different Options Available for ex Command
With ex command, you can use different options, which are discussed in the table provided below −
Option | Description |
---|---|
-b | Set Vim to Binary mode. |
-c <command> | Execute <command> after loading the first file. |
-C | Compatible with Vi (‘compatible’). |
--clean | Uses ‘nocompatible’, Vim defaults, no plugins and no viminfo. |
+ | Start the end of the file. |
+<lnum> | Start at line <lnum>. |
-e | Ex mode (similar to ex). |
-E | Improved Ex mode. |
-h, or --help | Print help related to command and exit. |
-L | Same as -r. |
-m | Modifications in writing files are not allowed. |
-M | Prevents modifications in text. |
-n | Avoids creating swap files; uses memory only. |
-N | Provides partial Vi compatible (‘nocompatible’). |
-o[N] | Opens N windows (default: one for each file) |
-O[N] | Similar to -o, but splits vertically. |
-p[N] | Opens N tab pages (default: one for each file) |
-r | Lists swap files and exists. |
-r (file name) | Recovers a crashed session. |
-R | Read Only mode (similar to “view”). |
-s<scriptin> | Reads normal mode commands from file <scriptin>. |
-S<session> | Sources file <session> after loading the first file. |
-T<terminal> | Set the terminal type to <terminal>. |
--cmd<command> | Executes <command> before loading any vimrc file. |
--not-a-term | Skips the warning for input/output not being a terminal. |
--ttyfail | Exists if input or output is not a terminal. |
u<vimrc> | Uses <vimrc> instead of any .vmrc. |
--version | Prints version information and exists. |
-V[N][fname] | Enable verbosity (level N) and logs messages to fname. |
-w<scriptout> | Appends all typed commands to file <scriptout>. |
-W<scriptout> | Writes all typed commands to file <scriptout>. |
Examples of ex Command in Linux
Let’s explore the basic examples of ex command in Linux −
- Open a File for Editing
- Print a Specific Line
- Substitute a Word
- Append a Text
- Insert Text Before Current Line
- Delete a Line
- Write Changes to File
- Quit the Editor
Open a File for Editing
You can use the ex command to open a file for editing on a Linux system. For example, to open a file named example.txt, you can use the following command −
ex example.txt
Print a Specific Line
You can also print a specific line from a file opened through the ex command. For example, to print the first line of the file, you can use the below-given command −
:1p
Substitute a Word
With the file opened through the ex command, you can also substitute a word in a specific line. Let’s say, to replace oldword with newword in the first line, you can simply use the following command −
:1s/oldword/newword/
Append a Text
You can also append text in your file after the current line. For example, to append text “Fellows” after the current line, you can use −
:a Fellows .
Insert Text Before Current Line
Another advantage of using the ex command to open a file is that you can insert a text before the current line in your file. For example, to insert “Good day” before the current line, you can use −
:i Good day .
Delete a Line
You can also delete a specific line in a file which is opened through the ex command. For example, to delete the third line of the file, you can use the below-given command −
:3d
Write Changes to File
Once, you are done with the editing, you can save the changes made to the file by using −
:w
Quit the Editor
To quit the editor opened through ex command, you can simple use −
:q
That’s how you can use the ex command to create or open a text file and make changes to the file according to your needs.
Conclusion
The ex command in Linux is a versatile line editor that is used for extending the capability of the ed editor with more advanced features. It operates in both command and input modes and allows users to efficiently manipulate and insert text efficiently.
This tutorial has provided the basic guidelines of the ex command, starting with the syntax and following up with options and examples. By mastering the ex command, you can significantly enhance your text editing and file management tasks on the Linux system.