How to find and replace Vim in Linux

Source: Internet
Author: User
Tags uppercase letter

Someone always asked me if Vim can find, of course! And it's a super strong search! This article describes the settings and how to use Vim in detail. This includes finding and replacing, finding the cursor's word, highlighting foreground/background color, switching highlighting, case sensitive lookup, and more.

Find

Press/To enter lookup mode in normal mode, enter the string to find and press ENTER. Vim jumps to the first match. Press N to find the next, and press N to find the previous.

Vim lookup supports regular expressions, such as/vim$, which matches the "vim" at the end of the line. You need to find special characters to escape, such as/vim\$ matching "vim$".

Note Find carriage return should be \ n, and replace with carriage return with \ r (equivalent to <CR>).
Case Sensitive Lookup

Adding \c in lookup mode indicates a case insensitive lookup, and \c indicates a case sensitive lookup. For example:

/foo\c
Will find all of the "foo", "foo", "foo" and other strings.

Case Sensitive Configuration

Vim uses case sensitive lookups by default, and is often configured to be case insensitive for convenience:

"Set default case insensitive lookup
Set ignorecase
"If you have an uppercase letter, switch to case sensitive lookup
Set Smartcase
Paste the above settings into your ~/.VIMRC and reopen vim to take effect.
Find the current word

Press * in normal mode to find the word (word) where the cursor is located, requiring a blank character or punctuation before and after each occurrence. For example, the current is Foo, which can match Foo in Foo Bar, but not the Foo in Foobar. This is useful when looking for function names and variable names.

Press g* to find the character sequence of the word at the cursor, without requiring the characters before and after each occurrence. Foo Bar and Foobar can be matched to.

Find and replace

: the s (substitute) command is used to find and replace strings. The syntax is as follows:

: {scope}s/{target}/{replace}/{replace flag}
For example:%s/foo/bar/g will look for Foo in the global scope (%) and replace it with bar, all occurrences will be replaced (g).

Scope of Action

The scope of action is divided into current lines, full-text, constituencies, and so on.

Current line:

: s/foo/bar/g
Full:

:%s/foo/bar/g
Selection, select the area in Visual mode after entering:, Vim can be automatically completed as: ';, ' >.

: ';, ' >s/foo/bar/g
2-11 lines:

: 5,12s/foo/bar/g
Current line. With the next two lines + 2:

:., +2s/foo/bar/g
Replace flag

The G, at the end of the command above, is one of the replacement flags that represents the global substitution (that is, all occurrences of the replacement target). There are a number of other useful replacement flags:

The null replacement flag indicates that only the first occurrence of the goal, starting at the cursor position, is replaced:

:%s/foo/bar
I indicates case insensitive lookup, I represents case sensitive:

:%s/foo/bar/i
# is equivalent to \c (insensitive) or \c (sensitive) in the pattern
:%s/foo\c/bar
c indicates a need for confirmation, such as global lookup "foo" replaced with "bar" and needs to be confirmed:

:%S/FOO/BAR/GC
After a carriage return, VIM moves the cursor to the location of each "foo" and prompts

Replace with bar (y/n/a/q/l/^e/^y)?
Press Y to replace, n means no substitution, a means replace all, Q is exit lookup mode, l means replace current position and exits. ^e and ^y is the cursor move shortcut key, reference: Vim in how to quickly move the cursor.

Highlighting settings

Highlight color settings

If you feel as if the highlighted color is not very comfortable, you can also set it:

Highlight Search Ctermbg=grey Ctermfg=black
Paste the above configuration to ~/.VIMRC and reopen vim to take effect.
The above configuration specifies that the foreground color (foreground) of the search result is black and the background color (background) is gray. Disable/enable highlighting

Have wood feel vim is still highlighting the search results after each lookup replacement? You can manually let it stop highlighting and enter in normal mode:

: Nohighlight
"is equivalent to
: Nohl
In fact, the above command disables all highlighting, and the correct command is: Set Nohlsearch. Next search needs: Set Hlsearch start search highlighting again. How can I let vim find/replace automatically cancel highlighting, the next time you find it automatically open?

"When the cursor is not moving for some time, disable the highlight

Autocmd Cursorhold * Set Nohlsearch
"When you enter a lookup command, then enable the highlight

Noremap N:set Hlsearch<cr>n
Noremap N:set Hlsearch<cr>n
Noremap/: Set hlsearch<cr>/
Noremap? : Set Hlsearch<cr>?
Noremap * *:set hlsearch<cr>

Paste the above configuration to ~/.VIMRC and reopen vim to take effect.

Delete the ^m in the text
 
Problem Description: For newline, window with carriage return Line (0A0D) to express, Linux is a carriage return (0A) to express. This way, there will always be a ^m when you copy files on Windows to UNIX. Write a shell or C program that filters a line break (0D) for a window file under UNIX.
 
。 Using commands: Cat filename1 | tr-d "^v^m" > NewFile;
 
。 Use command: sed-e "s/^v^m//" filename > outputfilename. Note that in 1, 22 of the methods, ^v and ^m refer to Ctrl + V and ctrl+m. You have to manually input, not paste.
 
。 In VI Processing: First use VI to open the file, and then press the ESC key, and then enter the command:%s/^v^m//.
 
。 :%s/^m$//g
 
If the above method is useless, the correct solution is: [Page]
 
。 tr-d \ "\\r\" < src >dest
 
。 tr-d \ "\\015\" dest
 
。 Strings A>b
6. Replacement Confirmation
There are many times when we need a character (string) to be replaced in some position in the article, while the other position is not replaced by a selective operation, which requires the user to confirm, vi Search and replace the same support
For example
: S/vivian/sky/g replaces current line all Vivian for Sky
After the command plus a letter C can be implemented, namely: S/VIVIAN/SKY/GC
As implies, C is the abbreviation of confirm
 
7. Other
 
Use the: s command to implement a string substitution. The specific uses include:
 
: s/str1/str2/replaces the first occurrence of a string with a string str2 str1
 
: S/str1/str2/g replaces all occurrences of the string in a line with a string str2 str1
 
:。 , $ s/str1/str2/g Replaces all occurrences of the body's current line to the end with a string str2 str1
 
: 1,$ s/str1/str2/g replaces all occurrences of a string in the body with a string str2 str1
 
: g/str1/s//str2/g function ditto
 
From the above substitution command you can see that G is placed at the end of the command to replace each occurrence of the search string;
 
The first occurrence of the string is substituted; G is placed at the beginning of the command to replace all the lines in the body that contain the search string

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.