Questions tagged [search]

For questions about searching for text with / or search(). For find/replace operations, use tag [substitute] instead.

Filter by
Sorted by
Tagged with
136 votes
3 answers
17k views

How do I navigate to topics in Vim's documentation?

In Vim's built-in help system, how do I… search for topics that I want help on? follow hyperlinks? browse around for related material?
romainl's user avatar
  • 40.6k
126 votes
7 answers
96k views

Is there a way to count the number of occurrences of a word in a file?

Is it possible to count how many times a word or a pattern appears in a file? This is sometimes useful to find out how many times a function has been called, etc.
thameera's user avatar
  • 17.4k
103 votes
10 answers
159k views

How can I clear word highlighting in the current document (e.g. such as after searching for a word)?

When you search for a word in a file with something like /console.log, all of the instances of console.log are highlighted. When you're no longer interested in these, the highlighting can be ...
drs's user avatar
  • 3,028
92 votes
6 answers
49k views

Vim search replace all files in current (project) folder

One of the open questions I have about Vim is if there is a way to perform a search/replace in the current project (bear with me if I use this notion inherited from other editors). For instance, let'...
Simone Carletti's user avatar
5 votes
4 answers
1k views

Can I use a different color for the selected match than for other matches [duplicate]

In the screencast below I searched statusline and then cycle through the results with n. Is it possible to highlight the selected match with a different color than the other matches, instead of ...
Bananach's user avatar
  • 494
5 votes
1 answer
2k views

How to show all unique types of a pattern?

I have a very large file containing about, for example, 5 million lines like this: Songs=****, Singers=**** Singers=*** Songs=*** .... # many many lines SongType=*** ... # many many lines again Album=...
Lerner Zhang's user avatar
78 votes
12 answers
26k views

How to find and replace in Vim without having to type the original word?

I'd like to optimize my "find and replace" workflow in Vim. It's something I do often, as I'm sure most of you do too. Usually something along the lines of -- copy a block and change the name of a ...
Joel's user avatar
  • 853
25 votes
3 answers
2k views

How can I get ‘n’ to go forward even if I started searching with ‘?’ or ‘#’?

Vim has (at least) two sets of commands that search in opposite directions. Typing / and then a pattern will search forward through the document for the pattern; typing ? and then a pattern will ...
bdesham's user avatar
  • 2,983
14 votes
5 answers
2k views

Quick search, limited to a C++ function

I work on a rather large C++ project. One of the more frustrating aspects of its organization is very large functions placed inside ridiculously large files. I often want to search for any instance ...
jkerian's user avatar
  • 333
13 votes
7 answers
2k views

How to search for pattern in certain syntax regions?

I am using vim to edit latex documents. Often, I will want to change the name of a variable to something else. However, it's a little annoying to have to wade through irrelevant matches when I search ...
Rob F.'s user avatar
  • 133
13 votes
8 answers
11k views

How to replace a pair of brackets

I have a file, containing some lines like this: aaa bbb (ccc) ddd. Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd. I think that \...
Yves's user avatar
  • 1,013
10 votes
6 answers
2k views

Show exact number of matches even for large (>99) numbers

When I search for something /search-term and it shows up more than 100 times in my file, vim shows me that I'm on match x/>99 instead of x/121 etc. For smaller numbers it shows me the exact number ...
minseong's user avatar
  • 2,323
8 votes
2 answers
1k views

How do I make list of search results editable?

There are several times, I need to grep for some_pattern in some XHTML files (many a time, file count in a specified folder goes beyond 10K+). Previously, I have used Lugaru's Epsilon Programmer's ...
Cylian's user avatar
  • 539
6 votes
3 answers
2k views

How can I make a search in vimscript let n and N look for more?

I'm writing a script that does a search like so: normal /needle^M. This works like I want, except that I want to be able to use n or N to repeat the search. In case it helps, here's the full ...
Flurrywinde's user avatar
2 votes
1 answer
776 views

How to remove inner content of html tag conditionally?

I've html file (e.g. curl -o page.html http://example.com/) where I would like to remove inner html block for given html tag. I've the following code which works: $ ex -V1 page.html <<-EOF /&...
kenorb's user avatar
  • 18.5k
35 votes
1 answer
13k views

Can I search for a Unicode combining character in Vim?

I have a file with the character ã (lowercase a + combining tilde). Encoding and fileencoding are both utf-8. ga shows <a> 97, hex 61, octal 141 <~> 771, Hex 0303, Octal 1403 (but ...
cxw's user avatar
  • 974
20 votes
2 answers
14k views

How to use Ctrl-P to interactively search file contents for all files in a project?

I tried today to perform a search on every files of a project. I use Ctrl-P and search on lines like so : But the result of the search is only on the previously opened file. Here I open only main....
nobe4's user avatar
  • 16k
16 votes
2 answers
9k views

What is the functional difference between :nohlsearch and :set nohlsearch?

I noticed that when I use :nohlsearch that it does not actually do the same thing as :set nohlsearch. In particular, the hlsearch setting is not actually switched off (which is what :set hlsearch) ...
Steven Lu's user avatar
  • 2,251
14 votes
2 answers
1k views

different search patterns per window / buffer

Is it somehow possible to have different search patterns per window or buffer, ie. I want to search for term A in window/buffer A and term B in window/buffer B so that using hlsearch, highlights ...
languitar's user avatar
  • 293
13 votes
2 answers
14k views

Multiline Search and Replace

Would like to perform a search and replace in a file with 12000 lines. Specifically, if an occurrence of ^ SetFontSize 28 exists after a ^Hide block and before the next ^Hide or ^Show, change 28 ...
Guy's user avatar
  • 131
10 votes
2 answers
2k views

Stay at matching search result after escape

For years I unknowingly used some, shall we say, deviant distribution of vim that would stay at the location of the highlighted search result after hitting <Esc> in the middle of a search ...
Mahmoud Al-Qudsi's user avatar
10 votes
2 answers
904 views

case-sensitive * with ignorecase on

I like the behavior of the 'smartcase' option, which lets searches I type in by hand be case-insensitive by default, case-sensitive when there are any capital letters. I also like the behavior of the '...
Daniel Wagner's user avatar
8 votes
1 answer
3k views

What's the difference between `/x` and `:/x`

Assume buffer content: aa bb yy xx aa bb What's the different between /xx and :/xx?
dedowsdi's user avatar
  • 6,258
0 votes
1 answer
164 views

How to get the same message and error behavior as normal `n` when calling `normal n` in function?

I wrote a vimscript function that prevents the cursor from changing its location on the screen when I press n: function! s:my_n() let l:start=line("w0") let end = line("w$")...
Bananach's user avatar
  • 494
64 votes
7 answers
57k views

How to show only matching lines?

Is there a way to hide all lines that did not match, while going through the list of result lines? In a long file, I would like to search for a pattern that will match roughly 200 lines or so. The ...
Volker Siegel's user avatar
33 votes
4 answers
11k views

Delete from cursor till nth occurence of character x

I know that I can delete from the cursor till and including the first occurence of a character x with d/x<CR> or with v/x<CR>d if I want to see the selection first and then delete. How can ...
user2609980's user avatar
31 votes
3 answers
3k views

How can I perform a search when vim displays content using "more" pager?

I want to see the value of a variable, so I run the command :let Vim displays the results of this command using the --more-- pager. How can I search this for a phrase, e.g. html? I've looked the ...
the_velour_fog's user avatar
27 votes
3 answers
4k views

Is there search and replace history in Vim?

We can retrieve search and replace history on IDEs and text editors. Likewise is there a search history or a way to retrieve previously searched expression in Vim?
vusan's user avatar
  • 381
18 votes
3 answers
3k views

Mapping with motion

I'm trying to understand how can I use operator with subsequent motion inside a mapping. For example: nmap /c c{here we pending for a motion}/<C-r>"<CR> The map should do the following: ...
Timur Fayzrakhmanov's user avatar
15 votes
3 answers
1k views

How do I apply a set of keystrokes to every occurrence of a word?

If I want to delete every word, that is easy, because I would do something like this: :s%/WORD//g But what if I want to do xyz to every occurrence of "WORD"? For example, let's say I want to delete ...
DJMcMayhem's user avatar
  • 17.6k
12 votes
3 answers
523 views

How to selectively change a hightlighted string?

By searching, I've highlighted the string that I want to change. Now I want to replace only the highlighted text. I'm using n to advance to the next instance of the search string. I was thinking I ...
zundarz's user avatar
  • 305
11 votes
1 answer
3k views

Percent key ( % ) matching behavior for angle brackets ( < > )

In vi it's very helpful to be able to place the cursor on a '(' or '{' or '[' character, press the '%' key, and move to the matching ')', '}', or ']'. But this does not work for me with angle ...
Clovis_Sangrail's user avatar
10 votes
3 answers
855 views

How can I automatically center first search result?

I want to have the very first search result centered on the screen. I know about :nnoremap n nzz mapping, but it only works for centering next matches, not the first. I thought that maybe I could use ...
Jan Warchoł's user avatar
10 votes
1 answer
555 views

To print with search results highlighted

When we search a pattern in vim, the matches are usually highlighted. However, when we print it out, the highlights disappear. I tried both :hardcopy > my_file.pdf and :TOhtml. Neither retains the ...
user3813057's user avatar
  • 1,461
10 votes
2 answers
848 views

Navigating code projects without using external commands

I am trying to gather a toolbox of Vim commands/queries, in command or search mode, for navigating a code-base without using Ctags or other external tools. Ctags are great but support can vary ...
user1332148's user avatar
9 votes
2 answers
3k views

How do I jump to the next line with the same indent level?

I have a space formatted (yaml) file like the following: en: templates: template: "VERYLONGTEXTHEREWITHHUNDREDSOFLINES" destination: "I want to jump here" other: stuff ...
Drew's user avatar
  • 193
9 votes
4 answers
7k views

How to execute command on every matching pattern, not just lines?

I know in vim I can use :global to execute commands on the matching lines. But I'm wondering if it's possible to execute those commands on every matching pattern, not just lines, consider following ...
Aaron Shen's user avatar
  • 2,519
8 votes
2 answers
129 views

Searching for a phrase in hard-wrapped text

When searching for a phrase in hard-wrapped text, one wants newlines to be treated as spaces. Can one have that happen? Saying /the\_sphrase\_sI\_sseek is a bit awkward.
Toothrot's user avatar
  • 3,129
8 votes
1 answer
32k views

How to search for any repeating character, X number of times

Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times? I know I can search for .., but those to characters won'...
user3.1415927's user avatar
8 votes
2 answers
438 views

How to store all occurrences of a multiline search in a register?

I'm not sure if I've failed at searching for the answer to this, but now I've worked it out I wanted to document it here. The problem is searching for a multiline pattern, and storing all such ...
Jonathan's user avatar
  • 141
7 votes
1 answer
2k views

How can I repeat last Ex-mode command in normal mode?

I'm looking for something like . or @@ that will work in normal mode for repeating the last Ex-mode command. As per the rule of the XY problem, I'll also state the broader goal: I have a fancy regex ...
Wildcard's user avatar
  • 4,409
7 votes
2 answers
5k views

Do a forward slash search but don't have vim immediately jump to the string?

When I do a /someStringtoFind in vim it immediately jumps to the closest matched string. Almost always this is what I want it to do. Sometimes though, I want to be able to enter a string to find and ...
red888's user avatar
  • 193
7 votes
1 answer
2k views

Search and highlight two different strings in different colors

I have a situation with typesetting a book in which the quotation marks are all screwed up. The author used a variety of marks¹ and apparently didn't believe in discriminating between them. I've ...
Caleb's user avatar
  • 2,245
6 votes
1 answer
419 views

How to move the cursor to the next reference, option, heading in man pages?

If we load the built-in man.vim filetype plugin by writing in our vimrc: runtime ftplugin/man.vim We can read a man page inside a Vim buffer, using the :Man command described in :h :Man: MAN ...
user9433424's user avatar
  • 6,138
6 votes
5 answers
6k views

Match any number of digits NOT followed by

I want to search for: From start of the line, any number of white spaces followed by any number of digits NOT followed by a dot. And replace it with all that was searched and a dot. This is just ...
deshmukh's user avatar
  • 265
5 votes
3 answers
678 views

What does the 'z' flag for search() do?

I was looking at the documentation for search() (:help search()). I noticed that there is a "z" flag: 'z'    start searching at the cursor column instead of zero When the 'z' flag is not ...
Flux's user avatar
  • 1,041
4 votes
2 answers
2k views

Delete all of a file except for certain words that contain certain letters

I have a file that contains words that I want to save, along with other junk that I do not need. I just want to delete everything except the words that contain a certain pattern. Take email addresses ...
Questionmark's user avatar
4 votes
2 answers
1k views

How to search in Ex mode and place the cursor on the matched pattern?

I've the following example: vim -E http://example.com/ where I'd like to search for <head> tag. It seems that search only works when the lines are separated, but when the lines are joined ...
kenorb's user avatar
  • 18.5k
4 votes
1 answer
142 views

Skip to next batch of search results (or find next non-match)?

So you search /needle, and end up trapped in a haystack of result lines. Is there a quick way to skip to the first non-matching line (and then the next batch of results)? E.g. I'm viewing the results ...
usretc's user avatar
  • 374
4 votes
2 answers
1k views

Copy text based on delimiting strings

I see how to copy text in vim using visual mode, arrow keys, y to yank, and p to paste. I also see how to find a given starting string using back slash. However, I'd appreciate knowing how to copy ...
QuantumFool's user avatar