Is there an easy way in Gvim to search for more than one search item in a file simultaneously ? i.e I want Gvim to highlight Word1 and Word2 in a given file or RegExp1 and RegExp2 in a given file at the same time.
Use the "or" regex operator: /Word1\|Word2
Related
I have multiple text files I need to extract a viable amount of characters between two specific words, "".
Can someone give me an example grep pattern that will find all characters of any kind, including spaces, between these two words so I can then replace with a blank space? Thank you.
I don't have any example code I can put in my question, I am using a text editing program and I would like to find all the text between two unique words in the file and delete it, the text editing program allows the use of grep patterns.
You can use below grep line to search pattern like word1 something and other things and then word2
grep -o -E "word1(\b).*word2(\b)" file.txt
As you may notice this command's output also includes word1 and word2.
I am checking a log file using vim. There's only one word can be highlighted, when I search another word,the second word is highlighted but the previous one is not highlighted anymore.
Can anyone show me a way that easily highlight/cancel highlight multiple words in vim? For example, I want to highlight words "stack", "over" and "flow", then I want to cancel the highlight of "stack" and highlight another word "error". So that I can analyze the log more efficiently.
Thanks.
There are two simple ways to highlight multiple words in vim editor.
Go to search mode i.e. type '/' and then type \v followed by the words you want to search separated by '|' (pipe).
Ex: /\vword1|word2|word3
Go to search mode and type the words you want to search separated by '\|'.
Ex: /word1\|word2\|word3
Basically the first way puts you in the regular expression mode so that you do not need to put any extra back slashes before every pipe or other delimiters used for searching.
/\v\/folder|\/copy - search for \folder and \copy
and add to .vimrc set hlsearch
To have all words highlighted in the same color and be able to search and replace all of then, add them as alternatives to the search pattern, e.g. /foo\|bar. My SearchAlternatives plugin provides handy mappings to add and remove patterns.
If you want different colors for different matches, you need a highlight mechanism different from the built-in search. My Mark plugin is used by many people for that. (The plugin page has links to alternative plugins.)
I am aware that it is possible to configure vim to highlight all matches for the searched word.
But, is it possible to highlight the entire line(s) on which the searched for word is found? If so, how?
If you generally want the current line highlighted:
:set cursorline
If you just want the searches highlighted, the only easy way is by extending the search pattern to cover the entire line:
:set hlsearch
:let #/ = '.*'.#/.'.*'
Note that on n / N, the cursor will now jump to the beginning of the line, not the matched word. Also, you won't be able to do :%s//... substitutions of the matched word (without repeating the search pattern) any more.
The exact solution depends probably on your goal:
Do you want to make the matched lines stand out a little more?
Do you want to know the line numbers for further use?
Do you want to act directly on those lines?
You could use the quickfix window to list the lines containing a match:
:vim! foo . | copen
You could use the :global command to list or act on every line containing a match:
:g/foo<CR> " list the lines
:g/foo/<Ex command> " act on each line
:set hlsearch
will highlight the searched-for word.
Note also that you can highlight your current line i.e. the line your cursor is on. So when you move between matches, the complete line you move to will be highlighted.
You may install this plugin highlight.vim. One of the shortcut key <C-h><C-j> allowed you to highlight the line contains the previous search pattern.
Please use this:
To highlight all the search pattern matches in a file set the following option:
:set hlsearch
To disable the highlighting temporarily, use:
:nohlsearch
I want to replace word with WORD, but only on the lines which start with -. Anybody knows how to do it?
:%g/^-/s/word/WORD/g
it's just a normal search and replace, but using g// to filter the lines you want to run it on.
In normal mode I press +* and VIM highlighs the occurences of the word under cursor. How to change for example 2,4-5 (second, fourth and fifth) words in search results with %s command?
I know I can use %s and to change the searched word in certain lines, but it is not what I need.
Assuming you did a find first, you could use
:%s//replaced/gic
It will ask for each replacement if it needs to be done.