Hyellooo! I am working on a project and hit a road block.
I am looking to find a phrase "write true" in a text using grep, which is super easy, but my problem is I need to only italicize the word "true" and not "write".
The reason I need to find it only when it follows the word "write" is because those are the only times that it needs to be italics, any other times it appears it will just stay normal.
So I can find "write true" but then the grep italicizes both of the words. I need to find both words but protect the word "write" from being italic. Does this make sense? Hmm. Thanks for any help!
Try (?<=write )true
Positive Lookbehind (?<=write )true to find and modify all 'true' after 'write ' ('write ' will stay intact).
Positive Lookahead write(?= true) to find and modify all 'write' before ' true' (' true' will stay intact)
There are Negative Lookbehind (?!<=) and Negative Lookahed (?!=) as well. Just to keep us from getting bored. They search the word if it has no another word behind or ahead.
Related
I have data in sublime arranged as follows;
353154 0.001039699782
506472 0.02085443362
482346 0.08343533852
439791 0.001349253676
486087 0.9999999476
I am trying to put quotes and commas so as to get following output;
('353154', '0.001039699782'),
('506472', '0.02085443362'),
('482346', '0.08343533852'),
('439791', '0.001349253676'),
('486087', '0.9999999476')
I am aware of using CMD+Shift+L in order to move cursors right and left. But I need help on how to get the commas and quotes between the two columns. Kindly advise.
You can do this with regex search and replace. Select Find → Replace… and make sure the Regular Expression button (usually looking like this *) is selected. In the Find: field, enter
([\d\.]+)\s+([\d\.]+)
and in the Replace: field, enter
('\1', '\2'),
Hit Replace All and it will look like this
See the demo at regex101.com, which also explains what each of the capture and replacement groups do.
Please keep in mind that you'll have to delete the final comma , if that's not proper syntax for your language of choice.
I'm trying to replace a few character word in one place with the word searched in another. I.e
VARIABLE INT005 SOME TEXT BETWEEN NAME=INT020;
I want the program To copy whats after VARIABLE (INT005 in this case) and paste it after NAME=(here should be again INT005 replaced into the place of INT020)
A regex replacement should work here. Assuming you are only looking to make these replacements on a single line, you may try the following find and replace:
Find: \bVARIABLE (\S+)(.*?)\bNAME=\S+;
Replace: VARIABLE $1$2NAME=$1
Demo
Edit:
If your text could span multiple lines, then either turn on "dot all" mode from Textpad (not sure where you would do that), or use this find version:
\bVARIABLE (\S+)([\s\S]*?)\bNAME=\S+;
I can search the following without problems:
_GET
$variable
However, sublime fails to search $_ (p.e. $_GET.) I have tried to escape it somehow:
$\_GET
\$_GET
$__GET
I'm on Ubuntu 14.04LTS
Turn off the regular expressions search. It is the button on the far left of the search field (in this picture currently selected):
With regular expressions turned off:
Although I'm not sure if this would fit your exact problem since you tried escaping using \$_, this answer may still help for posterity.
Did you also make sure "whole word" search is turned off? That's the 3rd button from the left (next to the Aa)
With whole word turned on:
Failing with the attempted escaped \$_:
And it succeeding with _GET:
Note that whole word search of $_ would succeed if there was a whole $_ phrase, surrounded by whitespace. For example with whole word search on:
I am a sentence with the keyword $_ which will be matched.
would work, whereas:
I am a sentence with the keyword $_GET, which will never match. $_POST, $_REQUEST, and $_SERVER won't work either.
would break the whole word search.
I'm getting more and more comfortable with Vim after a few months.
BUT, there is only one simple feature I can't get any answer from the web. That is "Search and replace the results". The problem is that I know:
:/keyword to search, and hit enter "keyword" will be highlighted (of course with set hlsearch)
n, or N to navigate
:% s/keyword/new_keyword/g to replace all occurences of keyword with new_keyword.
BUT, I would think that there must be a way to search, and replace the matched keyword (highlighted) with any new_keyword WITHOUT doing ":% s/keyword/new_keyword/g", which is a lot of typing considering search & replace is such a day-to-day feature.
Any answers/comments will be greatly appreciated!
If you've already done a search you can do a substitution for the same pattern by simply leaving out the pattern in the substitute command. eg:
/keyword
searchs for "keyword", and then:
:%s//new_keyword/g
will replace all occurrences of "keyword" with "new_keyword".
Searching and using the dot command (you didn't meantion you are using the dot command, that's why I highlight it) to repeat the last input action is my best bet here.
I use s///g for search and replace.
Well, since #keyword# and #new_keyword# account for most of the characters, and you need some way to differentiate between them (i.e., a character in vim, or tab between entry fields in dialog in a different editor), you're left with maybe four or five keystrokes beyond that.
So I think you're probably overestimating number of keystrokes and also forgetting that (1) it becomes very natural, and (2) working this way allows you also to naturally modify the action performed by specifying a different range or option flag.
But you can cut down on keystrokes. If you want you can map a key to automatically bring up the command line with '%s/' already in place. e.g.:
nmap s :%s/
The command above would remap 's' (I'm not recommending remapping to that key, but it gives the idea) and set you up to insert the keyword.
Also, you can set the 'gdefault' option to default to substituting multiple times per line. This lets you skip the ending '/g' in your keystrokes:
set gdefault
See ':h gdefault' for help section on that option.
In the end I would say just get used to the default way it works, because using it that way allows you to keep same basic operation when you want to specify different ranges or option flags, and creating a new special map is just another thing to remember. gdefault may be worth setting if you think you're going to want it majority of time, adding /g flag at end when gdefault is set has effect of turning /g off. . .
Move to the first highlighted word then record a macro for replacing the word and moving to the next one, e.g:
gg
n
qq
caw new_word^[
n
q
#q
##
##
...
I want to search for a string and find the number of occurrences in a file using the vi editor.
THE way is
:%s/pattern//gn
You need the n flag. To count words use:
:%s/\i\+/&/gn
and a particular word:
:%s/the/&/gn
See count-items documentation section.
If you simply type in:
%s/pattern/pattern/g
then the status line will give you the number of matches in vi as well.
:%s/string/string/g
will give the answer.
(similar as Gustavo said, but additionally: )
For any previously search, you can do simply:
:%s///gn
A pattern is not needed, because it is already in the search-register (#/).
"%" - do s/ in the whole file
"g" - search global (with multiple hits in one line)
"n" - prevents any replacement of s/ -- nothing is deleted! nothing must be undone!
(see: :help s_flag for more informations)
(This way, it works perfectly with "Search for visually selected text", as described in vim-wikia tip171)
:g/xxxx/d
This will delete all the lines with pattern, and report how many deleted. Undo to get them back after.
Short answer:
:%s/string-to-be-searched//gn
For learning:
There are 3 modes in VI editor as below
: you are entering from Command to Command-line mode. Now, whatever you write after : is on CLI(Command Line Interface)
%s specifies all lines. Specifying the range as % means do substitution in the entire file. Syntax for all occurrences substitution is :%s/old-text/new-text/g
g specifies all occurrences in the line. With the g flag , you can make the whole line to be substituted. If this g flag is not used then only first occurrence in the line only will be substituted.
n specifies to output number of occurrences
//double slash represents omission of replacement text. Because we just want to find.
Once got the number of occurrences, you can Press N Key to see occurrences one-by-one.
For finding and counting in particular range of line number 1 to 10:
:1,10s/hello//gn
Please note, % for whole file is repleaced by , separated line numbers.
For finding and replacing in particular range of line number 1 to 10:
:1,10s/helo/hello/gn
use
:%s/pattern/\0/g
when pattern string is too long and you don't like to type it all again.
I suggest doing:
Search either with * to do a "bounded search" for what's under the cursor, or do a standard /pattern search.
Use :%s///gn to get the number of occurrences. Or you can use :%s///n to get the number of lines with occurrences.
** I really with I could find a plug-in that would giving messaging of "match N of N1 on N2 lines" with every search, but alas.
Note:
Don't be confused by the tricky wording of the output. The former command might give you something like 4 matches on 3 lines where the latter might give you 3 matches on 3 lines. While technically accurate, the latter is misleading and should say '3 lines match'. So, as you can see, there really is never any need to use the latter ('n' only) form. You get the same info, more clearly, and more by using the 'gn' form.