vi Command in Linux
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Overview
The vi command is a powerful and versatile text editor found in Linux operating systems. It allows users to create, edit, and navigate through text files with various modes, commands, and shortcuts. In this article, we will cover the syntax, options, and examples of the vi command.
Linux vi Command Syntax
The syntax for the vi command is as follows:
Where:
- options: Optional flags that alter the behavior of the vi command.
- file: The name of the file to be edited or created.
vi Command Options:
- -R: Opens the file in read-only mode, preventing any changes from being saved.
- -c command: Executes the given command upon starting vi.
- +n: Opens the file and places the cursor at line number n.
- -r: Recovers a file after a crash, using the swap file.
Example Usages
-
Opening a file for editing:
Explanation: Opens the 'example.txt' file in the vi editor. If the file does not exist, it will be created.
-
Opening a file and going to a specific line:
Explanation: Opens the 'example.txt' file in the vi editor and positions the cursor at line 10.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Tips
-
Use 'i' to enter insert mode, 'ESC' to exit insert mode, and '
' to save and quit the file. -
Use 'h', 'j', 'k', and 'l' keys to move the cursor left, down, up, and right, respectively.
Advanced Use Cases of vi Command in Linux
-
Searching for a pattern in the file:
Explanation: Searches for the specified pattern in the file. Press 'n' to jump to the next occurrence of the pattern.
-
Replacing all occurrences of a pattern:
Explanation: Replaces all occurrences of the 'old' pattern with the 'new' pattern in the entire file.
-
Undo and redo changes:
Explanation: Press 'u' to undo the last change. Press 'Ctrl+r' to redo the change.
Turn Learning into Career Growth
Conclusion
-
The vi command is a powerful text editor in Linux operating systems.
-
vi offers a wide range of options, commands, and shortcuts for efficient text editing.
-
Advanced usages include searching for patterns, replacing patterns, and undoing and redoing changes.




