Questions tagged [search]

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

Filter by
Sorted by
Tagged with
104
votes
2answers
8k 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?
83
votes
10answers
101k 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 ...
89
votes
7answers
53k 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.
19
votes
3answers
1k 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 ...
5
votes
1answer
959 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=...
51
votes
5answers
16k 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'...
13
votes
5answers
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 ...
11
votes
7answers
912 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 ...
2
votes
1answer
567 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 /&...
67
votes
11answers
15k 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 ...
16
votes
2answers
8k 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....
10
votes
1answer
856 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 ...
21
votes
1answer
6k 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 ...
10
votes
2answers
5k 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 ...
8
votes
1answer
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?
8
votes
2answers
860 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 ...
0
votes
0answers
63 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! my_n() let l:start=line("w0") let end = line("w$") let cursor = ...
40
votes
7answers
27k 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 ...
21
votes
2answers
1k 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 ...
24
votes
4answers
7k 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 ...
4
votes
3answers
3k 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 ...
13
votes
2answers
4k 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) ...
11
votes
3answers
411 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 ...
17
votes
2answers
2k 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?
14
votes
3answers
859 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 ...
10
votes
1answer
346 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 ...
9
votes
2answers
739 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 ...
6
votes
1answer
1k 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 ...
4
votes
2answers
539 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 ...
6
votes
2answers
950 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 ...
6
votes
1answer
203 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 ...
5
votes
2answers
3k 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 ...
5
votes
5answers
4k 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 ...
5
votes
3answers
768 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 ...
4
votes
1answer
267 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 '...
4
votes
2answers
862 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 ...
3
votes
1answer
145 views

Get immediate feedback about a search string with no matches in vim

Is there a way to immediately get feedback about a search string with no matches? At the moment I have to press Enter first to get Pattern not found. I'd like if the background (of the search line or ...
3
votes
3answers
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 ...
3
votes
1answer
179 views

How to jump to a next or previous specific character ignoring linebreaks and not destroying highlight or overwriting my registry?

I have not found much use for ()[]{}, and so I want to map them so it does a find command, jumping to the previous instance of that particular character. Thus, ) will jump forward to the next ), ...
2
votes
4answers
423 views

Search for line, move to end of search string, then search+replace

(EDIT: In the original question, I had accidentally used \w instead of \<. Comments and answers with \w should be interpreted as using \< as well.) In my LaTeX source files, section headings ...
2
votes
1answer
42 views

Finding where one word is close to another word: parametrized command in vimrc

I asked this question, about how to find where one word is close to another word, and Quasimodo gave the very fine answer: /\vaid(\W+\w+){,3}\W+country Now I want to parametrize this command into ...
2
votes
0answers
146 views

Search within syntax group?

Is there a way to take advantage of syntax highlighting for searches? For example, can a search be limited to comments or non-comments, if they are correctly marked by syntax highlighting?
2
votes
1answer
309 views

How to print a multi-line match?

I've a file where I would like to extract a multiline pattern in non-greedy way (similar to this scenario) by using ex editor. This example works fine: $ ex +'/aa/,/cc/p' -scq! <(echo -e "start\...
0
votes
1answer
67 views

How do you substitute on previous matches?

For example, if I have the following: MY_VAR = "hello world" print "the var %s" % MY_VAR If I position the cursor on MY_VAR and press *, Vim highlights both matches. Suppose I want to rename this to ...
0
votes
1answer
559 views

Run a command on every match of a search (not on the line)

I am trying to run a command on every occurrence of a search match. So, If i have some thing like this where a search match matches only these two anchor tags, .................. <a src="****" &...
0
votes
1answer
17 views

get line of first occurence of pattern in buffer

I am trying to write a function which I bind to "n" and "N" to highlight the current search pattern. That part works. I would like to detect when the cursor wraps to the first match in the buffer (...