Vim - Getting familiar
Vim - Getting Familiar
Section titled “Vim - Getting Familiar”This chapter introduces the basics of Vim, including how to start it, understand its modes, and perform fundamental file operations. Let’s dive in and get your hands dirty with Vim.
- Starting Vim (Terminal and GUI)
- Understanding Vim’s Modes
- Creating a new file
- Viewing a file in read-only mode
- Editing an existing file
Start Vim
Section titled “Start Vim”Vim can be launched from a terminal or as a separate GUI application (like GVim or MacVim).
Using a Graphical Launcher (GVim/MacVim)
Section titled “Using a Graphical Launcher (GVim/MacVim)”If you have a GUI version of Vim installed (e.g., GVim on Linux/Windows, MacVim on macOS), you can usually start it by clicking its application icon. This will open a new Vim window.
A GVim window typically looks like a standard text editor but operates with Vim’s modal editing paradigm.
Using the Terminal
Section titled “Using the Terminal”The most common way to use Vim is from the terminal. This works consistently across Linux, macOS, and Windows (e.g., in Command Prompt, PowerShell, or Windows Terminal).
- Open your terminal application.
- Type
vimand press Enter:
$ vim- This will start Vim, often displaying a welcome screen.
- To quit Vim, ensure you are in Normal mode (press
Esca few times if unsure), then type:qand press Enter. If you have unsaved changes, Vim will warn you. Use:q!to quit without saving.
The command :q will appear at the bottom-left of the terminal as you type it.
Vim Modes
Section titled “Vim Modes”Vim’s power comes from its modal nature. Different modes are used for different tasks. Here are the most important ones:
Normal Mode (Command Mode)
Section titled “Normal Mode (Command Mode)”This is the default mode when Vim starts. In Normal mode, keys on the keyboard act as commands for navigation, deletion, copying, pasting, etc. You don’t type text directly here. Press Esc from any other mode to return to Normal mode.
Tip: If you’re ever unsure what mode you’re in, press Esc. It will either take you to Normal mode or do nothing if you’re already there.
Insert Mode
Section titled “Insert Mode”This mode is for typing and editing text, similar to most other editors. To enter Insert mode from Normal mode, common commands include:
i(insert before cursor)a(append after cursor)I(insert at the beginning of the line)A(append at the end of the line)o(open a new line below and enter insert mode)O(open a new line above and enter insert mode)
When in Insert mode, Vim usually displays -- INSERT -- at the bottom-left. Press Esc to return to Normal mode.
Command-Line Mode (Ex Mode)
Section titled “Command-Line Mode (Ex Mode)”This mode is used for entering more complex commands, such as saving files (:w), quitting (:q), searching and replacing (:%s/old/new/g), or setting options (:set number).
To enter Command-Line mode from Normal mode, type : (colon). The cursor will move to the bottom of the screen, where you can type the command. Press Enter to execute it. Press Esc to cancel and return to Normal mode.
You can also enter this mode from Insert mode by first pressing Esc (to go to Normal mode) and then :.
Visual Mode
Section titled “Visual Mode”Visual mode is used to select text. Once text is selected, you can perform operations on it (e.g., delete, copy, change case).
v(character-wise visual mode): Start selecting character by character.V(linewise visual mode): Start selecting whole lines.Ctrl-v(blockwise visual mode): Start selecting a rectangular block of text.
Enter Visual mode from Normal mode by pressing one of these keys. Move the cursor to expand the selection. Vim usually displays -- VISUAL --, -- VISUAL LINE --, or -- VISUAL BLOCK -- at the bottom. Press Esc to exit Visual mode.
Create and Save a New File
Section titled “Create and Save a New File”Follow these steps to create a new file, add text, and save it:
- Open Vim with a filename (even if it doesn’t exist yet):
$ vim mydocument.txt- Alternatively, start Vim (
$ vim) and then use the edit command: :edit mydocument.txt(or:e mydocument.txt)- Vim is now in Normal mode, editing
mydocument.txt(which is currently empty). - Press
ito switch to Insert mode. - Type some text, for example:
Hello, Vim world! - Press
Escto return to Normal mode. - To save the file, type
:w(for ‘write’) and press Enter. The filemydocument.txtis now saved to disk. - To quit Vim, type
:qand press Enter.
Open File in Read-Only Mode
Section titled “Open File in Read-Only Mode”To open a file for viewing without the risk of accidentally modifying it, use the -R option:
$ vim -R mydocument.txtAlternatively, you can use the view command, which is equivalent:
$ view mydocument.txtWhen a file is opened in read-only mode, Vim will usually indicate this (e.g., [readonly] in the status line) and prevent writes unless you force them (e.g., with :w!).
Edit an Existing File
Section titled “Edit an Existing File”To edit an existing file:
- Open the file with Vim:
$ vim mydocument.txt- Navigate to where you want to make changes (using
h,j,k,lor arrow keys in Normal mode). - Switch to Insert mode (e.g., press
i) and make your edits. - Press
Escto return to Normal mode. - To save changes and quit: type
:wqand press Enter. (Alternatively,:xdoes the same if changes were made). - To quit without saving changes: type
:q!and press Enter.