search for string in vi - search

I want to search for this:
SELECT * FROM `influencers` WHERE (author_name =
within a log file using vi, I cant figure out how to properly escape this, I have tried:
SELECT * FROM \`influencers\` WHERE \(author_name =
And several similar versions, but no luck

In vim, the only character you need to escape is the *:
/SELECT \* FROM `influencers` WHERE (author_name =
If you're using a different vi variant than vim, you'll need to tell us what you're using.

This should work:
SELECT \* FROM `influencers` WHERE [(]author_name =
EDIT: Seeing Keith's answer, he's right. My square brackets are unnecessary. But I'll leave my answer to make a point: whenever I have regex problems, wrapping questionable characters in square brackets is often a quick fix (and doesn't hurt).

Related

How do I find and replace string of # (pound) characters in vim?

I have a file where I have comments like ######### Utility Functions #######. I am trying to clean it up and make it something like # Utility Functions # which requires me to match a string of more than one pound signs and replace it with a single pound sign.
I have tried the following
:%s/#+/#/gc
:%s/"#"+/#/gc
:%s/\#+/#/gc
:%s/(#)+/#/gc
and a bunch more. As I understand # has a special significance to match multiple characters like * but in reverse direction. This would mean that I would have to escape it somehow to achieve the above. Any help appreciated.
try that: :%s/\v#{2,}/#/g
#{2,} " 2, means at least two
There is a shortcut for "more than one" in most regex flavours: + so
:%s/\v#+/#/g
works too, if you use "very magic" (\v), see :h magic.
No, # has no special meaning, and + as you use it is just a regular +. You need to escape the + to make it a "multi":
:%s/#\+/#/g
See :help multi.
You can try using quotes.
sed 's/"#"//g'

vim how to search for URL

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

Vim replacing linefeeds

I resisted Vim, but have now given in. It works large files like a hot knife through butter.
Situation: I have a large text file, I want to put a pipe character at the beginning and ending of each line.
Problem: These Vims and other variations didn't work:
:%s/$/|\$|
:%s/\r/|\r|
:%s/$/|\r|
I suspect it's something simple to fix this, but searching Google and Stack didn't help.
You nearly had it:
:%s/^\|$/|/g
^\|$ means beginning or end of line. In a Vim regex, the | "or" pipe gets escaped. That is followed by /|/g -- replace with | globally.
Personally, I'd prefer the expressiveness of 'surround each line with pipe chars':
:%s/.*/|&|
This, in my brain, is more intuitive than 'replace a non-existing, imaginary character at the start or end of a line'.
It also depends on fewer magic chars (^,$, quoted \| which are all clumsy to type and error prone. (I mean, do you remember to quote the regex |? Do you know the difference between $ and \_$? Did you know some, if not all, of these depend on the 'magic configuration' of vim?)).
The above suffers from none of those.

How to change \u7f51 to common character in vim?

In ruby, since it's utf-8 encoded in the system wide. so it's pretty straight forward:
string = "\u7f51\u5740\u4e0d\u80fd\u4e3a\u7a7a"
=>网址不能为空
Anyone can I tell me that how to do this in vim?
If you are wanting to insert Unicode characters, see the utf-8-typing help.
For example, Ctrl-V u7f51 will insert 网.
If you are wanting it in Vim scripts, you can use Unicode escapes in the same way as in Ruby; the command echo "\u7f51\u5740\u4e0d\u80fd\u4e3a\u7a7a" will echo the string you provide in your question.
Being aware of this, Ctrl-R comes into play, with the " and = registers; see the docs for i_CTRL-R and i_CTRL-R_= for more info.
Given the following, with the cursor inside the double-quoted string:
string = "\u7f51\u5740\u4e0d\u80fd\u4e3a\u7a7a"
Apply these keystrokes: ci"Ctrl-R="Ctrl-R""EnterEsc
This will turn it into the following:
string = "网址不能为空"
Try to figure out how it works yourself, but I'll explain more if you can't figure it out.

In vim, is there a plugin to use % to match the corresponding double quote (")?

The % key is one of the best features of vim: it lets you jump from { to }, [ to ], and so on.
However, it does not work by default with quotes: Either " or ', probably because the opening and closing quote are the same character, making implementation more difficult.
Thinking a bit more about the problem, I'm convinced that it should be implemented, by counting if the number of preceding quotes is odd or even and jumping to the previous or next quote, accordingly.
Before I try to implement it myself, I'd just like to know if someone already has?
Depending on your reason for needing this, there may be a better way to accomplish what you're looking for. For example, if you have the following code:
foo(bar, "baz quux")
^
and your cursor happens to be at the ^, and you want to replace everything inside the quotes with something else, use ci". This uses the Vim "text objects" to change (c) everything inside (i) the quotes (") and puts you in insert mode like this:
foo(bar, "")
^
Then you can start typing the replacement text. There are many other text objects that are really useful for this kind of shortcut. Learn (and use) one new Vim command per week, and you'll be an expert in no time!
Greg's answer was very useful but i also like the 'f' and 'F' commands that move the cursor forward and backward to the character you press after the command.
So press f" to move to the next " character and F" to move to the previous one.
I have found this technique very useful for going to the start/end of a very long quoted string.
when cursor is inside the string, visually select the whole string using vi" or vi'
go to start/end of the string by pressing o
press escape to exit visual select mode
this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.
Edit
Adding Stefan's excellent comment here which is a better option for anyone who may miss the comment.
If you use va" (and va') then it will actually visually select the quotes itself as well.
– Stefan van den Akker
I'd like to expand on Greg's answer, and introduce the surround.vim plugin.
Suppose that rather than editing the contents of your quotes, you want to modify the " characters themselves. Lets say you want to change from double-quotes to single-quotes.
foo(bar, "baz quux")
^
The surround plugin allows you to change this to
foo(bar, 'baz quux')
^
just by executing the following: cs"' (which reads: "change the surrounding double-quotes to single-quotes").
You could also delete the quote marks simply by running: ds" (which reads: "delete the surrounding double-quotes).
There is a good introduction to the surround plugin here.
I know this question is old but here is a plugin to use % to match the corresponding double quote:
https://github.com/airblade/vim-matchquote

Resources