how to do conditional replacing using vim - vim

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.

Related

How to search with a criteria in sublime?

in sublime if you want to search for a string "apple" but not "apple."
How do you do that?
Thank you all.
I heard somewhere you can add a - to substract unwanted hit but don't know where to put it.
With regular expression, you can use:
\bapple(?!\.)
Where (?!\.) is a negative lookahead that make sure we haven't a dot after apple and \b is a word boundary to be sure to not match pinapple

Replace a string using vim

I want to replace the 50th occurrence of Alex with Alex(the father) using vim.
The problem is this: after I execute the replace command, I want to search again for the word Alex and I do not want the replaced one to be displayed anymore.
So when I do gg49/Alex and press 'n' I want vim to skip Alex(the father) and jump to the 51st occurance of Alex (51st in the original document).
So what I want is that vim does not show me the sub-string Alex, only the exact match. Is there any way to do this?
You can do this search with a negative lookahead against a left parenthesis to avoid matching Alex when it is followed by anything that starts with (.
/\<Alex\>\((\)\#!

How to highlight multiple words in vim

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.)

Highlight search pattern without moving cursor

In vim, is it possible to highlight a search pattern without moving the cursor?
For example, if I want to find m_depthTable I could do:
/m_depthTable
and that will highlight all instances of m_depthTable, but it will also move to the next occurance.
I want to highlight without moving. Possible?
You could do a substitute command with the n flag. This won't move the cursor or do the substitute.
:s/pattern//n
just
/pattern<enter>
then press ``
You can write directly into register that contains last search pattern:
:let #/="m_depthTable"

GVIM. How to change certain words that match the pattern?

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.

Resources