Linux Vi Commands: Complete Guide
Vi is a text editor widely used in Linux systems. It utilizes a command-line interface that can be directly accessed within the terminal. Vi offers a variety of commands and functions, and below is a detailed explanation of Vi commands.
- Access the Vi editor:
- Open a file: vi filename.
- Create a new file: vi newfilename
- Switch mode:
- Command mode is the default mode in Vi after entering, where you can save and exit files. Pressing the Esc key allows you to enter command mode.
- Insert mode: Press the keys i, a, or o in command mode to enter insert mode, where you can insert, delete, or modify text.
- Ex mode, accessed by pressing the colon key in command mode, allows for executing complex commands and operations.
- Cursor moving:
- Move one character to the left.
- j: Move down one line.
- k: move up one line.
- Move one character to the right.
- Move to the beginning of the line.
- $:Move to the end of the line.
- gg:Go to the beginning of the file
- G: Scroll to the end of the file.
- Press Ctrl + F to scroll down.
- Ctrl + B: Scroll up page.
- Editing operation:
- i: Inserting text in front of the cursor.
- Insert text after the cursor.
- o: Insert a new line below the current line and enter insert mode.
- Insert a new line above the current line and enter insert mode.
- x: Delete the current character.
- dd: delete the current line
- yy: Duplicate the current line.
- p: Content copied and pasted
- Search and replace:
- Keyword: Search for keyword downwards.
- Key term: search key term upwards
- n: navigate to the next matching item
- N: locate the previous matching item
- Replace the first occurrence of the old content with the new content on the current line.
- Replace all matching items in the current line with the new content.
- Replace all occurrences of “old content” with “new content” in the entire file.
- Save and exit
- :w Save the file without exiting Vi.
- :q: Quit Vim
- :wq: Save the file and exit Vi.
- Do not save the file and exit Vi forcefully.
These are just a few of the Vi commands; Vi also has many other advanced features and commands. Hopefully these basic commands will help you get started with using Vi editor.