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
Enter
to start the search
- Press
n
to find the next occurrence
- Press
N
to 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
Esc
to ensure you're in normal mode
- Type
/Linux
and press Enter
- Press
n
to move to the next occurrence of "Linux"
- Press
N
to 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
Esc
to ensure you're in normal mode
- Type
:%s/Linux/GNU\/Linux/g
and press Enter
- 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
:wq
and press Enter
Visual Mode
Vim's visual mode allows you to select text before performing operations:
v
- Start character-wise visual selection
V
- Start line-wise visual selection
Ctrl+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
V
to start line-wise visual selection
- Press
j
to extend the selection to include "Line 3"
- Press
d
to 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 number
or :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
Esc
to ensure you're in normal mode
- Type
:w
and press Enter
Now switch to the second file:
- Type
:bn
and press Enter
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
Esc
to ensure you're in normal mode
- Type
:wq
and press Enter
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
o
to enter insert mode on a new line:
[Monitoring]
Enabled=true
Interval=60
AlertEmail=admin@example.com
Save the file and exit:
- Press
Esc
to ensure you're in normal mode
- Type
:wq
and press Enter
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.