Vi usage and Common commands, vi Use common commands

Source: Internet
Author: User

Vi usage and Common commands, vi Use common commands

Keywords: vi usage and Common commands

1. Use of the vi Editor

Some functions of the editing appliance:
1) open a file, create a file, and save the file
2) move the cursor
3) text editing
4) Copy, paste, and delete multiple rows/columns
5) search and replace

2. vi editor configuration method

Input in sequence on the terminal
1)Cd/etc/vim: Switch path
2)Cp vimrc ~ /. Vimrc: Copy vimrc to the Home Directory
3)Cd ~ : Switch to the Home Directory
4)Gedit. vimrc: Open vimrc
5) copy the following code to vimrc.

"Disable compatibility function set nocompatible" display row number set number "when editing, the backspace key is set to 2 spaces set backspace = 2" when editing, the tab key is set to 4 spaces set tabstop = 4" set automatic alignment to 4 spaces set shiftwidth = 4 "Case Insensitive set ignorecase" when searching highlighted set hlsearch

6) Save and close

3. Three Modes of vi Editor Normal Mode: move the cursor, copy, paste, and delete the edit mode: edit the text command line mode: search and replace

Note: If you do not know the mode, Press ESC to returnNormal Mode

4. Use of vi

In normal mode:
: W: Save the file
: Q: exit the file.
: Wq: Save and exit the file
: Q! : Force exit vi. This file is not saved even if it is modified.

In edit mode:
I: start inserting text before the cursor
A: insert text after the cursor
O: open a new row under the current row and start it at the beginning of the row.

Move the cursor:
In normal mode:
H (left)
J (lower)
K (upper)
L (right)

Quickly locate a row:
Ngg: move the cursor to the beginning of line n (n is a number ). For example1gg indicates that the line is first to the first line, that is, the file header.
G: Go to the end of the file

How to quickly locate a column in a row (Single Row/Column Switching ):
0 (number zero): move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
Fx: Search for the next place in the current row with the letter x

Copy and paste text
Yy: copy the current row
Nyy: copy the current row and Its n-1 rows
P: Paste

Delete
Dd: Delete the row where the cursor is located.
Ndd: Delete the current row and Its n-1 rows
X: Delete the character at which the cursor is located

Undo
U: undo

Search
/Pattern: Starting from the cursorEnd of FileSearchPattern (skip to the first line before/pattern to search for the entire file), and then press n or N
? Pattern: Starting from the cursorFile HeaderSearchPattern, and then press n or N

Note:
N repeat the previous search command in the Same Direction
N repeat the previous search command in the opposite direction

Replace
: % S/p1/p2/g: replace all p1 in the file with p2,
: % S/p1/p2/gc: confirm the replacement

S: replace substitute
G: global
C: confirm confirmation


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.