Learn Linux programs first to learn VIM

Source: Internet
Author: User
To learn Linux programs, first learn VIM-general Linux technology-Linux programming and kernel information. The following is a detailed description. Today, I have experienced the power of Linux LiveCD and VI. I feel that the Linux Command Line is really powerful, and the performance of VI under the command line is truly surprising, therefore, you have downloaded the windows version of Gvim for trial use. I feel that the configuration of Linux programs is really troublesome, but it is a headache. Fortunately, he finally provided an editing tool with a dark background color, and the syntax coloring is really good.


Vim Tips (post)

(1) Why can't other people's methods work for me?

(2) how to find the last edited file?

(3) how to copy and paste it?

(4) how to replace multiple files?

(5) how to open all fold once?

(6) how to select, copy, or find the word where the cursor is located?

(7) how to count the searched strings?

(8) how to remove the highlighted search?

(9) How can I solve the problem of garbled chinese menus in gvim in linux?

(10) How to highlight the edited File Syntax?

(11) How to quickly insert multiple rows of numbers 0-99?

(12) how to set Chinese encoding?

(13) how to remove or insert characters such as ^ M?

(14) How to indent a piece of code (insert a tab) as a whole )?

(15) how to merge two rows?

(16) how to set the indentation distance (tab width )?

(17) How to Prevent VIM from generating backup files (~ File )?

(18) How do I set the gvim font in windows?

(19) how to set the colorscheme of Vim

(1) Why can't other people's methods work for me?

It is estimated that this is the most difficult question to answer. :-)

First, check whether your VIM version is new enough and whether the functions are complete. Use the: version command to view your VIM version and functions. In a common situation, if you are using the RedHat system, make sure that the rpm package of vim-enhance is installed. Otherwise, it will be incomplete VIM.

(2) how to find the last edited file?



Ctrl + O (press twice)

(3) how to copy and paste it?

Copy

Method 1: First ma marks the current position as a, moves to the new position, and then y 'a or y' a copies the content from the marked position a to the current position. The difference between 'and': 'is a character, and' is a behavior unit. This method should be applicable to all vi

Method 2: Press v, V, or Ctrl + V to enter the visual mode, move the cursor to select a piece of content, and press y. Differences between the three: v is the unit of characters, V is the unit of behavior, Ctrl + V is the block selection. This method is only applicable to VIM. Paste p or P. Difference: the former is pasted after the cursor, and the latter is pasted before the cursor.



(4) how to replace multiple files?

Generally, sed or vim can be used. For example, to replace the *. c file in the current directory:

Method 1:

For I in *. c; do

Sed-I-e's/oldvalue/newvalue/G' $ I

Done

Method 2:

Vim *. c

: Argdo % s/oldvalue/newvalue/g



(5) how to open all fold once?

ZR or: set foldlevel = 999



(6) how to select, copy, or find the word where the cursor is located?

In VIM, iw or aw is used to represent a word. The two are slightly different.

Select the word where the cursor is located: viw (v enters visual mode, then iw)

Copy the word of the cursor: yiw

There are two common commands: # And *



(7) how to count the searched strings?

To count the occurrence frequency of a pattern in the buffer, set 'report' to 0 and replace the pattern with its own. The number of replicas reported by Vim is the number of occurrences of the mode. Example:

: Set report = 0

: % S/./&/g characters

: % S/\ I \ +/&/g words

: % S/^ lines

: % S/the/&/g "the" anywhere

: % S/\/&/g "the" as a word

You may want to reset 'hlsearch' or use ": nohlsearch ".



(8) how to remove the highlighted search?

Method 1: nohlsearch

Method 2:/awertgvcxz (find a string that certainly does not exist)

After the highlighted search (: set hlsearch) is set, you can permanently disable the highlighted search by using: set nohlsearch, or temporarily disable the highlighted search by using: nohlsearch. The highlighted search will be automatically restored during the next search.



(9) In linux, how does one solve the problem of garbled chinese menus in gvim?

Take vim6.3 as an Example

: Source $ VIMRUNTIME/delmenu. vim

: Source $ VIMRUNTIME/menu. vim



You can also add the preceding content to. vimrc.

A single menu. vim or nothing is garbled.

Note: The source here refers to the vim source command, which is irrelevant to the source of the bash shell.



(10) How to highlight the edited File Syntax?

It should be explained that there are many variants of VI, which do not provide such a function.

It is often its derivative versions, such as vim. The following describes how to highlight the syntax in vim.



First, determine the operating system you are using. If it is windows, the default configuration file

You can work now. If not, you can click the syntax menu and select the one that suits you.

File type (the new gvim version disables the file type by default. You need to open it and select the type according to the start letter)

If it is linux, check whether it is redhat. For redhat, install

Vim-enhanced rpm package. After doing this, you can use syntax on to enable syntax highlighting.

If necessary, use: set filetype =... to set your file type.

If you use a graphical interface, such as gvim, you can perform syntax on and set filetype operations.

Select from the menu with the mouse, the same as in windows.

If you are using vim in the console, you should also make sure that the supported color is set on your terminal.



(11) How to quickly insert multiple rows of numbers 0-99?

Enter the following code in the command line, or save the code to an external file, and then the source file:

Let I = 0

While I <1, 100

Put = I

Let I = I + 1

Endw

If the number of digits is not enough, you can use Ctrl-V to select and insert 0 to the I.



(12) how to set Chinese encoding?

Make the following settings in. vimrc to enable gb2312 for the UI and display font, and automatically convert the utf8 file:

Set encoding = prc

Set fileencoding = prc

Set fileencodings = utf8, prc

Note that, according to the above settings, if you open an empty (0 bytes) or a pure 7bits file, VIM will regard it as a UTF-8 encoded file, if you enter Chinese characters in the disk, it will also be utf8. Therefore, you may need to change the encoding back before saving the disk: set fileencoding = prc. In addition, for files of pure 7bits, because utf8 is recognized, conversion is required every time you open the storage disk, which is slow. If you do not have access to utf8 for a long time, it may be better to comment out a line of fileencodings temporarily.

[Warning]: conversion may cause information loss! If you need to edit an important system file, you 'd better comment out the fileencodings line temporarily.



(13) how to remove or insert characters such as ^ M?

Use special characters such as ^ M in VIM insert mode or command line, such

: S // g

Actual display is

: S/^ M // g

You can remove all ^ M from the file. Similarly, you only need to press ^ M in the file.



This problem often occurs when files are exchanged between WINDOWS/DOS and UNIX systems, because these two systems have different interpretations of the "line feed" concept of text. Therefore, another solution is to convert such text. Vim can do this internally. First open the text, and then

Set fileformat = unix and then save the file again to overwrite the original file to remove: ^ M



(14) How to indent a piece of code (insert a tab) as a whole )?

Select a piece of code, <or> or 2 <or 3> ......



(15) how to merge two rows?

The key for merging two rows is J.

In addition, if you set

Set backspace = 2

You can use backspace or delete to easily delete the last or next row.



(16) how to set the indentation distance (tab width )?

Set tabstop = 8

Set shiftwidth = 8



(17) How to Prevent VIM from generating backup files (~ File )?

Set nobackup

For more details, see help backup. In addition, you can set backupdir to store the backup files in a directory without turning off backup. For example

Set backupdir = C: \ Program \ Files \ Vim \ tmp



(18) How do I set the gvim font in windows?

: Set guifont = * The select font dialog box is displayed,

After the selection, then: set guifont? Check, and write the guifont settings into vimrc.



(18) how to set the colorscheme of vim

: Colorscheme is selected by tab, and the favorite scheme is written into the. vimrc file. The. vim file of colorscheme is in the $ vim $/colors/folder, which can be found at www.vim.org.

Download various colorscheme.


At present, we can see that VI is more powerful than that. Its high configurability is surprising, especially when it can be automatically completed on character terminals, for more information, see the logs at the end of last year and configure VIM as the power of IDE.
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.