How to Use Vi/VIM Editor On Ubuntu Operating System
Vi and Vim (Vi Improved) are powerful text editors that come pre-installed on many Unix-based systems, including Ubuntu. Vim is an enhanced version of Vi, offering additional features such as syntax highlighting, better navigation, and more advanced functionality. If you’re working with Ubuntu and need to use Vi or Vim, this guide will help you get started with the basics.
1. Installing Vim on Ubuntu
Vi is usually pre-installed on most Ubuntu installations. If you want to use Vim, which has more features, you can install it using
apt
sudo apt update
sudo apt install vim -y
Once the installation is complete, you can open files using the
vim
2. Opening and Creating Files
- To open a file with Vim, use:
vim filename.txt
If the file doesn’t exist, Vim will create a new file with the specified name.
3. Vim Modes
Vim operates in different modes, and understanding them is crucial for effective editing:
- Normal Mode: This is the default mode for navigation, copying, pasting, and deleting text. Pressto return to Normal mode.
Esc
- Insert Mode: Allows you to insert or edit text. To enter Insert mode from Normal mode, press.
i
- Visual Mode: Used for selecting text. Pressto enter Visual mode.
v
- Command-Line Mode: Allows you to execute commands like saving, quitting, and searching. Pressto enter Command-line mode.
:
4. Basic Commands in Vim
Here are some essential commands to get started:
4.1. Navigating in Normal Mode
- h: Move left
- j: Move down
- k: Move up
- l: Move right
- w: Move to the beginning of the next word
- b: Move to the beginning of the previous word
- gg: Move to the beginning of the file
- G: Move to the end of the file
- Ctrl + f: Move one page down
- Ctrl + b: Move one page up
4.2. Inserting Text
- i: Enter Insert mode before the cursor.
- a: Enter Insert mode after the cursor.
- o: Insert a new line below the current line and enter Insert mode.
- O: Insert a new line above the current line and enter Insert mode.
4.3. Saving and Exiting
- : Save the current file without exiting.
- : Quit Vim (only if no changes have been made).
- !: Quit without saving changes.
- or: Save and quit Vim.
- ZZ: Save the file and exit Vim (in Normal mode).
4.4. Editing Text
- x: Delete the character under the cursor.
- dd: Delete the current line.
- yy: Copy (yank) the current line.
- p: Paste the copied or deleted text after the cursor.
- u: Undo the last change.
- Ctrl + r: Redo the last undone change.
4.5. Visual Mode
- Pressin Normal mode to enter Visual mode, then use arrow keys or
v
,h
,j
,k
to select text.l
- After selecting text, you can useto delete or
d
to copy the selected text.y
4.6. Search and Replace
- /pattern: Search forward for a pattern. Pressto find the next occurrence.
n
- ?pattern: Search backward for a pattern.
- :%s/old/new/g: Replace all occurrences of “old” with “new” in the file.
- :%s/old/new/gc: Replace all occurrences with confirmation for each.
5. Working with Multiple Files
Vim allows you to work with multiple files in the same session:
- filename: Open a new file in the same Vim session.
- : Go to the next file.
- : Go to the previous file.
- filename: Split the window and open a new file.
6. Customizing Vim
You can customize Vim’s behavior by editing the
.vimrc
vim ~/.vimrc
Here are a few common configurations you can add:
- Enable line numbers:
set number
- Enable syntax highlighting:
syntax on
- Set auto-indentation:
set autoindent
- Enable mouse support:
set mouse=a
After adding these lines, save the
.vimrc
7. Advanced Tips
- Multiple Undos: Vim supports multiple undos. Useto undo and
u
to redo.Ctrl + r
- Jumping to Line: Useto jump to a specific line. For example,
:number
takes you to line 10.:10
- Using Buffers: Vim uses buffers to manage files. You can switch between buffers usingand
:bnext
.:bprev
- Use Vim Help: Vim has built-in documentation. Typein Command-line mode to access it.
:help
Conclusion
Vim is a versatile and efficient editor that can greatly enhance your productivity once you learn its commands and features. With this guide, you should have a good understanding of how to use Vim for basic editing tasks on Ubuntu. The key to mastering Vim is practice, so don’t hesitate to explore its functionality and become comfortable with its commands. Happy editing!