Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get vim find to ignore whitespace?

Tags:

vim

I have XML entries that span two or more lines sometimes, and so I can look for something like this:

something on one line

But if I try the vim command /something on one line and the line is like this:

something on one 
line

then it doesn't find it, because the second text block is actually seen as

something on one^J           line

Which might have something to do with the fact that I'm using a DOS formatted file.

How can I get vim search to ignore whitespace and newlines?

like image 714
Jonathan Avatar asked Nov 18 '13 17:11

Jonathan


People also ask

How do I remove white spaces in vi?

One way to make sure to remove all trailing whitespace in a file is to set an autocmd in your . vimrc file. Every time the user issues a :w command, Vim will automatically remove all trailing whitespace before saving.

How do I ignore a space in Vimdiff?

The correct command line key for diff should be -w , to ignore all whitespace changes.

How do I search for a space in Vim?

Use backslash \ before space in search string.


2 Answers

In vim search, _s means a new line, a space or a tab. So you can try something such as:

/something on one\_s*line

to match the example string you used as example.

like image 119
Lorenzo Marcon Avatar answered Oct 06 '22 19:10

Lorenzo Marcon


Replacing the spaces with \_s\+ manually is cumbersome. The Search for visually selected text topic on the Vim Tips Wiki has a mapping that does this for you for the current visual selection. You can use it like the built-in * mapping, to search (ignoring spaces) for the current selection. Very handy!

like image 20
Ingo Karkat Avatar answered Oct 06 '22 19:10

Ingo Karkat