This article is reproduced from: http://harttle.land/2016/08/08/vim-search-in-file.html
Someone always asks if I can find it in Vim, of course! And it is a super strong search! This article introduces in detail the settings and usage of Vim. Including search and replace, find the word under the cursor, highlight the foreground / background color, switch the highlight state, case sensitive search, etc.
Find
Press / in normal mode to enter the search mode, enter the character string to be searched and press Enter. Vim will jump to the first match. Press n to find the next one, and press N to find the previous one.
Vim search supports regular expressions, for example, / vim $ matches "vim" at the end of the line. Need to find special characters need to be escaped, for example / vim \ $ matches "vim $".
Note that \ n should be used to find the carriage return, and \ r should be used to replace the carriage return (equivalent to <CR>).
Case sensitive search
Add \ c to the search mode for case-insensitive search, and \ C for case-sensitive search. E.g:
/ foo \ c
Will find all "foo", "FOO", "Foo" and other strings.
Case sensitive configuration
Vim uses case-sensitive search by default. For convenience, we often configure it to be case-insensitive:
"Set the default case-insensitive search
set ignorecase
"If there is a capital letter, switch to case-sensitive search
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 where the cursor is located. It is required that each occurrence of the word be a blank character or a punctuation mark. For example, foo currently matches foo in foo bar, but not foo in foobar. This is very useful when looking up function names and variable names.
Press g * to find the character sequence of the word under the cursor. There is no requirement for the characters before and after each occurrence. That is, foo in foo bar and foobar can be matched.
Find and replace
The: s (substitute) command is used to find and replace character strings. The syntax is as follows:
: {Scope of action} s / {Target} / {Replacement} / {Replacement logo}
For example:% s / foo / bar / g will find 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 the current line, full text, selection, etc.
Current line:
: s / foo / bar / g
full text:
:% s / foo / bar / g
To select a region, enter it after selecting the region in Visual mode, and Vim will automatically complete as: ‘<,’>.
: ‘<,‘> S / foo / bar / g
Line 2-11:
: 5,12s / foo / bar / g
Current line. +2 in the next two lines:
:., + 2s / foo / bar / g
Replacement sign
The g at the end of the above command is one of the replacement flags, which means global global replacement (that is, all occurrences of the replacement target). There are many other useful replacement flags:
The empty replacement flag indicates that only replacement starts from the cursor position and the target appears for the first time:
:% s / foo / bar
i means case-insensitive search, I means case-sensitive:
:% s / foo / bar / i
# Equivalent to \ c (insensitive) or \ C (sensitive) in the pattern
:% s / foo \ c / bar
c indicates that confirmation is required, for example, the global search for "foo" is replaced with "bar" and confirmation is required:
:% s / foo / bar / gc
After Enter, Vim will move the cursor to the position where "foo" appears, and prompt
replace with bar (y / n / a / q / l / ^ E / ^ Y)?
Press y to replace, n to not replace, a to replace all, q to exit the search mode, and l to replace the current position and exit. ^ E and ^ Y are shortcut keys for cursor movement. Refer to: How to quickly move cursor in Vim.
Highlight settings Highlight color settings
If you feel like the highlight color is not very comfortable like me, you can set it in ~ / .vimrc:
highlight Search ctermbg = yellow ctermfg = black
highlight IncSearch ctermbg = black ctermfg = yellow
highlight MatchParen cterm = underline ctermbg = NONE ctermfg = NONE
The above configuration specifies that the foreground of the Search result is black and the background color is gray; the foreground of the progressive search is black and the background color is yellow; the character at the cursor is underlined.
More CTERM colors can be consulted: http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim
Disable / enable highlighting
Do you think Vim still highlights the search results after every search and replacement? You can manually stop it highlighting, enter in normal mode:
: nohighlight
" Equivalent to
: nohl
In fact, the above command disables all highlighting, the only command to disable search highlighting is: set nohlsearch. The next time you need to search: set hlsearch to start the search highlight again.
Delay disabled
How can I make Vim automatically cancel the highlighting after a period of search / replace, and automatically turn on when a search occurs?
"When the cursor stays still for a while, highlighting is disabled
autocmd cursorhold * set nohlsearch
"When entering the search command, then enable highlighting
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 into ~ / .vimrc and reopen vim to take effect.
One-click disable
If delaying disabling search highlighting is still not comfortable, you can set a shortcut key to disable / enable search highlighting with one click:
noremap n: set hlsearch <cr> n
noremap N: set hlsearch <cr> N
noremap /: set hlsearch <cr> /
noremap?: set hlsearch <cr>?
noremap * *: set hlsearch <cr>
nnoremap <c-h>: call DisableHighlight () <cr>
function! DisableHighlight ()
set nohlsearch
endfunc
To turn off highlighting, just press Ctrl + H, and it will be automatically enabled when the next search occurs.
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.