How to Use vi/vim Command-Line Text Editors for Data Modification


Whether you're tweaking config files, updating scripts, or wrangling logs on a remote server, knowing how to modify data efficiently is essential. Enter vi
and vim
—powerful, lightning-fast text editors built for the command line.
In this blog, we'll walk through how to use these tools to edit data like a pro, from basic navigation to practical commands that streamline your workflow. If you're new to vi/vim
, don't worry—we'll keep it beginner-friendly and hands-on.
In Linux, the root user is like the "king" of the system — it has full access and can run any command without needing extra permission. You can switch to the root user with the command sudo -i
.
When using the EC2 default user, you can still run most commands, but for some, you might get a "permission denied" error. That’s because the EC2 user doesn’t have full access.
In real-world setups, we usually don’t use the EC2 or root user directly. Instead, we create our own user (with our name) and assign the necessary permissions to run commands securely.
As I mentioned earlier, we can’t modify files using the cat
command. To edit or update files in Linux, we use text editors like vim, vi, or nano.
Let's dive into some hands-on practice with vi
/vim
to learn how to modify data.
Connect to the EC2 instance using PuTTY or Command Prompt. If you're not sure how to do it, follow the steps in the guide provided below.
https://cloudcompute.hashnode.dev/how-to-access-your-instance-using-putty
🧑💻 Steps to Create and Read a File in Linux (EC2 Instance):
→ Login as EC2 user Use your terminal or PuTTY to connect to the EC2 instance as ec2-user
.
→ Switch to root user - Run: sudo -i (This gives you full access as the root user)
→ Create a file : Use the touch
command to create a file named Amazon
: touch Amazon
→ Check if the file has any data : lltotal 0
means there are no files with data in the current directory.root 0
(in the file listing) means the file has 0 bytes—no content yet.
→ Read the file content Use the cat
command: cat Amazon (Since the file is empty, nothing will be displayed)
→ Understand the permission string -rw-r--r---
→ Regular filerw-
→ Owner can read and writer--
→ Group can readr--
→ Others can read
→ To insert data into a new file: cat > filename
→ Type your data. Press Ctrl + D
to save and exit.
→ To view the contents of a file: cat filename
→ To append (add) more data to an existing file: cat >> filename
→ Type the additional data. Press Ctrl + D
to save and exit.
Note: cat >>> filename
is not valid syntax. Always use >>
(double greater-than) to append data.
Using cat >> filename, it will just append the data but not to modify or replace the data.
🐱 Understanding “cat” Redirection in Linux: Linux supports two valid ways to use cat
for writing to files:
1. Create or overwrite a file : cat > filename
This creates a new file or overwrites an existing one. After typing your content, press Ctrl + D
to save and exit.
2. Append data to an existing file : cat >> filename
This adds new content to the end of the file without deleting existing data. Again, press Ctrl + D
when you're done.
❌ Invalid Syntax: cat >>> filename.
This is not valid and will result in a syntax error. Linux only supports >
(overwrite) and >>
(append) for redirection.
🔁 File Redirection in Linux:
>
→ Overwrite the file
Replaces existing content with new data . Old data is deleted.>>
→ Append to the file
Adds new data after existing content. Old data is preserved.
→ With cat command we can only overwrite or append the data, but not to modify the data.
So, if you want to add data again and again, you should not use cat >>> filename
—this command is invalid. Instead, use cat >> filename
to append data, or use a text editor like nano
, vim
, or gedit
for more flexibility.
→ To open a file in vim : vim filename (or) vi filename
→ To exit vim
use the command: :wq
(Press Shift
+ :
to enter command mode, then type wq
and hit Enter
)
Vim is a powerful text editor used to modify or edit files on Linux and other Unix-like systems. It operates in three primary modes:
1. Command Mode
2. Insert Mode
3. Save & Quit Mode (Command-Line Mode (often referred to as Save & Quit Mode))
🛠️ Command Mode in vim:
Command mode is the default mode when you open the vim
editor. In this mode, you can perform various actions such as: Copying data , Deleting text, Undoing and redoing changes, Navigating through the file.
Press Esc
to return to this mode from any other.
📄 Navigating Lines in vim:
→ To move the cursor to the last line: “G” (or) Press Shift + G
(i.e., type G
in Command Mode)
→ To move to the first line: Type gg
in Command Mode.
→ To jump to a specific line: Example: go to line 3 → type 3gg ; 4rth line 4gg (or) type 3G ; 4G
→ Alternatives - shift :4 enter (shift colon - enter)
→ See which line you're on - :set number (or) Ctrl + G
→ To go to the middle of the file in vim
- :M (or) :normal! G M
— This moves the cursor to the middle line of the file.
→ Copy a line: Move to the line you want to copy and press yy
→ Paste the copied line: Press p
→ Paste the copied line 10 times: Press 10p
→ Delete a line: Move to the line and press dd
→Delete 5 lines: Press 5dd
→ undo - “u” ; Redo - “cntrl + r”
→ Copy multiple lines (e.g., 4 lines): Move to the starting line and press 4yy
, then paste with p
where needed.
To search for a word in vim
:
Forward search:
/<word>
(e.g.,/server
)Backward search:
?<word>
(e.g.,?server
)
After searching, press n
to go to the next match and N
to go to the previous match.
To replace a word in vim
: Use the command- :%s/oldword/newword/g:
— enters command mode%
— applies the command to the entire files
— stands for "substitute"g
— stands for "global", which means replace all occurrences in each line
Example- :%s/aws/devops/g
This replaces all instances of the word "aws" with "devops" throughout the file.
You cannot insert or edit text directly in command mode. To do that, you need to switch to insert mode by pressing i
.
Insert Mode in vim: Insert mode allows you to type or edit text in the file. In this mode, you can add or replace content, but only text — images cannot be inserted.
→ When you're in insert mode, you'll see -- INSERT --
at the bottom of the screen.
Switching Between Modes in vim:
→ To enter Insert Mode: Press i (from Command mode)
→ To return to Command Mode: Press Esc on your keyboard
→ After typing your text in Insert Mode, press Esc to go back to Command Mode.
Useful Insert Mode Shortcuts (from Command Mode):
→ Go to the end of the current line and enter Insert Mode: Press A
→ Go to the beginning of the current line and enter Insert Mode: Press I
→ Create new line above the current line & enter Insert Mode: Press O (uppercase 'O')
→ Create new line below the current line & enter Insert Mode: Press o (lowercase 'o')
All of these are keyboard shortcuts used in Command Mode and Insert Mode in vim
.
Difference Between Command Mode Keys and Insert Mode Keys in vim:
→Command Mode Keys: These keys perform actions like copying, pasting, deleting, navigating, etc. When you use command mode keys, you stay in command mode.
→Insert Mode Keys: These are keys (like i, I, A, o, O) that you press in command mode to switch to insert mode. Once you're in insert mode, you can start typing or editing text.
Save & Quit Mode in vim: This mode is used to save changes, quit vim
, or perform other commands. To enter this mode, press :
(colon) from Command Mode.
Common Save & Quit Commands:
:w — Save (write) the file
:q — Quit vim
:wq — Save and quit at the same time
:wq! — Force save and quit (even if the file is read-only)
:q! — Force quit without saving
Note: No need to use "Shift" — just type : to enter command-line mode.
🔚 Conclusion:
The vi
/vim
text editors are powerful tools for editing files directly from the command line, especially useful when working on Linux servers or remote systems. By mastering basic modes, navigation, and editing commands, you can efficiently create, modify, and manage files without relying on graphical interfaces.
Whether you're updating configuration files, writing scripts, or making quick edits, vim
offers speed, flexibility, and control — making it an essential skill for any system administrator or developer.
With practice, commands like :wq
, dd
, and /search
become second nature, turning a steep learning curve into a powerful productivity boost. If you're a DevOps engineer, cloud learner, or technical blogger, vi/vim proficiency streamlines daily operations and improves productivity.
Ready to level up your Linux editing game? Start small, experiment often, and let muscle memory do the rest. If you have questions or thoughts, feel free to leave a comment or share this with fellow tech enthusiasts! Thanks for reading🙏
😄Happy learning!
Subscribe to my newsletter
Read articles from Chandana Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Chandana Reddy
Chandana Reddy
Hi, I'm Chandana—a curious soul navigating the world through study, reflection, and shared wisdom. My journey is rooted in self-education: exploring new ideas, skills, and perspectives that empower personal growth. I believe that learning isn’t limited to classrooms—it’s an everyday practice that transforms who we are and how we connect. Through writing, conversations, and community-building, I share insights and tools that help others learn with purpose and passion. I’m not just a student of life—I’m a contributor to its knowledge ecosystem.