Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic usage of Vim

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is Vim

   Vim is a highly configurable text editor that is very efficient for creating and changing any type of text. Along with most UNIX systems and Apple OS X, it is included as "vi". Vim is stable and continues to evolve and get better. Its features are: persistent multi-level undo tree; extensive plug-in system; support for hundreds of programming languages and file formats; powerful search and replacement; and integration with many tools.

   Vim is Vi IMproved, an enhanced version of the editor Vi, an extremely powerful editor that conforms to the habits of IT engineers (programmers, operations and maintenance). If you are a professional SE, you must be looking for an excellent editor that can be freely customized and can be edited flexibly. So the answer is vim or Emacs. The most important feature of vim is the complete use of keyboard commands for editing. Although it is difficult to get started without mouse operation, a variety of ingenious combinations of keyboard operations can bring great efficiency improvement.

   in Linux systems, the configuration files for RHEL and CentOS are / etc/vimrc,Debian and the configuration files for Ubuntu are / usr/share/vim/vimrc.

  , what does RC in vimRC mean?

   is generally called Run Commands, or RC, which is the initial command to execute at the beginning of running a software, environment, or tool. According to Wikipedia.org, the use of the RC acronym is derived from MIT's compatible time-sharing operating system (Compatible Time-Sharing System,CTSS) [reference 1]. If you are interested, please understand for yourself.

Second, three basic models

Basically, vi/vim can be divided into three states, namely, normal mode, insert mode and command mode. The functions of each mode are as follows:

1. General mode

Controls the movement of the screen cursor, the deletion of characters, words or lines, and the movement and copying of a section.

2. Insert mode

Only under insertion can text input and deletion be done, and press the "ESC" key to return to normal mode.

You can press I, I, a, A, o, O, r, R. Where I and I denote Insert and r and R denote replace.

3. Command mode

Save or exit vim from the file, or set up the editing environment, such as finding strings, listing line numbers, etc.

You can press:, /,? Where ":" means to enter the vim command, "/" means to enter the string to search down later, "?" Indicates that the string to be searched up is entered later.

In fact, ordinary mode can be understood as normal, entering any other mode requires some special way, and from other special mode back to normal mode, just press the "ESC" key.

The three basic modes of Vim are shown below:

Third, the most commonly used configuration of vim

To make the following parameters permanent, you can write the following parameters into the / etc/vimrc configuration file, of course, some parameters already exist in the configuration file.  

1. Color setting

Syntax on     Open Code highlight

Syntax off     turn off Code highlight

Syntax enable   Open Code highlight

2. Search settings

Set hlsearch    -turn on search highlight

Set nohlsearch   -turn off search highlight

Set incsearch   -search while entering a search string

Set ignorecase   -ignore case when searching

3. User interface

Set ruler  -turns on the cursor position prompt

Set number      -- displays the line number

Set nonu  -- Line numbers are not displayed

Set cmdheight=1   -the height of the command section is 1

4. Edit the auxiliary configuration

Set autoindent      -automatic indentation

Set noautoindent    -not indented automatically

Set smartindent     -Smart indentation

Set autoread   -vim automatically updates and loads when the file is changed externally

Set showmatch  -displays matching parentheses

Fourth, commonly used state switch buttons

1. Normal mode → insertion mode

I    lowercase I,   inserted at cursor position

A    lowercase a,   is inserted at the next position of the cursor

I    capital letter I,   is inserted at the first non-space on the line where the cursor is located

A    capital letter A,   is inserted at the last character of the line in which the cursor is located

O    lowercase o,   inserts a new line at the next line where the cursor is located

O    capital O,   inserts Singapore Airlines on the previous line of the cursor

R    lowercase letter r,   replaces the character where the cursor is located once

R    capital letter R,   continues to replace the character at the cursor until the "ESC" key is pressed

2. Normal mode → command mode

W    saves the file

W!    forces the file to be saved (only if the user has permission to modify file access)

: Q    exit buffer

Q!    forcibly exits the buffer without saving

: wq   saves the file and exits the buffer

: wq!   forces the file to be saved and exits the buffer (provided the user has permission to modify file access)

W [filename]    is saved as a file named filename

: r [filename]    reads another file into the file you are editing

: N1 filename w [filename]    saves the data from N1 to N2 lines as a file named filename

: X      save and exit

ZZ     saves and exits

ZQ     does not save exit

3. Insert mode → view mode

Press "ESC" key

4. Command mode → view mode

Press "ESC" key

Commonly used cursor movement keys

1. Cursor character operation

H    move the left Arrow (←) cursor one character to the left

L    right Arrow (→) cursor moves one character to the right

J    Down Arrow (↓) move the cursor down one character

K    up Arrow (↑) cursor move up one character

Move the $   cursor to the end of the line (End) and press the "Shift" key

0    move the cursor to the beginning of the line (Home)

Move the ^    cursor to the first non-white space character (Home) at the beginning of the line, and press the "Shift" key

2. Operation of beacons

W    cursor moves to the beginning of the latter word

W    cursor moves to the beginning of the latter word and ignores punctuation

E    cursor moves to the end of the latter word

E    cursor moves to the end of the latter word and ignores punctuation

B    move the cursor to the beginning of the previous word

B    cursor moves to the beginning of the previous word and ignores punctuation

3. Cursor line operation

G      cursor moves to the first non-white space character on the last line of the file

The nG/ngg   cursor moves to the nth line of the file, which is equivalent to ": n"

The gg     cursor moves to the first non-white space character on line 1 of the file

4. Cursor page operation

Ctrl + f    turn down (Pagedown)

Ctrl + b    page up (Pageup)

Ctrl + d    turn down half a page

Ctrl + u    turn up half a page

The "H"    cursor moves to the first line of the currently displayed page

The "M"    cursor moves to the middle line of the currently displayed page

The "L"    cursor moves to the last line of the currently displayed page

5. Operate the cursor freely

Ctrl + o    returns to the previous position of the cursor

VI. Common editing operation keys

1. Delete operation (delete)

Dd     deletes the line of the cursor

Ndd     deletes the n-line down from the line where the cursor is located

D1G    deletes all lines from the line where the cursor is located to the first line

DG     deletes all lines from the line where the cursor is located to the last line

D $    removes all characters from the cursor position to the end of the line

D0     removes all characters from the cursor position to the beginning of the line

Each time x      is pressed, the "after" character of the cursor position is deleted.

Nx      deletes the "after" n characters where the cursor is located.

X      uppercase X, each time you press, deletes the "front" character of the cursor location.

NX     deletes the "first" n characters where the cursor is located.

2. Copy operation (yank)

Yy     copies the line of the cursor

Nyy     copy starts at the line of the cursor and goes down n lines

Y1G     copies all lines from the line where the cursor is located to the first line

YG     copies all lines from the line of the cursor to the last line

Y $    copies all characters from the cursor position to the end of the line

Y0     copies all characters from the position of the cursor to the beginning of the line

J     merges the line of the cursor with the next line into one line

Yw     copies the letter of the current cursor to the last letter of the word

Yaw    copies the word where the current cursor is located

3. Paste operation (paste)

P    lowercase p, paste the data from the clipboard, starting on the next line of the cursor

P    uppercase P, paste the data from the clipboard, starting with the previous line of the cursor ~ ~

4. Undo and redo operations (undo)

U    undo the previous action

5. Repeat operation

.    repeats the previous operation

6. Replace operation (replace)

R     replaces the character at the cursor

R     enters replacement mode until you press ESC to exit

Cc     replaces the line of the cursor

Cw    replaces the English word where the cursor is located

~     converts case

: N1    n2sAccord New    g Universe from N1 to N2 replace old with new,g for all

: 1 the old old of the whole file is replaced by new

: 1       c to confirm.

    new (y/n/a/q/l/ ^ E / ^ Y)?   is represented as follows:

     y:    replaces the current content

     n:    does not replace

     a:    replaces all

     Q:     exits

     l:     replaces the current content and exits

     Ctrl + e: find backwards

     Ctrl + y: find forward

7. Typesetting operation

Left alignment of the line where the le     cursor is located

Align the line of the ri      cursor to the right

The line of the ce     cursor is centered

8. View history

: history    displays the history recently entered in vim

Commonly used search shortcuts

1. Search string

/ string    searches down for the string "string"

? string    searches up for the string "string"

2. Repeat the last search

N    finds the next matching string based on the last search criteria

N    found the last matching string based on the last search criteria

Common multi-buffer operation keys

1. Open multiple files with a single buffer

Files    shows which files are currently open

: n     switches to the next buffer content

: n     switches to the contents of the previous buffer

: 2n     switches to the next buffer content

: bn    next buffer content (buffer next)

: contents of the last buffer on bp    (buffer previous)

2. Multiple buffers open multiple files

: sp [filename]    opens a new buffer. If there is a filename, the content is the file, otherwise it is the current file.

Ctrl+w n    create a new buffer

Ctrl+w Q    exit the buffer where the cursor is located

Ctrl+w j    cursor moves to the next buffer

Ctrl+w k    cursor moves to the previous buffer

Ctrl+w l    cursor moves to the right buffer

Ctrl+w h    cursor moves to the left buffer

Ctrl+w v    left and right cut window create a new buffer

Ctrl+w s    up and down cut window create a new buffer

Ctrl+w o    maximizes the buffer where the cursor is located, while other buffers are hidden

IX. About encryption

1. Encrypted text files

When you open or create a new file with vim, add the-x parameter, that is:

# vim-x test.txt   -Open the text file you want to encrypt

Then vim will prompt you:

Enter entryption key:

When you enter your password, vim will prompt you to repeat it to confirm:

Enter same key again:

When you set a password, it is displayed in *, not in clear text. After opening the file,: wq saves and exits before the password takes effect.

The next time someone (including yourself) opens the file, vim will prompt:

Enter encryption key:

At this point, all you have to do is enter the password you set before. If you enter the wrong password, vim will not prompt you for the wrong password, but will show a bunch of meaningless junk code.

2. Unencrypt and reset password

Open the file you have encrypted and enter:

# vim test.txt    -after opening the encrypted file, enter the following

: set key=

In this way, your password will be set to empty, and you will no longer need to enter your password in the future. And if you reset the password to, you can type:

: X

Notice that it is a capital letter X. At this point, vim will prompt you to enter the new password twice:

Enter entryption key: *

Enter sanme key again: *

If it is to clear the password, just hit the keyboard twice in a row.

Note:

After encrypting, removing the password, and resetting the password, it must be saved before it can take effect.

If the password is entered incorrectly, vim does not refuse visitors to edit the file. Edit and save the file in this case of garbled code, and the whole file will become cluttered and unrecognizable. In the future, even if you open the file with the correct password, you will not see the correct document.

Finally, a summary diagram of Vim usage is attached.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report