vim short for search replace with space in between - vim

What is the vim short for this case?
A BCD_x 1.pdf A BCD_x 2.pdf
Desired output:
A BCD_x 1.pdf:::A BCD_x 2.pdf

You mean something like this?
:s/\s\(A\s\)/:::\1
Or how about this?
:s/\sA\#=/:::
Of course, as always with regexp search-and-replace everything depends on the particulars of your data. Of which you haven't given us much to work with.

Related

How to grep text for small mistakes

Using standard Unix tools how can I search in a text file or output for a word with maybe 1-2 letters transposed or missed?
For example my input
function addtion(number, increment)
return number+increment
end
function additoin(number, increment)
return number+increment
end
I would like to search for addition and match addtion and additoin in my input and tell me about it. Because it's code, checking against dictionary is out of the question.
Currently cat file.txt | grep "addition" will simply yield me nothing.
You can play around with the agrep command. It can perform fuzzy, approximate matches.
The following command worked for me:
agrep -2 addition file
You can't do a fuzzy match with standard grep, but if there are specific misspelling you're interested in, you could construct a regular expression that matches those.
For example:
grep add[it]*on
matches the example misspelling you gave. But that's probably not general enough for your purposes.
A better approach is likely going to be to use some sort of static analysis tool specific to the language the code is in. It might not give you the right spelling, but should be able to tell you where the function name and calls to the function use different spellings.
Try the spell command. Note: You might need a dictionary (usually aspell-en in your distro's repositories).
As the answer says, you should definitely try agrep. In addition, there is a newer and much faster alternative ugrep for fuzzy search. Use -Z2 to allow up to 2 errors:
ugrep -Z2 addition file.txt
An insertion, deletion, or substitution is one error. A transposition (as in additoin) counts as two errors, i.e. two substitutions. Use option -i for case-insensitive search and -w to match whole words.
Try this on linux terminal:
grep -rnw "text" ./

GVIM Search and replace for rational numbers

I have numbers in the shape of (a/b) where a and b are integers. I would like to replace them with something like rat(a,b). Is that possible?
I would do:
%s#\v(\d+)/(\d+)#rat(\1,\2)#g
Vim is good at Search and Replace:
:%s/(\(\d\+\)\/\(\d\+\))/rat(\1,\2)/g
Too much backslashs! Yet another command:
:%s#\v\((\d+)/(\d+)\)#rat(\1,\2)#g
We can use # to separate patterns instead of /.
The very magic \v makes life easier.
I suggest you type :help :s to learn more.
The easier way to do this is probably to create a macro and replay it as many times as you need to.
press on qa then make the search & replace stuff
press q when done
then play the macro with #a
If you know regex, then you can go to Search patterns http://vim.wikia.com/wiki/Search_patterns

Is there a way to get a snippet to work immediately after a word?

Whenever I try to use a snippet (using snipMate) after a word, without a space, it does not work. So I have to hit space, type my snippet, hit tab, and then eliminate the space. Is there a better way of doing this? Is there a way to get the snipppets to work even immediately after a word? Here is what I mean:
let us say my snippet is this:
snippet test
<some code>${1}</code>${2}
typical use:
hello test[TAB]
turns into this:
hello <some code>|</code>
but if I try this:
hellotest[TAB]
it turns into this:
hellotest_____
the _ being white space. Is there a way to fix this?
Vim abbreviations can be of three types (full-id, end-id, and non-id, cp. :help abbreviations), which help solve this problem. snipMate, however, allows all non-whitespace characters for snippet names, and therefore has to rely on whitespace for separation.
You have to modify the parsing of the snippet name, in plugin/snipMate.vim, it's in the function TriggerSnippet():
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
There's no setting to that effect if that's what you ask. You will have to look at the source and do the change there yourself, I'm afraid.
Also, it can probably seen as a limitation but it's definetely not a bug so what you are after is an improvement, not a "fix". My advice, though, is to use it as it was designed: having triggers work even if they are part of another word makes no sense at all. Spaces are the most natural way of separating ideas and words.

Vim - How to move the result of a search to the beginning of the file?

I want to search some text and move the entire line where the text belongs to the beginning of the file. Just that.
How about the simple move command?
:g/^C/m0
:g/^B/m0
:g/^A/m0
:g/regex/norm dd1Gp
Well, what I'm gonna suggest is a primitive answer as primitive it can get. But nothing else springs to mind currently.
:g/A ... some text not including A, B or C.../d
(will tell you how many lines it has yanked)
and then you go to the beginning of the file and, for example
5P
Although, if cases are as simple as this, maybe sorting lines by first letter .... I've never done anything similar but look for older questions.

Advanced searching in Vim

Is there a way to search for multiple strings simultaneously in Vim? I recall reading somewhere that it was possible but somehow forgot the technique.
So for example, I have a text file and I want to search for "foo" and "bar" simultaneously (not necessarily as a single string, can be in different lines altogether).
How do I achieve that?
/^joe.*fred.*bill/ : find joe AND fred AND Bill (Joe at start of line)
/fred\|joe : Search for FRED OR JOE
Actually I found the answer soon after I posted this (yes I did google earlier but was unable to locate it. Probably was just searching wrong)
The right solution is
/(foo\|bar)
#Paul Betts: The pipe has to be escaped
Vim supports regular expressions by starting in command mode with a '/'.
So using something like "/(foo\|bar)" (as was stated before) would solve the problem. It's good to know why that works and what you are using (regular expressions).
/(foo|bar)

Resources