Vim Practical basic Operation Skills

Source: Internet
Author: User
Tags save file switches

There are many text editors, such as Gedit, KWrite and so on in graphical mode, and the editor in text mode has VI, VIM (enhanced version of VI) and nano. VI and VIM are the most commonly used editors in Linux systems. Some people have said that there are three kinds of people in the world: one is to use VI, the other is to use Emacs, and the other is the third kind of person.

(i) Beginner's personalized configuration your VIM

 1. What is VIM?

Vim is VI improved, an enhanced version of Editor VI, an extremely powerful editor for it engineers (programmers, OPS) habits. If you are a professional SE, be sure to look for an excellent editor that can be customized to fit your flexible editing capabilities. The answer, then, is vim or Emacs.

  2. Where is the configuration file?

The configuration file for the Windows system is the VIMRC file in the VIM installation directory.

Configuration files for Linux systems, Rhel and CentOS are/etc/vimrc,debian and Ubuntu profiles are/USR/SHARE/VIM/VIMRC.

The configuration file for Mac OS x is/USR/SHARE/VIM/VIMRC.

  3. What does RC in VIMRC mean?

Why do you list such meaningless problems? The reason is simple, I was in touch with vim at the beginning of the second question to ask is this (the first one is the "1" above). In general, at the beginning of running a software, environment, or tool, some of the commands to be executed initially are called Run Commands, or RC. According to Wikipedia.org's record, the use of the RC abbreviation originates from the compatible time-sharing operating system of MIT (Compatible time-sharing system,ctss) [Reference 1]. So if you see SYSTEMRC or SCREENRC in the future, you'll know why.

  4. Three basic modes

In the "Everyone to learn Vim" mentioned Vim has 6+5 mode, but in addition to the degree of subdivision of different, in fact, there are only 3 modes: Normal mode, Insert mode and command-line mode.

From normal mode into insert mode, you can press I, I, A, A, O, O, R, R. Where I and I represent insert (insert), R and R represent substitution (replace).

To enter normal mode from insert mode, simply press the ESC key.

To enter command-line mode from normal mode, you can press ":", "/", "?". where ":" means the input vim command, "/" indicates the following string to search down, "?" Represents the following input to search for a string.

To enter normal mode from command-line mode, simply press the ESC key.

You can think of normal mode as normal, enter any other mode, need some special way, and go back to normal mode from other special modes, then press the ESC key.

  5. The most commonly used configuration in vim

When you try to go to Google for some other people's VIMRC configuration, you will certainly find an article called "The Ultimate Vim Configuration", but its author, Amix later mentioned on his blog [Reference 2], This vim profile, which is ranked in the top ten in Google search VIMRC, is now obsolete, so he provides some updated information.

(1) Color setting

Syntax on "Turn on code highlighting

Syntax off "off code highlighting

Syntax enable "Turn on code highlighting

(2) Search settings

Set Hlsearch "Turn on search highlighting

Set Nohlsearch "Turn off search highlighting

Set Incsearch "Enter search string while searching

Set IgnoreCase "Ignore case when searching"

(3) User interface

Set Showmode "Open mode display

Set ruler "Turn on cursor position prompt

Set number "Displays line numbers

Set Nonu "does not display line numbers

Set Cursorline "emphasizes that the cursor is in the row

Set cmdheight=1 "Command section height is 1

(4) Edit secondary configuration

Set autoindent "Auto Indent

Set noautoindent "Do not indent automatically

Set Smartindent "Smart indent

Set Autoread "When the file is externally changed, VIM automatically updates the load

Set Showmatch "Display matching parentheses

Reference:

[1] Run Commands,http://en.wikipedia.org/wiki/run_commands

[2] The Ultimate vim Configuration (VimRC), http://amix.dk/blog/post/19486

(ii) commonly used status switch buttons

  1. Insert mode, Normal mode

I small Letter I, inserted at cursor position

A small letter A, inserted at the next position of the cursor

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

A capital letter A, inserted in the last word of the line where the cursor is prompt

o Small Letter o, inserting a new row at the next line of the cursor

O Capital letter O, inserting a new flight at the top line of the row where the cursor is located

R Small Letter R, replacing the character at the cursor at once

R Capital Letter R, which continuously replaces the character at which the cursor is located until the ESC is pressed

  2, Normal mode, command-line mode

: W Save File

: w! Force the file to be saved (provided that the user has permission to modify the file access rights)

: Q Exit Buffer

: q! Force exit buffer without saving

: Wq save file and exit buffer

: ZZ saves the file and exits

: wq! Force the file to be saved and exit the buffer (provided that the user has permission to modify the file access permissions)

: w <filename> Save as FileName file

: n1,n2 w <filename> Save data n1 rows to N2 as a filename file

: x If the file has changed, exit after saving. Otherwise, exit directly.

  3. Insert mode, Normal mode

Press the ESC key

  4, command-line, Normal Mode

Press the ESC key

(c) Common cursor movement buttons

 1. Cursor character manipulation

J Down

K Up

H Left

L Right

$ cursor moves to end of line, note to press the SHIFT key

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

^ The cursor moves to the first non-whitespace character (Home) at the beginning of the line, note to press the SHIFT key

  2. Cursor Word manipulation

W cursor moves to the beginning of the last word

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

e cursor moves to the suffix of the latter word

E cursor moves to the ending of the last word and ignores punctuation

b cursor moves to the beginning of the first word

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

  3. Cursor sentence operation

The cursor moves to the beginning of the last sentence

(The cursor moves to the beginning of the previous sentence

% mates "(" and ")" used to look for matching the other half

 4, Cursor line operation

G cursor moves to the first non-whitespace character of the last line of the document

NG cursor moves to the nth line of the document, equivalent to ": N"

The GG cursor moves to the first non-whitespace character in line 1th of the document, equivalent to "1G", or ": 1″

<N> move the cursor down N rows

  5. Cursor segment operation

The cursor moves to the beginning of the next paragraph

{cursor moves to the beginning of the previous paragraph

% mates "(" and ")" used to look for matching the other half

  6. Cursor page operation

Ctrl+f PAGE Down (Pagedown)

Ctrl+b page Up (Pageup)

Ctrl+d down Half page

Ctrl+u up half a page

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

M cursor moves to the middle row of the currently displayed page

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

  7. Free cursor operation

Ctrl+o back to the previous position of the cursor

(iv) common editing operation buttons

  1. Delete operation

DD deletes the cursor in the row

NDD Delete starts at the line where the cursor is located, n rows down

D1G Delete all rows from the first line, starting at the line where the cursor is located

DG Delete all rows from the line where the cursor is located, to the last row

d$ Delete all characters from the cursor position to the end of the line

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

  2. Copy operation (Yank)

YY the copy cursor is in the row

Nyy copy starts at the line where the cursor is located, n rows down

Y1G copy starts at the line where the cursor is located, to all rows in the first row

YG copy starts at the line where the cursor is located, to all rows in the last row

y$ copy all characters from the cursor position to the end of the line

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

  3. Paste operation (paste)

P lowercase p, pastes data from the Clipboard, starting at the next line in the row where the cursor is located

P Capital Letter p, pastes data from the Clipboard, starting at the top row of the line where the cursor is located

  4. Undo and Redo Operations (Undo,redo)

U (undo) Undo Last action

Ctrl+r (Redo) Redo last action

  5. Repeat operation

. Repeat Last Action

  6. Replacement operation (replace)

R replaces the character at which the cursor is located

R Enter replacement mode until you press ESC to exit

CC replacement cursor is in the row

CW Replace the English word where the cursor is located

~ Convert case

  7. Typesetting operation

:le<ft> left aligned to the line where the cursor is located

:ri<ght> right aligned to the line where the cursor is located

:ce<nter> Center of Cursor Line

(v) Common multi-buffer operation keys

  1. Multi-file editing mode

(1) argument list mode, which is to carry multiple file path parameters when opening the Vim editor.

(2) The buffer list mode opens multiple buffers for editing after entering the Vim editor.

  2. Open multiple files in a single buffer

: Files shows which documents are currently open

: N switches to the next buffer content

: N switches to the previous buffer contents

: 2n switch to next buffer content

: Bn Next buffers content (buffer next)

: BP previous buffer content (buffers previous)

  3. Multiple buffers open multiple files

: SP [filename] opens a new buffer, if there is a filename then the content is the file, otherwise the current file

Ctrl+w n Create a new buffer

CTRL+W Q exits 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 cutting window new buffer

Ctrl+w s up and down cutting window new buffer

Ctrl+w o maximizes the buffer in which the cursor is located, and other buffers are hidden

(vi) Common Search and bookmark operation shortcut keys

  1. Searching for strings

/string Search down string "string"

? string to search up string "string"

  2. Repeat Last Search

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

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

  3. Search for words

* Search down the word (exact match) where the cursor is located

# Search up the word (exact match) where the cursor is located

g* search down the word at the cursor (partial match)

g# search up the word at the cursor (partial match)

  4. Tag bookmarks (Mark)

Ma A is a lowercase letter, setting the document bookmark a at the cursor location

MA A is uppercase, set global bookmark a at cursor location

  5. Using Bookmarks (Mark)

' A to document bookmark A, above the TAB key

' A to document bookmark A at the beginning of line, enter key to the left

' A to global bookmark A, above the TAB key

' A to global bookmark A at the beginning of line, enter key to the left

' N if n=0, the buffer will open the previous document, and the cursor is at the last position of last edit, 1-9 and so on

' N if n=0, the buffer will open the previous document, and the cursor is at the beginning of the row where the last edited position, 1-9 and so on

  6. View the Bookmark list

: Marks view all current bookmarks

(vii) on encryption

  1. Encrypt your text file

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

Vim-x filename

Then Vim will prompt you to:

Enter entryption Key:

When you enter a password, Vim prompts you to repeat the input to confirm that:

Enter Same Key again:

When you set the password, it is displayed in *, non-plaintext. The next time someone (including yourself) opens the file, Vim will prompt you:

Need encryption key for "Main.cpp"

Enter Encryption Key:

You just have to enter the password you've previously set. If the password you entered is wrong, VIM does not prompt you for the wrong password, but instead displays a bunch of meaningless garbage codes.

  2. de-encrypt and reset the password

Open the file that you have encrypted, and then enter:

: Set key=

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

: X

Note that it is an uppercase X. At this point vim will prompt you, as long as the repeated input two times the new password can be:

Enter entryption Key: * * * *

Enter Sanme Key again: * * * *

  3. Problems caused by encryption

Vim's cryptographic function can be said to be a lot of problems. Like what:

(1) If the password is entered incorrectly, VIM will not deny the visitor to edit the file. In the case of garbled display and edit the file and save, the entire file will become chaotic, unrecognizable. In the future, even if you open the file with the correct password, you will not see the correct document.

(2) If a source code file that needs to be compiled is encrypted, it cannot be checked by the compiler's syntax.

(3) The interchange file cannot be encrypted. What's causing the problem? The problem is that if other users try to read your swap file, you can bypass the decryption process and your encryption will be meaningless. The workaround is to not use the swap file. There are three methods, one is to modify your VIMRC file (vim config file):

Set Noswapfile

The second is to use VIM to open the file at the beginning of the input command:

Vim-x-n filename

(Note that-X is an encryption parameter that doesn't matter what we call swap, don't confuse it)

Third, after entering vim to edit the file, enter:

: setlocal noswapfile

But the downside of not using swap files is that you may face a tragic loss of data that cannot be recovered. So want to use encryption, and do not want to lose the files of friends, the Diligent press ": W" bar.

Vim Practical basic Operation Skills

Related Article

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.