Text editing software vim

Source: Internet
Author: User

Vim Software is a text editor, like Notepad under Windows, but much more powerful than words under Windows.
Vim is the most commonly used text editor on Linux/unix, and its role is to create, edit, and display text files.
Vim has no menu, only commands.

Vim's Way of learning: practice, practice, and let commands form a natural habit.

1. Vim mode
    • Command mode
    • Insert mode
    • Edit mode
1.1 Command mode:

In command mode, the input characters are treated as commands.

vimvim filename

command-line Input vim or vim filename this will open vim. Vim filename is used to edit file files with vim software.
The default is command mode when VIM is started.

The direct input vim starts, the version and other information appears, but this interface does not appear when you edit the file with vim filename.

If you want to go back to command mode in other modes, click the ESC key:

1.2 Insert Mode

To switch to insert mode in command mode, you can use the following three commands:

    • I switch to insert mode, where the cursor is in the word match either insert
    • A switch to insert mode, specifier insertion in the word cursor
    • o Switch to insert mode and insert a blank line underneath the cursor.

When you enter insert mode, the characters you enter are treated as normal characters, which is no different from Windows Notepad. The above Iao describes the Enter insert mode, which you can try.

1.3 Edit mode

Enter edit mode after entering in command mode : .
The Vim tool can be set in edit mode, such as adding line numbers, setting automatic alignment, setting shortcut keys, and so on.

In edit mode, enter:

set number

When you enter, VIM displays the line number and automatically returns to the command mode.

Command function
: Set Number Show line numbers (edit mode)
: Set Nonumber Cancel line number (edit mode)
2. Insert

After the following command (character) is pressed, it goes directly into insert mode, but there is a different behavior when entering.

Command function
A Insert in the word specifier the cursor
A Insert at the end of the line where the cursor is
I Insert in the word match either the cursor
I Insert at the beginning of the cursor
O Insert a new line below the cursor
O Insert a new line above the cursor

There is a difference between lowercase and uppercase. Learn vim to strengthen the practice, let these commands form habits, like the keyboard practice, the formation of conditioned reflex.

3. Positioning next
command function
H cursor moves left (with cursor key: Ieft)
l cursor right (with cursor key: right)
K cursor up (with cursor key: up)
J cursor down (same cursor key: down)
GG cursor moves to the first line
G cursor moves to the last line
NG Cursor moved to nth line
: n Cursor moved to nth row ( Edit mode)
$ cursor moves to end of line
0 cursor Move to the beginning of the line
^ Cursor moved to the beginning of the line
ctrl+f
ctrl+b previous

The difference between 0 and ^ is: ^ Moves to the first character position (not white space) at the beginning of the line, and 0 moves to the true beginning of the line (including whitespace).

4. Delete command
Command function
X Delete the character at the cursor location
Nx Delete the second n characters at the cursor, or repeat the N x command
Dd Delete the cursor in the row
Ndd Delete the N-line at the beginning of the row of the cursor, or repeat the N-times DD command
Dw Delete a word
Dg Delete cursor line to end of line
DGG Delete the line at the beginning of the cursor
D Delete the cursor at the end of the line
d$ Ditto
: Row n, line M D Delete n-m lines (edit mode)
5. Select, copy, and cut commands
Command function
V Starting at the current position of the cursor, the cursor will be selected, and then the end of the V.
V Starting at the current line of the cursor, the line that the cursor passes through is selected, and then the end of the V is pressed.
Yy The copy cursor is in the row
Nyy Copy the following n rows of the current row
y$ Copy to end of line
yw Copy a Word
Dd Cut the current row (delete above is cut line)
Ndd Cut the following n rows of the current row
P (Lower) Paste to the downstream of the current line
P (Upper) Paste to upstream of current line
6. Replace and Cancel commands
Command function
R Replace the character where the cursor is, press R, and then enter the correct character
R Replace with cursor, press ESC to end
U Undo One Step
Ctrl+r Redo
7. Search and Replace commands
Command function
/string Searches for the specified string, pressing N,n to jump to the next or previous string
:% S/old/new/gi Search for old, replace with new,g for global, I means ignore case (edit mode)
: N1,n2 S/old/new/gi Same as above, but limited to searching between these lines in N1~N2.

You can highlight a search keyword when searching

Command function
Set Hlsearch Highlight
Set Nohlsearch Remove Highlight

When replacing:

    • % represents all rows, and if there is no%, only the current line is replaced.
    • G means replace all, for example, the current row has 10 V, to replace the X, the case of no G only replaces the first match, there is a G full replacement
    • Corresponds to%: N1,n2 represents the row of the specified range, that is, the row within the N1~n2 range.
8. Save
Command function
: W Save changes
: w filename Save As
: Wq Save and exit, or you can use the shortcut key ZZ
: q! Do not save exit
: wq! Force save and exit, only the file owner and Root can
9. Importing files

In edit mode, enter:

:r /etc/os-release

Import the contents of the/etc/os-release file into the current document and into the cursor location.

10. Run the command

In edit mode, enter:

:!whereis ls

! Follow the command later. This example executes the Whereis ls command. This method executes the command without exiting the VIM state.

11. Import the results of the command into vim

In edit mode, enter:

:r !ls /

The results of LS/command execution are imported into the current vim editing document.

12. Define shortcut keys

In edit mode, enter:

:map ^P I#<ESC>

This defines the Ctrl+p shortcut key, which is divided into three parts:

    • Map
    • Shortcut keys
    • Custom commands

I is what we press I in command mode, indicating that the cursor jumps to the beginning of the line and enters insert mode, then enter #, then press ESC to return to command mode.

When executing this shortcut: equivalent to the cursor line, press I, then enter #, then press ESC back to command mode.

Note: The ^p Input method is: Ctrl + V, ctrl+p.

13. Replace in edit

For example: Enter MyMail carriage return or space, automatically replaced by [email protected]

:ab mymail [email protected]
14. Writing the configuration file

We will find that restarting Vim, the defined shortcut keys are gone, and the solution is to write to the configuration file.

Create a. vimrc file in your home directory to write some commonly used configurations to this file, such as: Defined line numbers, shortcut keys, substitutions, and so on.
This file is automatically run when Vim is started, which is equivalent to re-entering these commands in vim:

set numberset hlsearchmap ^P I#<ESC>ab mymail [email protected]

Common configurations:

" 设置C/C++方式自动对齐 set autoindent  set cindent  set smartindent" 设置tab宽度 set tabstop=4" 设置自动对齐空格数 set shiftwidth=4" 设置编码方式 set encoding=utf-8  " 设置打开文件的编码格式 set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1" 开启语法高亮syntax on" 设置取消备份,禁止临时文件生成 set nobackup  set noswapfile" 设置搜索高亮(hlsearch) set hlsearch

Text editing software vim

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.