Basic VIM operations in CentOS
There are many text editors, including gedit and kwrite in graphic mode, vi, vim (enhanced version of vi), and nano in text mode. Vi and vim are the most commonly used editors in Linux. Someone once said that there are three kinds of people in the world: Vi, Emacs, and Third.
(1) initial personalized configuration of your vim
1. What is vim?
Vim is Vi IMproved, an enhanced version of the editor Vi. IT is extremely powerful and conforms to the IT Engineer (programmer, O & M) habits. If you are a professional SE, you must be looking for an excellent editor that can be customized and flexibly edited. The answer is vim or Emacs.
2. Where is the configuration file?
The configuration file for Windows is the vimrc file under the vim installation directory.
In Linux, the configuration files for RHEL and CentOS are/etc/vimrc, And the configuration files for Debian and Ubuntu 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 is this meaningless question also listed? The reason is very simple, that is, the second question I want to ask at the beginning of my exposure to vim is this (the first is the above "1 "). Generally, some Commands to be initially executed when running a software, environment, or tool are called Run Commands, or RC. According to Wikipedia.org, the RC abbreviation is derived from MIT's Compatible Time-Sharing System (CTSS) [Reference 1]. So if you see SystemRC or ScreenRC later, you will know why.
4. Three basic modes
In "everyone learns vim", we mentioned that vim has 6 + 5 models. However, apart from the degree of subdivision, there are actually only three commonly used models: normal Mode, Insert Mode, and Command-line Mode.
From Normal Mode to Insert Mode, you can press I, I, a, A, o, O, r, R. I and I represent Insert, r and R represent replace ).
From Insert Mode to Normal Mode, you only need to press ESC.
From Normal Mode to Command-line Mode, you can press ":", "/", and "?". ":" Indicates the vim command, "/" indicates the string to be searched down, "?" Indicates that the string to be searched up is entered later.
From Command-line Mode to Normal Mode, you only need to press ESC.
You can understand Normal Mode as Normal. To enter any other Mode, you need a special method. To return to Normal Mode from other special modes, Press ESC.
5. The most common configurations in vim
When you try to google some other people's vimrc configuration, you will find an article called The ultimate vim configuration, but its author, amix later mentioned in his blog [Reference 2] that the google search vimrc will rank in the top 10 vim configuration files, and now it is outdated, so he provides some updates.
(1) color settings
Syntax on "Enable code highlighting
Syntax off "disable code highlighting
Syntax enable "enable code highlighting
(2) Search Settings
Set hlsearch "enable search highlighting
Set nohlsearch "Disable search highlighting
Set incsearch
Set ignorecase "Case Insensitive during search
(3) User Interface
Set showmode "enable Mode Display
Set ruler "indicates that the cursor position is enabled.
Set number "displays the row number
Set nonu "no row number is displayed
Set cursorline "emphasizes the row where the cursor is located
Set Partition Height = 1 "command part height is 1
(4) edit the auxiliary Configuration
Set autoindent "auto indent
Set noautoindent "not auto indent
Set smartindent "smart indent
Set autoread "when the file is changed externally, vim automatically updates and Loads
Set showmatch "show matching parentheses
Refer:
[1] Run Commands, http://en.wikipedia.org/wiki/Run_commands
[2] The Ultimate vim Configuration (vimRC), http://amix.dk/blog/post/19486
(2) frequently used status switch buttons
1. Normal Mode-> Insert Mode
I lowercase letter I, insert at the cursor position
Lowercase letter a, insert at the next position of the cursor
I capital letter I, insert at the first non-space position in the row where the cursor is located
A capital letter A, inserted at the last character of the row where the cursor is located
O lowercase letter o, insert a new line at the next line of the row where the cursor is located
O uppercase letters O, insert a new line at the top of the row where the cursor is located
R: lowercase letter r. Replace the character at the cursor once.
R capital letter R. The character at the cursor is continuously replaced until you Press ESC.
2. Normal Mode-> Command-line Mode
: W save the file
: W! Force save files (provided that you have the permission to modify files)
: Q: exit the buffer.
: Q! Force exit the buffer without saving
: Wq: Save the file and exit the buffer.
: ZZ save the file and exit
: Wq! Force save the file and exit the buffer (provided that you have the permission to modify the File Access)
: W <filename> Save As file named filename
: N1, n2 w <filename> Save the data from row n1 to row n2 as a file named filename
: X if the file is changed, save and exit. Otherwise, exit directly.
3. Insert Mode-> Normal Mode
Press ESC
4. Command-line-> Normal Mode
Press ESC
(3) commonly used cursor movement buttons
1. cursor character operations
J down
K up
H left
L right
$ Move the cursor to the End of the line. Pay attention to press Shift.
0 move cursor to the beginning of the line (Home)
^ Move the cursor to the first non-blank character (Home) at the beginning of the line. Press Shift.
2. cursor word operations
W cursor move to the first word of the last word
W cursor moves to the first word of the last word and ignores punctuation marks
E move the cursor to the end of the last word
E move the cursor to the end of the last word and ignore punctuation marks
B move the cursor to the first word of the previous word
B. move the cursor to the beginning of the word and ignore punctuation marks.
3. cursor sentence Operations
) Move the cursor to the beginning of the last sentence.
(Move the cursor to the beginning of the previous sentence.
% Used with "(" and ")" to find the matching half
4. cursor Row Operations
Move G cursor to the first non-blank character in the last line of the document
NG cursor moves to the nth line of the document, equivalent to ": n"
Move the gg cursor to the first non-blank character in the first line of the document, equivalent to "1 GB", also equivalent to ": 1 ″
<N> move the cursor down N rows
5. Optical section operations
} Move the cursor to the beginning of the next section.
{Move the cursor to the beginning of the previous segment
% Used with "(" and ")" to find the matching half
6. cursor page operations
Ctrl + f page flip (Pagedown)
Ctrl + B page up (Pageup)
Ctrl + d flip down half page
Ctrl + u flip up half page
H move the cursor to the first line of the current display page
M cursor moves to the middle line of the current display page
L move the cursor to the last line of the current display page
7. freely operate the cursor
Ctrl + o return to the top position of the cursor
(4) frequently used editing buttons
1. delete)
Dd deletes the row where the cursor is located.
Ndd deletes n rows down from the row where the cursor is located
D1G delete all rows from the row where the cursor is located to the first row
DG deletes all rows from the row where the cursor is located to the last row
D $ Delete All characters from the cursor position to the end of the line
D0 deletes all characters from the cursor position to the beginning of the line
2. copy operation (yank)
Yy copy the row where the cursor is located
Nyy copies n rows down from the row where the cursor is located
Y1G copies all rows from the row where the cursor is located to the first row.
YG copies all rows from the row where the cursor is located to the last row
Y $ copy all characters from the cursor position to the end of the line
Y0 copies all characters from the cursor position to the beginning of the line
3. paste (paste)
P: lowercase letter p. paste the data in the clipboard, starting from the next row in the row where the cursor is located.
P capital letter P, paste the data in the clipboard, starting from the last line of the row where the cursor is located
4. undo and redo operations)
U (Undo) Undo the previous operation
Ctrl + r (Redo) Redo the previous operation
5. Repeated operations
. Repeat the previous operation.
6. replace)
R replaces the character at the cursor
R enters the replacement mode until you Press ESC to exit
Cc replaces the row where the cursor is located
Cw replaces the English word of the cursor
~ Case sensitivity
7. Typographical operations
: Le <ft> align the cursor row to the left
: Ri <ght> align right of the row where the cursor is located
: Ce <nter> center of the row where the cursor is located
(5) frequently used multi-buffer operation buttons
1. Multi-file editing mode
(1) The argument list mode contains multiple file path parameters when opening the vim editor.
(2) buffer list mode: after entering the vim Editor, open multiple buffers for editing.
2. open multiple files in a single buffer zone
: Files: displays the files currently opened.
: N switch to the next Buffer
: N switch to the content of the previous Buffer
: 2n switch to the next Buffer
: Bn next buffer content (buffer next)
: Buffer previous)
3. open multiple files in multiple Buffers
: Sp [filename] open a new buffer. If filename exists, the content is the file; otherwise, it is the current file.
Ctrl + w n create a buffer
Ctrl + w q exit the buffer where the cursor is located
Ctrl + w j move cursor to next Buffer
Ctrl + w k move cursor to previous Buffer
Ctrl + w l move cursor to right Buffer
Ctrl + w h move cursor to left Buffer
Ctrl + w v left and right cutting window New Buffer
Ctrl + w s create a buffer in the Upper/lower cut window
Ctrl + w o maximize the buffer where the cursor is located, and hide other Buffers
(6) shortcut keys for common search and bookmarking operations
1. Search for strings
/String: Search down the string "string"
? String to search for the string "string"
2. Repeat the previous search
N find the next matched string based on the last Search Condition
N find the last matched string based on the last Search Condition
3. Search for words
* Search for words at the cursor position (exact match)
# Search for words at the cursor position (exact match)
G * search down the word at the cursor position (partial match)
G # search for words at the cursor position (partially match)
4. mark)
Ma a is a lowercase letter, which sets the document bookmarks a for the cursor
MA A is an uppercase letter, and A is the place where the cursor is located.
5. Use bookmarks (mark)
'A to file bookmarks a, above the Tab key
'A to the beginning of the line where file bookmarks a are located, and left of the Enter key
'A to bookmarks A, above the Tab key
'A to the beginning of the row where global booka is located, and left of the Enter key
'N' if n = 0, the buffer will open the last document, and the cursor is at the last position of the last edit, 1-9 and so on.
'N' if n = 0, the buffer will open the last document, and the cursor will start at the beginning of the row where the last edited position is located, and so on from 1 to 9.
6. view the bookmarks list
: Marks: view all current bookmarks
(7) Encryption
1. Encrypt Your text files
When you use vim to open or create a file, if you add the-x parameter, that is:
Vim-x filename
Then vim will prompt you:
Enter entryption key:
After you enter the password, vim will prompt you to repeat the input to confirm:
Enter same key again:
When you set the password, it is displayed in *, not in plaintext. When someone (including yourself) opens the file next time, vim will prompt:
Need encryption key for "main. cpp"
Enter encryption key:
In this case, you only need to enter the password you set before. If the password you entered is incorrect, vim does not prompt you for a wrong password, but displays a bunch of meaningless junk code.
2. unencrypt and reset the password
Open the encrypted file and enter:
: Set key =
In this way, your password is set to null, and you do not need to enter the password in the future. If you reset the password, you can enter:
: X
Note that X is an uppercase letter. At this time, vim will prompt you, as long as you repeatedly enter two new passwords:
Enter entryption key :*****
Enter sanme key again :*****
3. Encryption Problems
Vim's encryption function can be said to be a lot of problems. For example:
(1) If the password is incorrectly entered, vim will not reject visitors from editing files. When the file is edited and saved in case of garbled characters, the entire file becomes messy and unrecognizable. In the future, even if you use the correct password to open the file, you will not see the correct document.
(2) If the source code file to be compiled is encrypted, it cannot pass the compiler syntax check.
(3) files cannot be encrypted. What problems does this cause? The problem is that if other users find a way to read your swap file, they can bypass the decryption link and your encryption will be meaningless. The solution is not to use swap files. There are three methods. One is to modify your vimrc file (vim configuration file ):
Set noswapfile
Second, enter the command at the beginning of opening the file with vim:
Vim-x-n filename
(Note that-x is an encryption parameter. It has nothing to do with what we call swap. Do not confuse it)
Third, after entering vim to edit the file, enter:
: Setlocal noswapfile
However, the disadvantage of not switching files is that you may face the tragedy of data loss that cannot be recovered. So if you want to use encryption and do not want to lose files, you should press ": w.