How can I search a string in vim and thereby ignoring any whitespace (or particularly line breaks) between any characters?

And probably thereby making a function that can search ignoring all whitespace.

I found a way to ignore whitespace in regex. No really elegant. Just insert \s* between any character. So therefor making this a function would be really needed.

I also have found a way to search around line breaks in vim:
vim search around line breaks

However the last link gives a solution only if the line break is there were a space was. However I want to ignore all whitespace.

So when I search helloworld here:

blabla blalba hell  
oworld bla bla h  
elloworl bla bla  

It should match it twice, despite the line breaks.

I thought I basically need to alter the function from the vim link a bit which changes the search to:

h\n?e\n?l\n?l\n?o\n? \n?w\n?o\n?r\n?l\n?d  


h\s_?e\s_?l\s_?l\s_?o\s_? \s_?w\s_?o\s_?r\s_?l\s_?d

But I have not idea how to do alter the function for that.

2 Answers 2


The Search for visually selected text topic on the Vim Tips Wiki has a mapping that searches for the current visual selection, irrespective the amount and type of whitespace in between the words. You can use it like the built-in * mapping, to search (ignoring spaces) for the current selection. Very handy!

However, you want even more indifference to whitespace, allowing line breaks (and other white space?) at any position in the text. That's possible as well. You can adapt the current search pattern (stored in register /, accessible from Vimscript via @/) with this command:

:let @/ = join(split(@/, '\zs'), '\_s*')

The split() first cuts the current (literal) search into a List of individual characters (so it won't work properly with existing regular expression stuff like \+ or \|!), then join()s it back together with \_s* (matching any amount of whitespace), and assigns it back to the search register.

You can either build a mapping from that (:nnoremap <Leader>/ :let ...<CR>), or incorporate this into the visual mode mapping mentioned above.


This solution https://stackoverflow.com/questions/20053907/how-can-i-get-vim-find-to-ignore-whitespace points to a visual solution that may work for your case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .