Find a URL in a Document While Using Vim


https://example.org/


The forward slashes in a URL affect the search operator in Vim. And you won't be able to match the entire URL without using escape sequences.


Below, I discuss different ways to search for a URL in Vim.


Use Escape Sequences


The proper way to match a URL is to escape any characters which have special meaning using a backslash \ In our case, we escape the period \. and the forward slash \/


Press the forward slash to begin a search in Vim. Then type your search pattern. Then press the Enter key.


So, if we are searching for this URL:


https://example.org/


Then we type these characters into Vim from Normal mode:


/https:\/\/example\.org\/


Escaping always works.


Here, we escape the period . which is a wildcard within regular expressions.


And we escape the forward slash / which is the delimiter for the search command /


Which characters you have to escape depends on the current "magic" setting for Vim. See References below.



One shortcut you can take is that if your URL contains a unique string of characters then you can search for that string and ignore the rest of the URL.


https://example.org/unique_path


/unique_path



If your URL is relatively simple then you can use the search backward command which begins with the question mark.


In this case, use N to search forward and n to search backward.


https://example.org/


?https://example\.org/


If you feel comfortable dropping the literal period in the URL then you can also drop the escape sequence for the period. Just keep in mind that will now match any character between the "example" and "org".


https://example.org/

https://exampleAorg/

https://exampleBorg/

https://exampleCorg/


?https://example.org/


Use Wildcards for All Symbols


A quick way to search for a URL is to replace any special characters with periods.


The periods in the pattern below are wildcards. And that means any character could occupy those places. But the most likely outcome is that the pattern will only match the URL you are looking for.


https://example.org/


/https...example.org.



If you have a more complicated URL, you can set the search register directly.


Here we use \V in front of the pattern and single quotes around the pattern.


This saves you from almost every escape sequence.


http://example.org/path?q=query


:let @/='\Vhttp://example.org/path?q=query'


What to do About Backslashes


In Vim, you always have to escape backslashes while searching.


In this case, just double up any backslashes. And escape any other characters depending on which search strategy you are using.


Note dollar sign $ is a special character in Vim.


\\fileshare\c$\windows\mspaint.exe


/\\\\fileshare\\c\$\\windows\\mspaint\.exe

..fileshare.c..windows.mspaint.exe

:let @/='\V\\\\fileshare\\c$\\windows\\mspaint.exe'


For clarity, that register string begins with:


'-\-V-\-\-\-\-f-i-l-e-s-h-a-r-e
  \ V 1 2 3 4 fileshare


References


Magic | Vim Help

\. | Vim Help

Search Commands | Vim Help

Vim Modes | Vim Help

Set the search register directly | Peter Rincker | Stack Overflow

:let @/= Syntax | Vim Help

Single Quotes | Vim Help

Pattern Delimiter | Vim Help


Created: Saturday, October 8, 2022

Updated: Saturday, October 8, 2022




/gemlog/