specific pattern search in gvim - search

I need to do a case sensitive search in gvim.
The pattern that I want to search looks something like:
For ex:
tABCD_EFGH_IJKL
Here the first alphabet 't' is a permanent character, after which the word starts with a capital letter (any alphabet).
Please help.

Try this regex:
t[A-Z]{4}_[A-Z]{4}_[A-Z]{4}
To understand it, let's break it down.
t "t"
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
_ Underscore
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
_ Underscore
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
To search with a regex in vim, hit / (forward slash) in Insert mode, type the pattern, then press Enter.

Related

Mark words in notepad++ including dash (-)

I would like to mark in Notepad++ the sql scripts in a text log. The sql files have this format in the text:
AAAAAAAA.BBBBBBBBBBB.sql
So what I execute is this sentence in search menu:
\w*.sql
As I should get BBBBBBBBBBB.sql. The point is that in some script names there are dashes (-), and when that happens I dont get the whole name, but just the end after the last dash.
For example, in:
AAAAAAAA.BBBBB-CCCCCCC.sql
I would like to get BBBBB-CCCCCCC.sql, but I just get CCCCCCC.sql
Is there any possible formula to get them?
If the match can not start and end with a hyphen:
\w+(?:-\w+)*\.sql
\w+ Match 1+ word characters
(?:-\w+)* Optionally match - and 1+ word characters
\.sql Match .sql
See a regex demo.
Note that in your pattern the \w* can also match 0 occurrences and that the . can match any character if it is not escaped.
Another option could be using a character class to match either - or a word character, but this would also allow to mix and match like --a--.sql
[\w-]+\.sql
See another regex demo.

^[:blank:] does not match dot in sed

I have an input as follows:
INa.aa................... October 2010 after its previous U.S.-based owners failed to pay debts
My goal is to put brackets around every word starting with letter i/I. So I issued a command:
sed 's/\<i[^[:blank:]]*\>/(&)/gi' input_data
Which returned this output:
(INa.aa)................... October 2010 after (its) previous U.S.-based owners failed to pay debts
What I don't get is, why doesn't the ^[:blank:]* also include the dots after INa.aa?
Thank you for any suggestions.
You use the \> "end of word" escape. A word boundary is defined as
the character to the left is a "word" character and the character to the right is a "non-word" character, or vice-versa
in the manual (referring to \b). In the case of \>, the "vice-versa" does not apply.
What is a "word" character?
A "word" character is any letter or digit or the underscore character.
And "non-word" are all the others. You expect the boundary between your periods and a blank to match \>, but it doesn't: both the period and the blank are non-word characters. The word boundary is between the last a and the first ..
The period between the as is also surrounded by word boundaries, but because there aren't any blanks involved, it's a part of the match.
If you want to match everything up to the next blank, you can just skip the \> in your regex.

.htaccess mod-rewrite how to fetch a non-word character

In .htaccess file, \w matches a word, i.e., numbers or letters. But it does not matches special characters like -. So, what escape character matches these characters ?
To match - with word character \w you will need to use character class:
[\w-]

Vim: word vs WORD

I'm learning Vim and can't wrap my head around the difference between word and WORD.
I got the following from the Vim manual.
A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space
(spaces, tabs, ). This can be changed with the 'iskeyword'
option. An empty line is also considered to be a word.
A WORD consists of a sequence of non-blank characters, separated with
white space. An empty line is also considered to be a WORD.
I feel word and WORD are just the same thing. They are both a sequence of non-blank chars separated with white spaces. An empty line can be considered as both word and WORD.
Question:
What's the difference between them?
And why/when would someone use WORD over word?
I've already done Google and SO search, but their search-engine interpret WORD as just word so it's like I'm searching for Vim word vs word and of course won't find anything useful.
A WORD is always delimited by whitespace.
A word is delimited by non-keyword characters, which are configurable. Whitespace characters aren't keywords, and usually other characters (like ()[],-) aren't, neither. Therefore, a word usually is smaller than a WORD; the word-navigation is more fine-grained.
Example
This "stuff" is not-so difficult!
wwww wwwww ww www ww wwwwwwwww " (key)words, delimiters are non-keywords: "-! and whitespace
WWWW WWWWWWW WW WWWWWW WWWWWWWWWW " WORDS, delimiters are whitespace only
To supplement the previous answers... I visualise it like this; WORD is bigger than word, it encompasses more...
If I do viw ("select inner word") while my cursor is on app in the following line, it selects app:
app/views/layouts/admin.blade.php
If I do viW (WORD) while my cursor is at the same place, it selects the whole sequence of characters. A WORD includes characters that words, which are like English words, do not, such as asterisks, slashes, parentheses, brackets, etc.
According to Vim documentation ( :h 03.1 )
A word ends at a non-word character, such as a ".", "-" or ")".
A WORD ends strictly with a white-space. This may not be a word in normal sense, hence the uppercase.
eg.
ge b w e
<- <- ---> --->
This is-a line, with special/separated/words (and some more). ~
<----- <----- --------------------> ----->
gE B W E
If your cursor is at m (of more above)
a word would mean 'more' (i.e delimited by ')' non-word character)
whereas a WORD would mean 'more).' (i.e. delimited by white-space only)
similarly, If your cursor is at p (of special)
a word would mean 'special'
whereas a WORD would mean 'special/separated/words'
That's a grammar problem while understanding the definition of "word".
I get stuck at first in Chinese version of this definition (could be miss-translation).
The definition is definitely correct, but it should be read like that:
A word consists of:
[(a sequence of letters,digits and underscores),
or (a sequence of other non-blank characters)],
separated with white space (spaces, tabs, <EOL>).
Whitespace characters were only needed when delimiting two same types of 'word'
More examples in brackets as follow:
(example^&$%^Example) three "word" :(example), (^&$%^) and (Example)
(^&^&^^ &&^&^) two "word" : (^&^&^^) and (&&^&^)
(we're in stackoverflow) five "word" :(we), ('), (re), (in) and (stackoverflow)
Another way to say it. If ur coding, and want to move thru the line stopping at delimiters and things line that "() . [] , :" use w.
if you want to bypass those and just jump to words lets say like a novel or short story has, use W.
For coding the small w is probably the one used most often. Depends where you are in the code.

Vim search and replace: change position of characters

I have strings like (i,1) and (i,2) in my text.
I want so search and replace them so i get
(1,i) and (2,i) respectively. How do I go about that in Vim?
:%s/(\([^,]\+\),\([^)]\+\))/(\2,\1)/g
Explanation:
: gets you into command mode
% apply the following command to each line
s the command is substitute. It consists of two parts, the pattern and the replacement. These are
seperated by /
/ Seperator for pattern. The pattern is (\([^,]\+\),\([^)]\))
( You want to match an opening paranthesis
\( store the following matching part in \1.
[^,] match anything except ,
\+ match at least one (of the not ,)
\) end the matching part for \1
, match the comma (don't store anywhere)
\( same as above, but store in \2
[^)] match anything except )
\+ match at least one (of the not ))
\) as above
) match the closing paranthesis (don't store anywhere)
/ Seperator for replacement. The replacement is (\2,\1)
( literal insertion of (
\2 insert 2nd match of pattern
, insert literal ,
\1 insert 1st match of pattern
) insert literal )
/ Seperator to indicate the end of replacement.
g so that the pattern is replaced multiple time on one line (necessary only if a line contains (bbb,ccc) multiple times)
this command does it:
%s/(\zs\([^,]*\),\([^)]*\)\ze)/\2,\1/g
or
%s/\v\(([^,]*),([^)]*)\)/(\2,\1)/g
brief explanation:
\v : very magic, to save some escapes
\( : match openning "("
([^,]*) : group 1 the text between "(" and ","
, : the comma
([^)]*) : group2 , the text between the "," and ")"
\) : ending ")"
replacement part: (\2,\1) (swap the two groups)
If you want a no-brainer, choose a macro (:help q) instead of search and replace (:help :s). I often go for macros if the search and replace patterns get complicated (e. g., they take more than one or two minutes to write). Macros are easy and intuitive, you perform your change once and simply repeat it. Solution for your problem using macros:
qq/(i,[12])<CR><SPACE>xpxp<BACKSPACE><BACKSPACE>xpq1000#q
That's 29 very easy keystrokes.
qq Record following keystrokes in register q
/(i,[12])<CR> Search for (i,1) or (i,2)
<SPACE>xpxp... Your text manipulation
q Stop recording
1000#q Replay contents from register q 1000x
Don't worry, if one of the commands fail (in this case the search for (i,1)) repetition stops immediately. Of course you can also do it the safe way:
#q Replay the keystrokes from register q (once)
## Repeat the last replay (that's easier than #q)
So you'd type something like #q######## to make the changes and check them.

Resources