Introduction
Text editing is a fundamental skill for anyone working with Linux systems. System administrators, developers, and power users frequently need to create, modify, and manage text files for configurations, scripts, logs, and documentation.
Among the various text editors available in Linux, Vim stands out as one of the most powerful and widely used editors. While it has a steeper learning curve compared to other text editors, mastering Vim can significantly enhance your productivity when working with text files in Linux.
In this lab, you will learn essential Vim commands and techniques for efficient text editing in Linux. We will start with basic navigation and editing operations, then progress to more advanced features. By the end of this lab, you will have gained practical experience using Vim for common text editing tasks.
Opening and Closing Files in Vim
In this step, you will learn how to open and close files using Vim. Vim is a powerful text editor available on virtually all Linux systems. While it may initially seem complex, learning a few basic commands will help you become comfortable with this essential tool.
Opening a File with Vim
Let's start by creating a new directory for our practice files. We'll work in the ~/project directory, which is already set up for you.
First, let's create a simple text file using the touch command:
touch ~/project/notes.txt
This command creates an empty file named notes.txt in the project directory.
Now, let's open this file with Vim:
vim ~/project/notes.txt
After executing this command, you should see a new screen with the Vim editor. You might notice some tildes (~) along the left side of the screen. These indicate lines that are not part of the file (empty space).
Understanding Vim Modes
One of the key concepts in Vim is that it has different modes:
- Normal mode: The default mode when you start Vim. In this mode, keystrokes are interpreted as commands.
- Insert mode: The mode for typing text into your file.
- Command mode: Accessed by typing
:in normal mode, allows you to execute commands.
Exiting Vim
Before we start editing, let's learn how to exit Vim:
- Press the
Esckey to ensure you're in normal mode. - Type
:qand pressEnterto quit.
If you've made changes to the file and try to quit, Vim will prevent you from exiting without saving. You can:
- Type
:q!and pressEnterto quit without saving changes. - Type
:wqand pressEnterto save changes and quit. - Type
:wand pressEnterto save without quitting.
Let's practice this by opening the file again:
vim ~/project/notes.txt
Press Esc to ensure you're in normal mode, then type :q and press Enter to exit Vim.
Creating Content in Vim
Now, let's open the file again and add some content:
vim ~/project/notes.txt
To start typing text:
- Press
ito enter insert mode. You should see "-- INSERT --" at the bottom of the screen. - Type the following text:
## My Linux Notes
- Vim is a powerful text editor
- Learning Vim improves productivity
- Vim has multiple modes for different tasks
- Press
Escto return to normal mode. - Type
:wqand pressEnterto save and quit.
You can verify the content of the file with:
cat ~/project/notes.txt
You should see the text you entered displayed in the terminal.
Congratulations! You've learned the basics of opening, editing, and closing files with Vim.
Basic Navigation in Vim
In this step, you will learn how to navigate through text files in Vim. Efficient navigation is essential for editing files quickly and precisely.
Cursor Movement Keys
In Vim's normal mode, you can move the cursor using these keys:
h- Move leftj- Move downk- Move upl- Move right
These keys are known as the "home row" navigation keys because they're located under your fingers when your hands are in the standard typing position.
Let's practice navigation. First, create a new file with multiple lines:
vim ~/project/practice.txt
Press i to enter insert mode and type the following text:
Line 1: This is the first line of our practice file.
Line 2: We will use this file to practice navigation.
Line 3: Vim provides efficient ways to move around text.
Line 4: With practice, navigation becomes intuitive.
Line 5: The last line of our practice file.
Press Esc to return to normal mode.
Now practice moving around the file:
- Use
jto move down line by line - Use
kto move up line by line - Use
hto move left character by character - Use
lto move right character by character
Word Navigation
For faster navigation, Vim provides shortcuts to move by words:
w- Move to the beginning of the next worde- Move to the end of the current wordb- Move to the beginning of the previous word
Try these commands in normal mode to navigate through the text you've entered.
Line Navigation
To navigate to specific positions within a line:
0- Move to the beginning of the current line$- Move to the end of the current line^- Move to the first non-blank character of the line
File Navigation
To navigate through larger portions of the file:
gg- Move to the beginning of the fileG- Move to the end of the file:n- Move to line number n (replace n with a number, e.g.,:3to go to line 3)
Practice these navigation commands in your file. For example:
- Press
ggto go to the beginning of the file - Press
Gto go to the end of the file - Type
:3and pressEnterto go to line 3
When you've finished practicing, save the file:
- Press
Escto ensure you're in normal mode - Type
:wqand pressEnterto save and exit
Let's check the content of the practice file:
cat ~/project/practice.txt
The output should display the text you've entered in the file.
Creating a Configuration File
Now, let's apply what you've learned to create a simple configuration file. Configuration files are common in Linux systems, and knowing how to edit them is an important skill.
vim ~/project/config.ini
Enter insert mode with i and type the following content:
[Settings]
UserName=admin
Password=secure123
Port=8080
Debug=true
Use the navigation commands you've learned to move around the file and ensure everything is correct.
When you're satisfied, save the file and exit:
- Press
Escto return to normal mode - Type
:wqand pressEnter
You can verify the content with:
cat ~/project/config.ini
You should see the configuration file content displayed in the terminal.
Great job! You've now learned and practiced basic navigation in Vim, which will make your editing more efficient.
Editing Text in Vim
In this step, you will learn how to edit text effectively in Vim. We'll cover inserting, deleting, and changing text, which are operations you'll use frequently.
Inserting Text
Vim provides several ways to enter insert mode:
i- Insert text before the cursora- Append text after the cursorI- Insert text at the beginning of the lineA- Append text at the end of the lineo- Open a new line below the current lineO- Open a new line above the current line
Let's practice these commands. Create a new file:
vim ~/project/edit_practice.txt
Now try the following:
- Press
iand type "This is some text. " - Press
Escto return to normal mode - Press
aand type "I am appending after the cursor." - Press
Escto return to normal mode - Press
oto open a new line below and type "This is a new line below." - Press
Esc - Press
Oto open a new line above and type "This is a new line above." - Press
Esc
Deleting Text
In normal mode, you can delete text using these commands:
x- Delete the character under the cursordd- Delete the entire current linedw- Delete from the cursor to the end of the wordd$orD- Delete from the cursor to the end of the lined0- Delete from the cursor to the beginning of the line
Let's practice deletion. In your file, try:
- Position your cursor on a character and press
xto delete it - Position your cursor at the beginning of a line and press
ddto delete the entire line - Position your cursor at the beginning of a word and press
dwto delete the word
Changing Text
Vim also provides commands to delete text and enter insert mode in one step:
cw- Change from the cursor to the end of the wordc$orC- Change from the cursor to the end of the linecc- Change the entire line
Try changing text:
- Position your cursor at the beginning of a word
- Press
cw - Type a new word
- Press
Esc
Undoing and Redoing Changes
Made a mistake? Vim allows you to undo and redo changes:
u- Undo the last changeCtrl+r- Redo the last undone change
Try making some changes, then undo and redo them.
Copying and Pasting Text
Vim uses the terms "yank" for copy and "put" for paste:
yy- Yank (copy) the current lineyw- Yank from the cursor to the end of the wordp- Put (paste) after the cursorP- Put (paste) before the cursor
Let's practice copying and pasting:
- Position your cursor at the beginning of a line
- Press
yyto copy the entire line - Move your cursor to another position
- Press
pto paste the copied line
Creating a Practical Example
Now, let's apply what you've learned to create a simple shell script:
vim ~/project/hello.sh
Enter insert mode with i and type:
#!/bin/bash
## A simple hello world script
echo "Hello, World!"
echo "Today is $(date)"
echo "Welcome to Linux text editing with Vim"
Use the editing commands you've learned to modify the script if needed.
When you're satisfied, save the file and exit:
- Press
Escto return to normal mode - Type
:wqand pressEnter
Let's make the script executable and run it:
chmod +x ~/project/hello.sh
~/project/hello.sh
You should see the output of your shell script in the terminal, displaying the greetings and current date.
Congratulations! You now know the essential text editing commands in Vim, which will help you edit files more efficiently.
Advanced Vim Features
In this final step, you will learn some advanced features of Vim that can significantly boost your productivity. These include searching, replacing text, and working with multiple files.
Searching for Text
To search for text in Vim:
- In normal mode, press
/ - Type the search term
- Press
Enterto start the search - Press
nto find the next occurrence - Press
Nto find the previous occurrence
Let's create a file to practice searching:
vim ~/project/search_example.txt
Enter insert mode with i and type the following text:
Linux is a family of open-source Unix-like operating systems.
The Linux kernel was first released by Linus Torvalds.
Linux is widely used in servers, desktops, and embedded systems.
Linux distributions include Ubuntu, Fedora, and CentOS.
Many server environments run on Linux due to its stability.
Now practice searching:
- Press
Escto ensure you're in normal mode - Type
/Linuxand pressEnter - Press
nto move to the next occurrence of "Linux" - Press
Nto move to the previous occurrence
Find and Replace
Vim allows you to replace text using the substitute command:
- To replace the first occurrence of a pattern on the current line:
:s/old/new/ - To replace all occurrences on the current line:
:s/old/new/g - To replace all occurrences in the entire file:
:%s/old/new/g - To replace with confirmation:
:%s/old/new/gc
Let's try a substitution:
- Press
Escto ensure you're in normal mode - Type
:%s/Linux/GNU\/Linux/gand pressEnter- This will replace all occurrences of "Linux" with "GNU/Linux" throughout the file
Verify that the replacements were made by scrolling through the file.
Save and exit the file:
- Type
:wqand pressEnter
Visual Mode
Vim's visual mode allows you to select text before performing operations:
v- Start character-wise visual selectionV- Start line-wise visual selectionCtrl+v- Start block-wise visual selection (select columns)
Let's create a new file to practice visual mode:
vim ~/project/visual_example.txt
Enter insert mode with i and type:
Line 1: This is the first line for practicing visual mode.
Line 2: We can select parts of this text.
Line 3: Visual mode is very powerful.
Line 4: It allows for precise text manipulation.
Line 5: This is the last line of our example.
Press Esc to return to normal mode.
Now try these visual mode operations:
- Move your cursor to the beginning of "Line 2"
- Press
Vto start line-wise visual selection - Press
jto extend the selection to include "Line 3" - Press
dto delete the selected lines
You should see that Lines 2 and 3 have been deleted.
Working with Multiple Files
You can edit multiple files in a single Vim session:
- To open multiple files at once:
vim file1.txt file2.txt - To switch to the next buffer:
:bn - To switch to the previous buffer:
:bp - To list all buffers:
:ls - To switch to a specific buffer:
:b numberor:b name
Let's create and work with multiple files:
vim ~/project/file1.txt ~/project/file2.txt
In the first file, enter insert mode with i and type:
This is the content of file1.txt.
We're practicing working with multiple files in Vim.
Save this file without exiting Vim:
- Press
Escto ensure you're in normal mode - Type
:wand pressEnter
Now switch to the second file:
- Type
:bnand pressEnter
Enter insert mode with i and type:
This is the content of file2.txt.
Switching between files in Vim is efficient.
Save this file and exit Vim:
- Press
Escto ensure you're in normal mode - Type
:wqand pressEnter
Creating a Configuration File with Comments
Let's apply the advanced features you've learned to create a more complex configuration file:
vim ~/project/advanced_config.conf
Enter insert mode with i and type:
## Server Configuration
## Last updated: 2023-10-20
[Database]
Host=localhost
Port=5432
User=dbuser
Password=secret
Database=myapp
[Web]
Host=0.0.0.0
Port=8080
Debug=true
LogLevel=info
[Security]
EnableSSL=true
SSLCert=/etc/ssl/certs/mycert.pem
SSLKey=/etc/ssl/private/mykey.pem
AllowedIPs=127.0.0.1,192.168.1.0/24
Now practice some advanced editing:
- Search for "localhost" using
/localhost - Replace "secret" with "db_password" using
:s/secret/db_password/ - Use visual mode to select and delete the "AllowedIPs" line
- Add a new section at the end using
oto enter insert mode on a new line:
[Monitoring]
Enabled=true
Interval=60
AlertEmail=admin@example.com
Save the file and exit:
- Press
Escto ensure you're in normal mode - Type
:wqand pressEnter
Verify the content of your config file:
cat ~/project/advanced_config.conf
Excellent work! You've now learned and practiced advanced features of Vim that will help you be more productive when editing text in Linux.
Summary
In this lab, you have gained foundational skills in text editing with Vim in Linux. Starting from the basics, you progressed through increasingly advanced operations that will enhance your efficiency when working with text files in a Linux environment.
Here's a summary of what you learned:
Basic Vim Operations:
- Opening and closing files
- Understanding Vim's different modes
- Creating and saving text files
Navigation in Vim:
- Moving through text using keyboard shortcuts
- Navigating by characters, words, lines, and pages
- Jumping to specific locations in a file
Text Editing:
- Inserting and deleting text
- Copying, cutting, and pasting
- Undoing and redoing changes
- Creating and editing practical files like scripts and configurations
Advanced Features:
- Searching for text
- Find and replace operations
- Using visual mode for text selection
- Working with multiple files in a single Vim session
These skills are essential for anyone working with Linux, whether you're managing servers, developing software, or simply need to create and edit text files efficiently. Vim's power lies in its speed and versatility – once you become proficient with its commands, you can edit text faster than with most graphical editors.
While it takes time to build muscle memory for Vim commands, the investment pays off with increased productivity. Continue practicing these techniques, and consider learning more advanced Vim features as you grow more comfortable with the editor.
Remember that you can always access Vim's built-in help by typing :help followed by the command or topic you're interested in. This extensive documentation is an excellent resource as you continue to explore Vim's capabilities.



