Vim Program Editor

Source: Internet
Author: User
Tags bbcode dashed line

1. Vim is the advanced version of VI , vim can not only display text content in different colors, but also can do such as shell script, C program and other programs editing functions, you can vim as a program editor.

2. The icon and code in the ASCII system can refer to the Http://zh.wikipedia.org/wiki/ASCII

3. All Unix like systems will be built into the vi Text Editor, other text editors will not necessarily exist; many of the software's editing interfaces actively call VI (e.g. crontab, visudo , Edquota , etc.);vim has the ability to edit the program, it can actively identify the correctness of the grammar with the font color, convenient program design, because the program is simple,vim editing speed is quite fast.

4. vim (http://www.vim.org) will automatically invoke the program's parser based on the file's extension or the opening information in the file, and then display the program code and general information in color. Even some of the syntax within the Linux basic configuration file can be checked with vim .

5. Basically, vi is divided into three modes, namely "General mode", "edit mode" and "command-column order mode":

1) General mode: Open a file with VI directly into the general mode (this is the default mode). In this mode, you can use the "up and down" button to move the cursor, you can use "delete character" or "Delete whole line" to process the contents of the archive, you can also use "copy, paste" to process your file data.

2) Edit mode: You can delete, copy, paste and so on in the general mode, but can't edit the contents of the file. Wait until you press "I, I, O, O, A, a, R, R," and any one of the letters before entering edit mode. If you want to go back to normal mode, you must press ESC to exit edit mode.

3) Instruction Column Command mode: In general mode, enter ":/?" "Any one of the three buttons, you can move the cursor to the bottom row." In this mode, you can provide "search data" action, and read, save, a large number of substitution characters, leave VI , display line number and so on is in this mode of action.



6. Enter " vi " to enter the general mode of VI :



The dashed line in the picture does not exist, just for illustration. The upper part shows the actual contents of the file, and the bottom row is the Status display column (the [new file] In the diagram indicates that the file is new, and if the file already exists, it shows how many lines and characters, such as "141L, 4617C"), or the command release column.

7. In general mode, you can enter edit mode by pressing i, o, a , and so on:




In edit mode, the bottom left corner of the status bar appears -INSERT- The word, that is, you can enter any character hint. At this point, the keyboard, in addition to [ESC] This button, other keys can be used as a general input button. Press the [ESC] button to return to normal mode.

8. Press the [:] key in the general mode and the cursor will move to the bottom line:



Press Wq to store and leave VI. If your file permissions are incorrect, such as -r--r--r-- , you may not be able to write, you can use the: wq! "Add an exclamation point" force write ". But that's the case when you can change your permissions.

9. Key instructions available for general mode:

Keys Function
How to move the cursor
h or left arrow (←) The cursor moves one character to the left
J or down key (↓) Move the cursor down one character
K or up key (↑) Move the cursor up one character
L or right-click (→) The cursor moves one character to the right

If you want to move multiple times, such as moving down 30 rows, you can use the combination of "30j" or "30↓" keys,

That is, by adding the number of times you want to do it, press the action.

[Ctrl] + [f] Screen "Down" to move one page, equivalent to [PAGE DOWN] key (common)
[Ctrl] + [b] Screen "Up" to move one page, equivalent to [PAGE UP] key (common)
[Ctrl] + [d] Screen "Down" to move half a page
[Ctrl] + [u] Screen "Up" move half page
+ The cursor moves to the first non-whitespace of the next line
- The cursor moves to the first non-whitespace of the next line
N<space>

N means "number", press the number and then press SPACEBAR, the cursor will move to the right

n Characters of a row.

0 or function key [Home] Move to the front of the line prompt (common)
$ or Function key [End] Move to the last face of this line prompt (common)
H The first character of the line that the cursor moves to the top of the screen
M The first character of the line that the cursor moves to the center of the screen
L The first character of the line that the cursor moves to the bottom of the screen
G Move to the last line of this file (common)
NG N is a number that moves to the nth row of this file. (Can fit: set Nu)
Gg Move to the first line of this file, equivalent to 1G. Common
N<enter> N is a number, the cursor moves down n rows (common)
Search and replace
/word Look under the cursor for a string that contains the word. Common
? word Look for a string above the cursor that contains the word.
N This n is the English key, which means "repeat the previous search action" and/or "related".
N

This n is the English key, and opposite to N, the previous search action for "reverse".

: n1,n2s/word1/word2/g

N1 and N2 are numbers, looking for word1 between the N1 and N2 lines,

and replace the string with Word2 (common)

: 1, $s/word1/word2/g

Look for the word1 string from the first line to the last line and replace the string with Word2.

Common

: 1, $s/word1/word2/gc

Look for the word1 string from the first line to the last line and replace the string with Word2.

and display the prompt character before the replacement to confirm the user. Common

Delete, copy, and paste
X, X In a line of words, X is the backward deletion of a character (equivalent to the [Del] key), X is to delete a character forward (equivalent to [backspace] is the backspace bar) (common)
Nx N is a number that continuously deletes n characters backwards.
Dd Delete the entire row where the cursor is located (common)
Ndd N is the number that deletes the downward n-row of the cursor. Common
d1g Remove all data from the first row of the cursor
Dg Delete all data from the last row of the cursor
d$ Delete the last character of the row where the cursor is located
D0 Delete the first character of the row where the cursor is located
Yy The row where the cursor is copied (common)
Nyy n is the number, where the cursor is copied down n rows. Common
y1g Copy all data from the row to the first row of the cursor
YG Copy all data from the row to the last row of the cursor
Y0 Copy all data from the same character as the cursor to the beginning of the line
y$ Copy all data at the end of the line with the same character as the cursor
P, p P Pastes the copied data on the next line of the cursor, and p pastes a row on the cursor. Common
J synthesizes a row of data from the row of the cursor with the next row (remove line breaks at the end of the line)
C Delete multiple data repeatedly, such as delete 10 behavior down [10CJ], remove 5 characters to the right [5ch]
U Undo the previous action. Common
[Ctrl]+r Redo the last undone action. Common
. This is the decimal point, which is the meaning of repeating the previous action.

10. General mode switch to edit mode of the available key description:

Enter edit mode for insert or replace
I,i

Enter insert mode (insert modes):

I is "inserting from the current cursor," and I is "inserting at the first non-whitespace space at the current line." Common

A, a

Enter insert mode (insert modes):

A is "insert from the next character where the cursor is currently located" and A is "open from the last character of the line where the cursor is located

Start inserting ". Common

O, O

Enter insert mode (insert modes):

o Insert a new line at the next line where the cursor is currently located, o insert a new

Line! Common

R, R

Enter replacement mode (replace modes):

R replaces only the character of the cursor at a time, and R replaces the text of the cursor until ESC is pressed.

Common

[ESC] Exit edit mode, return to normal mode (common)

11. General mode switch to instruction column mode the available key instructions:

instructions for storing, leaving, etc.
: w writing edited data to a hard disk archive (common)
: w! If the file property is read-only, the archive is forced to be written.
: q leave VI (Common)
: q! If you have modified the file, do not want to store, use! Do not store files for forced departures.
: Wq leave after storage, if: wq! leave after forced storage (common)
ZZ If the file has not changed, do not store away, If the file has been changed, store and leave
: e! revert to the original state of the archive
: w [filename] saves the edited data as another file (similar to a new file)
: R [FileName]

In the edited data, read the data from another file. Also will be "filename" This file content

is added to the row after the cursor

: n1,n2 w [filename] Store the contents of N1 rows to n2 into Filena Me this file.
:! Command temporarily leave   VI   to the Linux command line mode to execute command display results.
: Set nu displays line numbers and, when set, displays line numbers for that row at the beginning of each line
: Set Nonu with Set Nu Instead, to cancel the line number

12. When we press [Ctrl]-z 's Combo button in the general mode of vim , your vim will be thrown into the background to execute.

13. When we use the vim editor, vim creates a file named . FILENAME.SWP in the same directory as the edited file. For example, changes you make to Man.config will be recorded in the . MAN.CONFIG.SWP . If your system is disconnected for some reason and the file you are editing is not stored yet, when you open it again with vim , because of the relationship of the staging file, vim will proactively determine that your file may have some problems, The following interface will appear:



In the illustration above, vim hints at two key problems and solutions:

Question one: There may be other people or programs editing this file at the same time. The workaround is to:

A) Find the other program or person, ask him to finish the vim work, and then you can continue to deal with it.

b) If you just want to see the contents of the file without any changes, you can choose to open the file as read only (O).

Problem two: In the previous vim environment, there may be some unknown reason for vim interruption (crashed), the processing method is:

A) If your previous vim process has not been stored, you should press "R", i.e. use the (r) Ecover project, at which time vim will load the . MAN.CONFIG.SWP content and let yourself decide whether to store it or not. But that . MAN.CONFIG.SWP is not automatically deleted after you end vim , so you will have to delete it yourself after you leave vim . MAN.CONFIG.SWP To avoid this warning message every time you open this file.

b) If you are sure that the staging file is useless, then you can simply press "D" to delete the temporary file. At this point vim will load the man.config , and after the old . Man.config.swp is deleted, the new . MAN.CONFIG.SWPwill be created this time.

(E) The DIT anyway option will open the file you want to edit in a normal way, and will not load the contents of the staging file. However, it is easy to get two users to change each other's files and other issues.

14. When we press v or v or [Ctrl]+v ] In general mode, the place where the cursor is moved will begin to be anti-white, and the three keys are:

Keys Function
V Character selection, which will place the cursor over the white selection
V Row selection, the line that the cursor passes through is reversed to the white selection
[Ctrl]+v Block selection, you can select the data in a rectangular way
Y Copy the anti-white place.
D Remove the anti-white place

15. You can use vim to simultaneously access several files at the same time to open them, the relevant keys are:

Keys Function
: N Edit Next file
: N Edit Previous file
: Files List all the files that are currently being opened by vim

Use " : Files " in vim to look at the edited file as follows:

16. Enter ": SP {filename}" in the instruction column mode to open an additional window. That filename is optional, if you want to open another file in a new window, add a file name, otherwise only enter : SP , the same file appears in two windows. You can use "[ctrl]+w+↑" and "[ctrl]+w+↓") to switch between windows. The key functions of the multi-window case are as follows:

Keys Function
: SP [filename]

Opens a new window, if there is a filename, indicates that a new file is opened in a new window,

Otherwise two windows are the same archive content (synchronized display).

[Ctrl]+w+ J

[Ctrl]+w+↓

The key method is: Press [Ctrl] not put, and then press the W release all the keys,

Then press the J (or down arrow), and the cursor moves to the window below.

[ctrl]+w+ K

[Ctrl]+w+↑

The key method is: Press [Ctrl] not put, and then press the W release all the keys,

Then press the K (or up ARROW key), and the cursor moves to the top window.

[ctrl]+w+ Q

End leaving the current window

Vim will take the initiative to record the behavior you have done so that you can work easily next time. The file that recorded the action is:~/.viminfo. If we search for a string inside a file, the string is reversed, and the next time we edit the file, the search string is reversed. Even when editing other files, if the string is present, it will be actively reversed. Also, when we repeatedly edit the same file, when we enter the file for the second time, the cursor is on the last line that we left.

Vim has a number of environment settings parameters, you can enter " : Set All" in the general mode to check. Here are some simple settings that you usually use more often:

Keys Function

: Set Nu

: Set Nonu

Set and cancel line numbers

: Set Hlsearch

: Set Nohlsearch

Hlsearch is the high-light search.

This is the setting whether the searched string is reversed. The default value is Hlsearch

: Set Autoindent

: Set Noautoindent

is automatically indented (that is, when you press Enter to edit a new line,

The cursor is not at the beginning of the line, but is aligned with the first non-whitespace space on the previous row

: Set Backup

Whether to automatically store backup files. Generally nobackup, if you set up backup,

Then when you change any of the files, the source file will be saved in a different filename

The file for filename~ .

: Set Ruler

Whether to display the cursor position and other accommodation in the lower right corner of the status bar.

: Set Showmode

Do you want to show the status bar in the lower left corner for words like --insert-- ?

: Set backspace= (012)

When BACKSPACE is 2 o'clock, it is possible to delete any value; 0 or 1 o'clock,

You can delete only the characters you have just entered, and you cannot delete text that already exists.

: Set All

Displays all currently set values for the environment parameter.

: Set

Displays the setting parameters that are different from the system default values.

: Syntax on

: Syntax off

Whether different colors are displayed according to program-related syntax.

: Set Bg=dark

: Set Bg=light

To display different color tones, the preset is "light". If you think the annotation font is dark blue

is not easy to see, then this can be set to dark.

: Set laststatus=2

There are two lines of status bar (one line + row of instruction columns)

The global vim setting value is usually placed in /ETC/VIMRC This file, you can modify ~/.VIMRC This file (default does not exist), will you want to write the set value. The double quotation mark (") In this file is a comment that does not require a colon at the beginning of the write setting value.

. vim Common directives:



20. cat-a can be used to observe the special format of files created by DOS (Windows system), the newline character used by DOS is ^m$ , we call it CR and LF Two symbols. Under Linux, there is only the LF ($) newline symbol. At the start of the command under Linux, his judgment was based on "enter", while Linux's enter is the LF symbol, because the DOS newline symbol is CRLF , that is, one more ^m

Bash code
    1. # Dos2unix [-kn] file [NewFile]
    2. # Unix2dos [-kn] file [NewFile]

The- k option preserves the original mtime time of the archive (the time when the file was last modified); the-n option preserves the original old file and outputs the converted content to the new file.

21. Use the iconv command to convert the encoding of the file:

Java code
    1. # Iconv--list
    2. # Iconv-f originally coded-t new encoded filename [-o newfile]

The options are described below:

Options Function
--list

List the language data supported by Iconv

-F

From, followed by the original encoding format

-T

To, that is, the new code in the back, what format

-O File

Keep the original file, using the-o new file name, you can create a new encoding file.

If you want to convert traditional Chinese utf8 to UTF8 encoding in Simplified Chinese, you can do this:

Bash code
    1. # iconv-f utf8-t Big5 Vi.utf8 | \ > Iconv-f big5-t gb2312 | Iconv-f gb2312-t Utf8-o Vi.gb.utf8

22. Common text Editor Comparison: Http://encyclopedia.thefreedictionary.com/List+of+text+editors

Wikipedia's text editor comparison: Http://en.wikipedia.org/wiki/Comparison_of_text_editors

About VIM what is the "Chinese" description: Http://www.vim.org/6k/features.zh.txt

Everyone to learn vim (http://info.sayya.org/~edt1023/vim/)

Linux Fedora Tips Note: How to convert traditional/Simplified Chinese: http://blog.xuite.net/michaelr/linux/15650102

Vim Program Editor

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.