To use Vim in the terminal, first ensure that Vim is installed on your system. If not, you can install it using your package manager. For example, on Ubuntu, use the command sudo apt-get install vim.
Once installed, follow these steps to use Vim in the terminal:
-
Open the terminal: Launch it by searching or using a shortcut (e.g., Ctrl+Alt+T on Ubuntu).
-
Launch Vim: In the terminal, type
vimand press Enter to open the default interface. To open or create a file directly, typevim filename.Example:
bashvim example.txt -
Basic operations:
- Insert mode: Press
ito enter insert mode and start typing text. - Command mode: Press
Escto exit insert mode and return to command mode, where you can execute Vim commands. - Save the file: In command mode, type
:wand press Enter to save changes. - Exit Vim: In command mode, type
:qand press Enter to exit. To force exit without saving, use:q!. - Save and exit: In command mode, type
:wqor:xand press Enter.
- Insert mode: Press
-
Use advanced features:
- Search and replace: In command mode, type
:%s/old/new/gto replace all occurrences of "old" with "new". - Multi-window editing: Use
:splitor:vsplitto divide the window. - Undo and redo: In command mode, press
uto undo the last change, and pressCtrl+rto redo.
- Search and replace: In command mode, type
-
Customize configuration: Tailor Vim's behavior by editing the
~/.vimrcfile. For instance, enable syntax highlighting and automatic indentation.
For example, to write a Python script:
- Open the terminal and type
vim script.py. - Press
ito enter insert mode and write your code. - After finishing, press
Esc, then type:wto save. - Verify the code, then type
:qto exit Vim.
This outlines the fundamental workflow and common operations for using Vim in the terminal.