Tips for vim to edit commands

Source: Internet
Author: User

Java EE Development of various types of resources download list, the history of the most complete IT resources, personal collection summary.

Vim replacement and batch modification

1 Simple substitution Expressions
:%s/four/4/g
The "%" range prefix indicates that replacements are performed in all rows.

The last "G" tag represents all the matching points in the replacement row.

If you are only working on the current line, you can simply remove the%

If you have a word like "thirtyfour", the above command will go wrong. In this case, the word will be replaced with "Thirty4″." To resolve this problem, use "<" to specify the beginning of the matching word:
:%s/<four/4/g
Obviously, this will make a mistake when dealing with "fourty". Use ">" to solve this problem:
:%s/<four>/4/g
If you are coding, you may only want to replace the "four" in the annotation and keep the code in the. Because this is difficult to specify, you can add a "C" tag to the substitution command so that Vim prompts you before each replacement:
:%S/<FOUR>/4/GC

Word exact match substitution

Sed-e "s/\<old\>/new/g" file



modify All. java files in the directory

Find. *.java-type F | Xargs sed-i "S/gamecenter/appcenter/g"


Modify all files in the directory

Find. -type F | Xargs sed-i "S/gamecenter/appcenter/g"

grep to find words exactly

Grep-w Word file

2 Remove Extra space
To remove the extra space after each line, you can execute the following command:
:%s/s+$//
The command indicates that the range is "%", so this will work on the entire file. The match pattern for the "substitute" command is
"s+$". This represents one or more (+) spaces (s) before the end of the line ($). The "to" section of the substitution command is empty: "//". This will delete those matching whitespace characters.
Replace multiple spaces with one character ":"

:%s/\s\{2,}/:/g


3, matching the pattern of repetition
The asterisk entry "*" stipulates that the item in front of it can be repeated any time. So:
/a*
Match "A", "AA", "AAA", and so on. But also matches "" (empty string), because 0 times also contains.

The asterisk "*" applies only to the item immediately preceding it. So "ab*" matches "a", "AB", "ABB", "abbb", and so on.

To repeat the entire string multiple times, the string must be composed of an item. The way to make an item is to precede it with "(,"). So this command:
/(AB) *
Match: "AB", "Abab", "Ababab", and so on. and also matches "".
To avoid matching empty strings, use "+". This means that the previous item can be matched one or more times.
/ab+
Match "AB", "ABB", "abbb", and so on. It does not match "a" followed by "B".
To match an optional option, use "=". For example:
/folders=
Match "folder" and "Folders".

4 Specify the number of repetitions
To match a specific number of repetitions of an item, use the form "{n,m}". where "n" and "M" are numbers. The item in front of it will be repeated "n" to "M" Times (|inclusive| contains "n" and "M"). For example:
/ab{3,5}
Match "abbb", "abbbb" and "abbbbb".
When "n" is omitted, the default is zero. When "M" is omitted, the default is infinity. When ", M" is omitted, it means repeating exactly "n" Times. For example:
Pattern matching times
{, 4} 0,1,2,3 or 4
{3,} 3,4,5, etc.
{0,1} 0 or 1, same =
{0,} 0 or more, same *
{1,} 1 or more, same +
{3} 3

5 Multiple Select one match
In a lookup mode, the OR operator is "|". For example:
/foo|bar
This command matches "foo" or "bar". More choices can be linked to the following:
/one|two|three
Match "One", "two" or "three".
To match its multiple repetitions, the entire choice structure must be placed between "(and"):
/(Foo|bar) +
This command matches "foo", "Foobar", "Foofoo", "Barfoobar", and so on.
Another example:
/end (if|while|for)
This command matches "endif", "Endwhile" and "ENDfor".

-----------------------------------------------------------------

SED replacement command

SED-E "S/BAI/GOOGLE/G" tmp

For example: Bai modified to Google

Cat tmp
Baidu
Baimai
Dubai
Baibai
Abaia

sed-e "s/bai/google/g" tmp
googledu
Googlemai
Dugoogle
Googlegoogle
Agooglea

================================================================================

vim formatting and annotations


vim format code:
Format full-text instruction Gg=g
Format the current line of instruction = =
Format the current cursor next 8 lines 8==
Format selected line v Select the code snippet you want to format =

Note:
gg--arrives at the beginning of the file
=--Require indentation
g--until end of file

Comment Code: (This principle is a regular replacement using VIM)
Take C + + and Python for example.

Note Consecutive lines:

Instruction format: Start line, terminating line s/character to replace/replace with character/g

Such as
C + + source file comment out 10~20 line, instruction is:
: 10,20s/^/////g

The 10~20 line is commented out in the Python source file, and the instruction is:
: 10,20s/^/#/g

To cancel consecutive line comments:
instruction format Ibid. Just replace the characters with the substitution for the character to be interchanged:

Such as
Uncomment the 10~20 line in the C + + source file, and the directive is:
: 10,20s/^//////g

Uncomment the 10~20 line in the Python source file with the following directive:
: 10,20s/^#//g

================================================================================

vim syntax highlighting and automatic indentation

1, the location of the configuration file
Below the directory/etc/, there is a file called VIMRC, which is a public vim profile in the system and is valid for all users. In each user's home directory, you can create a private profile of your own, named: ". VIMRC ". For example, in the/root directory, there is usually one already. VIMRC files.
If you do not know the location of the configuration file and script, you can use the command in Vim: Scriptnames, which will display the following path
/etc/vimrc
/usr/share/vim/vim72/syntax/syntax.vim
/usr/share/vim/vim72/syntax/synload.vim
/usr/share/vim/vim72/syntax/syncolor.vim
/usr/share/vim/vim72/filetype.vim
/usr/share/vim/vim72/ftplugin.vim
/home/kdj/.vimrc
...
If you do not know the location of the Vim configuration file Search, enter: version, which will display
System VIMRC File: "/ETC/VIMRC"
User VIMRC file: "$HOME/.VIMRC"
User EXRC file: "$HOME/.EXRC"
$VIM preset: "/usr/share/vim"


2. Set syntax highlighting
1 Open VIMRC, add the following statement to make syntax highlighting:
Syntax on
2 If the syntax is not highlighted at this time, add the following statement to the profile file in the/etc directory:
Export Term=xterm-color

3. Set up Windows C + + Auto Indent (add the following set statement to VIMRC)
1) Set (soft) tab width to 4:
Set tabstop=4
Set softtabstop=4
2) Set the number of spaces indented to 4
Set shiftwidth=4
3 Set Auto indent: That is, the indentation value of each row is equal to the previous line; Use Noautoindent to cancel the setting:
Set Autoindent
4 to set up the automatic indent mode using C + + language:
Set Cindent
5 to set the specific indentation method for A/C + + language (for example, my Windows style):
Set Cinoptions={0,1s,t0,n-2,p2s, (03s,=.5s,>1s,=1s,:1s
6 If you want to display the line number of the text on the left, you can use the following statement:
Set Nu
Set Roler setting cursor display

7 Finally, if you don't have the following statement, add it:
If &term== "Xterm"
Set T_co=8
Set T_SB=^[[4%DM
Set T_SF=^[[3%DM
endif

================================================================================

Vim Related Introduction

Vim Help manual PDF
Http://blogimg.chinaunix.net/blog/upfile2/071116153236.pdf

VIM Online Handbook
Http://vcd.gro.clinux.org/doc/usr_toc.html
Http://vcd.gro.clinux.org/doc/help.html

Teach you how to convert vim into an IDE programming environment (text)
Http://blog.csdn.net/wooin/archive/2007/12/30/2004470.aspx


Vim color settings
http://zywangyan54.blog.163.com/blog/static/31810358200752993227703/

VIM program debugging
Http://www.wangchao.net.cn/bbsdetail_69434.html


Vim Usage Experience
Http://blog.csdn.net/camry_camry/archive/2004/09/23/114188.aspx


Vim Automatically annotate scripts
http://blog.chinaunix.net/u/6542/showart.php?id=357716


VIM plug-in Daquan and good introduction
Http://hi.baidu.com/00%C6%F3%B6%EC/blog/item/fd456c03a2d40f8bd53f7c29.html


Vim is learning and using
http://blog.linuxpk.com/3973/viewspace-2644

Vim's split-screen function (CSDN)

Vim Common Command Summary (CSDN)

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.