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?
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.
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 ...
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 ...
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'...
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 ...
27
votes
8answers
20k views

How to automatically turn off “hlsearch” after I'm done searching?

I love the "hlsearch" setting, but after I'm done searching, I find it obnoxious. Especially if I search for something that has a lot of matches. I know that I can turn this off with :set nohlsearch ...
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 ...
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 ...
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 ...
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 ...
19
votes
4answers
18k views

When to use :grep and :vimgrep in vim?

In vim :make makes it easy to build and fix errors, and :grep and :vimgrep behaves like :make. But why do you need these two commands? Why not just search for the pattern and just press n? What are ...
18
votes
4answers
6k views

How to show search results for all open buffers

One thing I often do (but rely on the command line to do) is searching/grepping in multiple files. Is there a way to display search results for all the open buffers? Ideally, I'd like to have a new ...
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?
16
votes
3answers
9k views

search for lines which contain a pattern but do not contain a second pattern

I need to search for lines which contain some word but do not contain a second word. For example, find lines that contain the word "This" but not the word "red" so that for a file containing these ...
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....
16
votes
1answer
2k views

Is it possible to get the matched string after calling :search()

In a vimscript-function, I need to assign a matched string to a variable l:matched after calling search() and I am wondering if there is a shorter way than what I am currently doing: let l:pattern =...
15
votes
1answer
712 views

Ignoring diacritics/accents when searching

Is there a way to instruct Vim that I want to ignore diacritics/accents when searching? For example, I would like to be able to search for kočička by entering /kocicka The ignorecase and smartcase ...
15
votes
4answers
2k views

How can I search for a string between certain line numbers?

This is a problem I had recently. I have a file with several hundred lines of text. How can I search for a string between two specific line numbers? For e.g. I want to search between line numbers 100 ...
14
votes
2answers
7k views

What is the reverse of the 'go to next' command

If I want to go to the next , on the line then I type t,. If I wanted to go to the previous , I could use /n, enter, N but this is a bit of a fiddle. What is the best way to go to reverse the t ...
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 ...
14
votes
1answer
3k views

How to interactively search/grep with VIM?

Is there any way to configure VIM to interactively search inside every file for an entire project? By interactive, I mean updating each keystroke. Something like Emacs counsel-git-grep, see: example ...
13
votes
3answers
1k views

What's the difference between the '\zs' and '\@<=' atoms in Vim regex?

This is what I get from the documentation: \zs "starts the highlighted part" after matching the preceding regex, and \@<= "starts the highlighted part" after matching the preceding atom. But I don'...
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 ...
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) ...
13
votes
1answer
1k views

Is there an incsearch for the replace command?

I really enjoy Vim's incsearch functionality. Is there a way to have it, or something similar to it, working while I do a replacement? For example, if in normal mode I type: :%s/potato/ Can I get ...
12
votes
3answers
1k views

Jump to next occurrence of the word I just changed

If I have the following: function foo(bar) { return bar + 1; } And I use cw to change the first bar to baz, how do I jump to the next matching bar, so that I can use . to make the same change? ...
12
votes
3answers
2k 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: ...
12
votes
1answer
1k views

Why is a Line Feed converted into a Null character inside the search register and into a Carriage Return on the command line?

If I have the following text: foo bar I visually select it and copy it. The text is now stored in the unnamed register " and here is its contents (output of :reg "): "" foo^Jbar^J According to ...
11
votes
7answers
4k views

How to search for lines matching current line in vim

I usually search for words by :/string, but if I have to search entire current line, how to do that. event: 1 beacon: 0x02 //line 1 event: 19 beacon: 0x02 //line 2 event: 1 ...
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 ...
11
votes
1answer
26k views

Search for lines starting with given string in vim?

I have this code snippet: # import ipdb; ipdb.set_trace() def foo(): ... import ipdb; ipdb.set_trace() ... # import ipdb; ipdb.set_trace() How should I specify search query, that it ...
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 ...
10
votes
3answers
301 views

Alternative to escape slashes in search and replace for long strings

Is there any alternative in vim search and replace for strings, than having to escape every special character in string. For example, If I want to match following string and replace with something [...
10
votes
4answers
259 views

Restricting operations to a section of code

There is many times in my day to day work life that I need to operate on a specific function, things like highlighting or searching for a variable, but I have never managed to limit the scope of the ...
10
votes
1answer
1k views

How to substitute in multiple lines?

I'd like to insert a word in multiple lines, is there any command like :[2,3,5,7,11]s/^/word\ /g which would insert word at the beginning of line 2, line 3, line 5, line 7 and line 11? Here, ...
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 ...
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 ...
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 ...
9
votes
2answers
759 views

Duplicate lines beginning with “x”, replacing “x” in the duplicate with “y”?

I have a file with the following pattern: foo 111 baz foo 222 baz foo 333 baz which needs to be turned into foo 111 bar 111 baz foo 222 bar 222 baz foo 333 bar 333 baz So, duplicate all the lines ...
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 ...
9
votes
1answer
1k views

Visual select current search result

I have hlsearch set, but it sometimes lulls me into thinking t hat I have the search term visually selected, and can just hit c to change it. The term searched for may not be an easy number of ...
9
votes
2answers
5k views

Faster way to search for whole words

I can search for whole words like this /\<search term\> \< and \> match left and right word boundaries respectively. But how can I insert those automatically? They are awkward to type ...
9
votes
4answers
895 views

Can I use “gf” (or similar) to open a file and jump to a search term?

Is there a way to get vim's gf command (or something similar) to recognize a +{cmd} argument for file-paths? One can start vim and go directly to a search term by doing: $ vim +/coding_effort ~/...
8
votes
4answers
267 views

Use pattern of global ex command found on a line to substitute in another line

In my fortran code I have a lot of blocks like the following subroutine name(arg1,arg2,arg3,...) : end subroutine and I want them to become subroutine name(arg1,arg2,arg3,...) : end subroutine name ...
8
votes
1answer
5k views

find next occurrence of word or line at cursor

For example when editing a big .gitignore file and pasting in a block for a newly added toolset's suggested ignore patterns, I manually looked at each line and typed /\.filetype where filetype is what'...
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 ...
7
votes
6answers
2k 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 \...
7
votes
2answers
511 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 ...

1
2 3 4 5
8