VIM command editing skills

Source: Internet
Author: User
Tags repetition
Download list of various resources developed by J2EE, the most comprehensive IT resources in history, personal favorites summary! Vim replacement and batch Modification 1 simple replacement expression: % sfour4g "%" range prefix indicates replacement in all rows. The last "g" Mark replaces all matching points in the row. If you only operate on the current row, you only need to remove %.

Download list of various resources developed by J2EE, the most comprehensive IT resources in history, personal favorites summary! Vim replace and batch modify 1 simple replace expression: % s/four/4/g "%" prefix indicates replacement is performed in all rows. The last "g" Mark replaces all matching points in the row. If you only operate on the current row, you only need to remove %.

Download list of various resources developed by J2EE, the most comprehensive IT resources in history, personal favorites summary!

Vim replacement and batch Modification

1. Simple replacement expression
: % S/four/4/g
The prefix of "%" indicates that replacement is performed in all rows.

The last "g" Mark replaces all matching points in the row.

If you only operate on the current row, remove %.

If you have a word like "thirtyfour", the above command will fail. In this case, the word is replaced with "thirty4 ″. To solve this problem, use "<" to specify the start of the matching word:
: % S/ Obviously, errors will still occur when processing "fourty. Use ">" to solve this problem:
: % S/ /4/g
If you are coding, you may just want to replace the "four" in the comment, but keep the "four" in the code. Since this is difficult to specify, you can add a "c" mark in the replacement command, so that Vim will prompt you before each replacement:
: % S/ /4/gc

Exact word matching and replacement

Sed-e "s /\ /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 precisely searches for words

Grep-w word file

2. Delete unnecessary Spaces
To delete unnecessary spaces behind each line, run the following command:
: % S/s + $ //
The command specifies that the range is "%", so this will apply to the entire file ." The matching mode of the substitute command is
"S + $ ". This indicates one or more (+) spaces (s) before the end of the line ($ ). The "to" part of the replacement command is empty :"//". In this way, the matching spaces are deleted.
Replace multiple spaces with one character ":"

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


3. Matching repetition Mode
The asterisk (*) specifies that the items before it can be repeated at any time. Therefore:
/*
Match "a", "aa", "aaa", and so on. But it also matches "" (Null String), because zero times are also included.

The asterisk "*" is only applied to the item next to it. Therefore, "AB *" matches "a", "AB", "abb", and "abbb.

To repeat the entire string multiple times, the string must be an item. Add "(" and ")" before the component. Therefore, this command:
/(AB )*
Matching: "AB", "abab", "ababab", and so on. It also matches "".
To avoid matching null strings, use "+ ". This indicates that the previous item can be matched once or multiple times.
/AB +
Match "AB", "abb", "abbb", and so on. It does not match "a" that follows "B ".
To match an option, use "= ". For example:
/Folders =
Matches "folder" and "folders ".

4. Repeated times
To match a specific number of times of repetition, use the form "{n, m. "N" and "m" are numbers. The item before it will be repeated "n" to "m" Times (| aggressive sive | contains "n" and "m "). For example:
/AB {3, 5}
Match "abbb", "abbbb", and "abbbbb ".
When "n" is omitted, It is 0 by default. When m is omitted, It is infinitely large by default. When ", m" is omitted, it indicates that the repetition is exactly "n" times. For example:
Number of pattern matches
{, 4}, or 4
{3,} 3, 4, 5, and so on
{0, 1} 0 or 1, same as =
{0,} 0 or more, the same *
{1,} 1 or more, same as +
{3} 3

5. Select one or more matching items.
In a search mode, the "or" operator is "| ". For example:
/Foo | bar
This command matches "foo" or "bar ". More options can be connected to the following:
/One | two | three
Match "one", "two", or "three ".
To match multiple times, the entire decision structure must be placed between "(" and:
/(Foo | bar) +
This command matches "foo", "foobar", "foofoo", "barfoobar", and so on.
Another example:
/End (if | while |)
This command matches "endif", "endwhile", and "endfor ".

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

Sed replacement command

Sed-e "s/bai/google/g" tmp

For example, modify bai to google

Cat tmp
Baidu
Baimai
Dubai
Baibai
Abaia

Sed-e "s/bai/google/g" tmp
Googledu
Googlemai
Dugoogle
Googlegoogle
Agooglea

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

Vim format and comment


VIM format code:
Format full-text command gg = G
Format the current line command =
Format the next 8 rows of the current cursor =
Format the selected row v select the code segment to be formatted =

Note:
Gg -- arrive at the beginning of the file
= -- Requires indentation
G -- until the end of the file

Annotation code:(This principle is to use VIM's regular expression replacement)
Take C ++ and Python as examples.

Comment row:

Command Format: start line, end line s/character to be replaced/Replace with character/g

For example,
Comment out 10 ~ in the C ++ source file ~ 20 lines, command:
: 10, 20 s/^ // g

Comment out 10 ~ In the Python source file ~ 20 lines, command:
: 10, 20 s/^/#/g

Cancel comments of consecutive rows:
The command format is the same as above. Only replacement characters and replacement with characters must be exchanged:

For example,
Cancel 10 ~ 20 lines of comments. The command is:
: 10, 20 s/^ ///g

Cancel 10 ~ 20 lines of comments. The command is:
: 10, 20 s/^ # // g

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

VIM syntax highlighting and automatic indent

1. Configuration File Location
Under the/etc/directory, there is a file named vimrc, which is a public vim configuration file in the system and is valid for all users. In each user's home directory, you can create a private configuration file named ". vimrc ". For example, A. vimrc file already exists in the/root directory.
If you do not know the location of the configuration file and script, run the following command in vim: scriptnames:
/Etc/vimrc
/Usr/share/vim/vim72/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 search location of the vim configuration file, enter: version to display
System vimrc file: "/etc/vimrc"
User vimrc file: "$ HOME/. vimrc"
User exrc file: "$ HOME/. exrc"
$ VIM default value: "/usr/share/vim"


2. Set syntax highlighting
1) Open vimrc and add the following statement to highlight the Syntax:
Syntax on
2) If the syntax is still not highlighted, add the following statement to the profile file in the/etc directory:
Export TERM = xterm-color

3. set Windows C/C ++ auto indent (Add the following set statement to vimrc)
1) set the (soft) tab width to 4:
Set tabstop = 4
Set softtabstop = 4
2) set the number of spaces for indentation to 4.
Set shiftwidth = 4
3) Set automatic indent: that is, the indent value of each line is equal to that of the previous line; Use noautoindent to cancel the settings:
Set autoindent
4) set the automatic indent mode in C/C ++ language:
Set cindent
5) set the specific indent mode for C/C ++ language (taking my windows style as an example ):
Set cinoptions = {s, t0, N-2, p2s, (03 s, =. 5 s,> 1 s, = 1 s,: 1 s
6) to display the line number of the text on the left, use the following statement:
Set nu
Set roler set Cursor Display

7) Add the following statement if it does not exist:
If & term = "xterm"
Set t_Co = 8
Set t_Sb = ^ [4% dm
Set t_Sf = ^ [3% dm
Endif

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

Vim Introduction

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

VIM online manual
Http://vcd.gro.clinux.org/doc/usr_toc.html
Http://vcd.gro.clinux.org/doc/help.html

How to convert Vim into an IDE programming environment)
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 User Experience
Http://blog.csdn.net/camry_camry/archive/2004/09/23/114188.aspx


Vim automatically adds comments to the script
Http://blog.chinaunix.net/u/6542/showart.php? Id = 357716


VIM plug-ins
Http://hi.baidu.com/00%C6%F3%B6%EC/blog/item/fd456c03a2d40f8bd53f7c29.html


Vim is an out-of-the-box
Http://blog.linuxpk.com/3973/viewspace-2644

Vim split-screen function (CSDN)

Summary of common vim commands (CSDN)

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.