vim how to search for URL - vim

How would you search for the following string in vim?
http://my.url.com/a/b/c
I've tried (a la Very No Magic)
:/\Vhttp://my.url.com/a/b/c
But it gives me:
E492 not an editor command: /\Vhttp://my.url.com/a/b/c
You would think there'd be a simple way to search a string literally... I'm not too interested in slash escaping every slash, or writing a complicated search, because I have to rapidly search different URLs in a text file.

I'm not sure why you get not an editor command since I don't. The simplest way to search without having to escape slashes is to use ? instead, e.g.
:?http://my.url.com/a/b/c
" or since the : is not necessary
?http://my.url.com/a/b/c
This does search in the other direction, so just keep that in mind

another way to search forward (from the position of your cursor) without escaping is use :s command.
you could do:
:%s#http://my.url.com/a/b/c##n
then press n to navigate matched text forward, N backwards
If you want to know how many matches in the buffer, use gn instead of n
note that, I said "without escaping", I was talking about the slash, if you want to do search precisely, you have to escape the period. .. since in regex, . means any char.

Can also set the search register directly.
:let #/='\Vhttp://my.url.com/a/b/c'
Then you can use n and N like normal.

Use MacVim (or GVim). Open the non-regex GUI search using ⌘f (or ctrlf on Windows). This is the recommended way to do a non-regex search in Vim. GUI Vim has many improvements over terminal vim like this one and I would highly suggest using it full time if you aren't already.

Searching in vim is just /, not :/. You can search for that string escaping only the slashes: /http:\/\/my.url.com\/a\/b\/c

Related

How to replace one character with mutliple characters using search and replace in vim

In vim, I find myself searching for a character and in command mode doing x followed by 10p or 30p or something like that fairly often. I was wondering if there is a regular expression version that I could use in the :s/.../.../ command. \{m} work for matching m counts of of characters in the seach, but its equivalent s/X/X\{10}/gc replaces with the literal stringX{10}.
I've been searching the documentation and haven't found out how to do it yet, if anyone knows of a way I'd appreciate knowing about it.
You can use :h sub-replace-expression like this
%s/X/\=repeat(submatch(0), 10)/gc

is there a way to see the replaced selection after search and replace in vim

When replacing a regex in vim using search and replace in vim, is there a way to see the result before moving onto the next replace.
The interactive option shows the string matching the regex before replace, but with a complex regex it might or might not work as expected so.
If theres a plugin or hack or default option to achieve this i would really like to know.
The traces.vim plugin provides range, pattern and substitute preview for Vim.
Neovim has a native method to do substitutions previews. I believe it is the 'inccommand' option.

Substitute in vim, with special characters

I am trying to use substitute command in vim to enclose all occurences of a particular pattern
\cite{author1}
\cite{author2}
with
(\cite{author1})
(\cite{author2})
Based on other answers in stack exchangeI used the following vim command
%s/\\cite{(\w\+)}/(\\cite{\1})/g
But, no luck. It says "no matches found". I put two back slashes, one of which is supposed to be the escape character. Kindly help.
I know I could use some other editor and finish the job, but I want to know my mistake. Thank you.
please escape ()
%s/\\cite{\(\w\+\)}/(\\cite{\1})/g
You do not need a capture group to get the entire match. You can use \0 or & for the whole match in the replacement portion of your substitution.
:%s/\\cite{\w\+}/(&)/g
If you do want to use a capture group, then you need to escape the capture group/parenthesis.
:%s/\(foo\)/(\1)/g
For more help see:
:h :s%
:h /magic
As mentioned, normally you need escape the parentheses.
You can use very magic mode (\v) to make the regex simpler; removing the need to escape lots of the regex parts, including the parentheses for capturing:
%s/\v(\\cite\{\w+\})/(\1)/g
Knowing what every sign in a regular expression does in vim is sometimes very
difficult, especially for all the modes and configuration variables that this
depends on. For that reason, it is very important to have visual feedback about
what text are we really matching in any moment while crafting regular
expressions.
If you work with neovim, there is a special option called inccommand that you
can enable to live preview the matches and substitution. That way you can figure
out you errors more quickly. This feature is very likely to be included also in
vim in the future, but independently of that, you can also use simple vim to
give you visual feedback if you enable the option incsearch.
With incsearch set, you can watch the matches of / and ? while you write them
just to be sure that the pattern is correct. Later you can exploit a nice
feature from the substitution: if you leave the first section empty, the last
search will be used.
So you could first make the search:
/\\cite{\w\+}/(&)/g
In the meantime, vim will be showing you the matched text visually. Once you
are sure that the pattern is correct press enter, and finally type:
:%s//(\1)<Enter>
Also, in case you want something more powerful than this simple hack, you can go
for my plugin Extend.vim, that can
do that and much more with great visual feedback.

Is there an alternative to escaping '/' in vi editor when searching for strings with '/' in them?

I frequently find myself searching for paths such as /a/b/c/file.txt in files (log files, scripts, etc.), using vi editor. This is usually on some server machine, to which I connect using SSH, so I'm looking for solutions which don't require a GUI editor.
It's painful to have to escape all those /s every time I want to search for string such as a file path.
Is there any alternative to using / while searching in vi? Or, is there some setting that will allow me to set this search character to something else? (even if it sets it only for the active session)
I know I can use grep, but a solution in vi would be nice.
You can use the ? character instead of /, But the only difference is /will do a forward search from top to bottom where as ? will search from last to first line.
You can use another delimiter, such as #:
:s#search#replace#g
Test
Given a file with this content:
hello this is me/you
I type:
:s#me/you#otherthing#g
And now the text is:
hello this is otherthing
You could try :cmap <C-/> \/

Get the last search or search&replace string back in vim?

How can I bring back the last string I used for a search or a search&replace?
For example, assume that I enter :%s/some_text/some_other_text/gc and vim gives me the E486: Patterns not found: some_text error message back. I then realize that I actually meant to write some_magic_text instead of some_text. At that point, how can I get back my original string in the bottom command row (or whatever it is called) so I can change it and do a second search? Is there a nifty little command for that?
In this brief example it looks unnecessary, but when the text you are looking to replace is mighty long and you just typed one letter wrong, it is fantastically annoying to have to retype everything.
And I am using MacVim if that makes any difference.
From the normal mode, hit q/ to navigate through your search history!
Check out this vimvcast which explains what you want.
More generally, you can recall any command you have previously typed by entering the first few characters, and then use arrow multiple times to navigate in history.
In your case, you could type:
:%s<Up>
See :help history
This answer might be good an improvement to what you are after, after all.
Use search with highlighting, to interactively check if the regex you are crafting is definitely working, and then use it in a search-replace.
:se is (incsearch, better put se is in your .vimrc)
/<search term>
check with n/N if you are happy with the matches
:s%//<replace term>/g
When omitting the <search term> in the search-replace in 4., the last used search will be used.
For acessing the list of last (search-replace) commands use q:, or as already noted q/ for the list of last search terms.
Bonus:
When using :se gd, s/<search>/<replace> will behave as s/<search>/<replace>/g.
Accessing just the first search match in each line can then still be done with adding /g, so essentially both behaviours are just switched.
/ and then up to bring up the last search query.

Resources