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.
Related
I would like for my cursor to automatically go to the search result after I stop typing. For example, I type in /new, after 100ms (or whatever the debounce/delay is), I'd like the cursor to automatically go to the first match (if it exists) rather than me having to constantly hit return with my pinkie, which gets a bit sore having to press return 100 times per day.
Is there a setting to do this in vim, or how could something like this be achieved if there is not? From vim's help for incsearch:
You
still need to finish the search command with <Enter> to move the
cursor to the match.
First, important information:
When you are performing a search and incsearch is set, the current match will be highlighted as the highlight group IncSearch. If you also have hlsearch set, then the other non-current matches on your screen will also be highlighted, instead as the highlight group Search. For example, if you search for thing and the word thing appears three times on screen in your current file, the first match after your cursor will be the 'current match' and highlighted so. The other matches will be highlighted differently.
Now to answer your question
You can move between matches while searching without ending the search by pressing <Enter>. This is done with <C-G> and <C-T> (That's CTRL-G and CTRL-T). Reference :h /_ctrl-g and :h /_ctrl-t. This allows you to move through the file and see all the matches of the pattern you have already typed without actually ending the search. This also allows you to use <C-L> to add characters to the search. I'll let you look that one up (:h /_ctrl-l>).
Using GVIM, I'd like to have something similar to the line count MSExcel offers - an on-the-fly line count that shows me how many lines I've selected so far that are non-blank (i.e. don't contain only whitespaces).
Up until now I've always used y for yank and then it shows on the bottom how many lines yanked, but:
this is not on-the-fly
this counts also blank/whitespace lines.
What's the best way to achieve this?
The downside of :substitute//n is that is clobbers the last search pattern and search history, and its output contains additional text and is difficult to capture. Alternatively, you can filter() the entire buffer, and count the matches:
:echo len(filter(getline(1, '$'), 'v:val =~# "\\S"'))
This can be easily turned into a custom mapping or command. If the performance is acceptable, you can even add this to your 'statusline':
:let &statusline .= ' %{len(filter(getline(1, "$"), ''v:val =~# "\\S"''))} lines'
Note: The statusline update won't work during a visual selection, because the marks '< and '> are only set after you've left the selection.
:%s/\S//n
3 matches on 3 lines
This combines a no-op :substitute (with the /n flag) that only counts the matching lines with the \S atom, which matches non-whitespace. As long as there is any such in a line, it is counted.
For the visual selection, just trigger it from there; it'll automatically use :'<,'> range instead of :%.
To get the number of blank lines you could use
:%s/^.\+//n
of course, instead of % you can use any other range command.
However, this approach will count only non-blank lines (without whitespace) not starting with a whitespace. Some hints on counting search results can be found here.
To allow for whitespace recognition you could use something like
:%s/^.*[^ ]\+//n
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 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.
How to calculate how many words are on one line within Vim/gVim?
It is kind of hard to do it manually.
It would also be nice to exclude " { } *. Essentially a word is something that begins with a letter. And words are space separated.
Go to the line you want to count and do (change the matching pattern as you want):
:s/\<[a-zA-Z]//gn
This won't replace anything, just will output something like 3 matches on 1 line.
g ctrl-g counts {}* as well, so you can use it if you don't want to avoid special characters.
:echo len(split(getline('.'), '\W\+')) " or \H if you want to ignore numbers as well
The anwser is in the help: :h split()