When using Vi in PVE, the arrow keys turn into letters and the backspace key doesn’t work, which is very inconvenient. Here’s the solution:
Edit the following file with vi:
vi /etc/vim/vimrc.tiny
Change `set compatible` to the following: press `esc` to return, then input `:wq` to save and exit. (h j k l: correspond to moving the cursor left, down, up, and right, respectively, and `Del` deletes)
set nocompatible
set backspace=2
While using Vim, we often need to use the following commands to exit editing:
:q! Force quit without saving changes
:qa! Force quit all windows without saving changes
:wq Save changes and quit
😡 Save changes and quit
ZZ Save changes and quit
Default commands:
Command Mode Common Commands
Move the cursor:
- ^ : Quickly move to the first non-blank character of the current line
- $ : Quickly move to the end of the current line
- gg : Quickly move to the first line of the file
- number+gg number+G : Quickly move to the specified line
- G : Quickly move to the last line of the file
- ctrl + f: Page forward
- ctrl + b: Page backward
- H : Move to the top of the screen
- M : Move to the middle of the screen
- L : Move to the bottom of the screen
- m+(a~z/A~Z) : Mark the current line with a mark; jump to the mark with ‘(a~z/A~Z)
Selection commands:
- v : Enter visual mode, move the cursor to select characters
- V : Enter visual line mode, move the cursor to select lines
- ctrl + v: Enter visual block mode, move the cursor to select vertically
- (selection command)+(movement command) : Combine with movement commands to select content between the current and moved cursor; e.g., v+} selects to the end of the code block; ggVGG selects the entire file
- Esc : Exit the current visual mode, return to command mode
Undo and Redo commands:
- u : Undo the last command (undo)
- ctrl + r: Redo the last undo command (redo)
Repeat commands:
- n(command) : Repeat the command n times
Delete commands (similar to cut in Windows with CTRL+x):
- x : Delete the character at the cursor or selected characters
- d(movement command) : Delete the content corresponding to the movement command
- dd : Delete the current line; ndd deletes multiple lines
- D : Delete to the end of the line
Copy and Paste:
- y(movement command) : Copy the content corresponding to the movement command (copy)
- yy : Copy the current line; nyy copies n lines
- p : Paste
- Note: vi copy and delete commands place the text in the buffer, while system copy places it in the clipboard. If you copy text from another source, you can’t directly paste it in vi using p; instead, enter edit mode and right-click to paste.
Replace commands (perform lightweight edits without entering edit mode):
- r : Replace a single character, then return to command mode
- R : Enter replace mode, replacing characters after the cursor, press Esc to return to command mode
Indentation commands:
- >> : Increase indentation of the current line
- << : Decrease indentation of the current line
- >/< : In visual mode, increase/decrease indentation
Repeat the last command:
- . : Repeat the last executed command
Search:
- /str : Search for str; n searches next; N searches previous
- * : Search forward for the current word
- # : Search backward for the current word
Command mode common commands:
- : : Enter command mode
- w : Save
- q : Quit, if not saved, does not allow quitting
- q! : Force quit without saving
- wq : Save and quit
- x : Save and quit
Global replace (%s///g):
- :%s/(old text)/(new text)/g : Find old text and replace with new text
Replace within the visual area (omit %):
- :s/(old text)/(new text)/g : Replace old text with new text within the selected area
Replacement confirmation:
- :%s/(old text)/(new text)/gc : Confirm each replacement with: y (yes), n (no), a (all), q (quit)
Save as, create new file, browse:
- :e . : Open vi’s built-in file browser to view current directory files
- :e filename : Open specified file (file must exist, save current file first)
- :n filename : Create a new file and open it
- :w filename : Save the current file to a specified filename without switching the current file (similar to save as, for backup)
Split screen commands (split):
- :sp : Horizontally split screen
- :vsp : Vertically split screen
Switch split screen windows:
- Split screen commands are based on the shortcut ctrl+w. The following commands require ctrl+w first:
- w : Switch window (window)
- r : Swap windows (reverse)
- c : Close the current window, cannot close the last window (close)
- q : Quit the current window, if it is the last window, close vi (quit)
- o : Close other windows (other)
Edit mode common commands:
Enter edit mode in 6 ways:
- i : Insert before the current character (insert)
- I : Insert at the beginning of the line
- o : Insert a blank line below the current line
- O : Insert a blank line above the current line
- a : Insert after the current character (append)
- A : Insert at the end of the line
Use edit commands with numbers:
For example, to insert 10 asterisks (**********):
1. Enter the number 10
2. Enter edit mode
3. Type *
4. Press Esc to return to command mode and see the text repeated 10 times
Add multi-line comments:
1.^ Move to the beginning of the line
2. ctrl+v Enter visual block mode
3. j Move the cursor down to select multiple lines
4. I (capital I) Enter edit mode and insert at the beginning of the first line
5. Type the comment symbol, such as #
6. Esc Return to command mode to see the selected lines with comment symbols added
Remove multi-line comments:
- ctrl+v Enter visual block mode, select and delete the comment syntax
Show and hide line numbers:
- :set nu : Show line numbers
- :set nonu : Hide line numbers
Leave a Reply