Tutorial 9. Text Editing in the Linux environment
- Use vim to create, edit, and manipulate text files
- Use additional commands to extract data from text files
Vim (visual improved) is a text editing program that works directly in the terminal. Its functionality is based on working in modes. By switching between modes, you can write, edit, navigate, and save your work without taking your hands off the keyboard.
Vim is a powerful program with many options, but you can get started with a just a few modes. To create a new text file, execute
vim file.txt. This opens a new file called file.txt, which you can edit using the following modes:
|insert, allows you to edit the text
|write quit, saves changes and exits the file
|quit, exits the file without saving changes
When you are ready to exit the insert mode, type
Esc and vim will return to the normal mode that it opened in. From here, you can type the letters for other modes and then type
Enter to execute them.
Other useful modes
|insert after the cursor
|insert at the end of a line
|visual, selects text one character at a time
|visual, selects text one line at a time
|v + Ctrl
|visual, selects text as a block
|yank, copy selected text
|delete selected text
|paste text after cursor
|moves to the top of the file
|moves to the bottom of the file
Searching for a string
/string will search your text file from inside vim for the string. Type
n to move fowards in your search and
N to move backwards.
Find and replace
:%s/old_text/new_text/g will search for all instances of the string old_text and replace it with the string new_text .
Insert a word at the beginning of each line
Ctrl + V,
Ctrl + i,
These commands will enter visual block mode, select the first character of each line, enter insert mode at the cursor (the entire selected section), and insert the new text.
Try and Learn
Launch the terminal. In the command line try to execute the following tasks with what you know:
- Create 2 directories: “folder” and “folder1”
- Enter “folder”
- Create and save a txt file called “doc” with the contents “Hello world!”
- Copy this file to “folder1”
- Return back to your home directory and remove “folder”
- Rename “folder1” to “example”
- Within “example”, search for every instance of “world” in “doc”
Congratulations! You have mastered text editing with Vim.