Search only between specific line numbers? - vim

I know that with Vim's substitution command you can specific a range of lines:
:12,24s/search/replace
I want to be able to specify a range with normal searches as well. Something like
:12,24/search
Since that doesn't seem to work (at least on my Vim configuration), does anybody know how to achieve this?

Great answer from akira. But after some digging, I found an alternative. It's not as elegant but easier to type in:
:12,24g/search/
This will give you one annoying prompt but it will end up on the first line within the range containing the sought string.

:help search-range
and then
:help /\%>l
so essentially:
/\%>12l\%<24lsearch

Do you really need line numbers? Another way could be to select the range visually.
select the range using v, V or whatever
press ESC to unselect the range
search using /\%Vwhat_to_search to search for 'what_to_search' in the previously selected range.
This is lesser to type, but not directly what you have asked for ;-)
See :help %V
[EDIT] Great, I have just learned that the range to search in can be changed after doing the search by selecting another range, unselecting this range again by pressing ESC and pressing n to repeat search. Vim is really always good for pleasant surprises.

Keep using the substitution command, but append the gc flags to your original example.
:12,24s/search//gc
From :help search-range
[To search within a range] use the
":substitute" command with the 'c'
flag.
Example:
:.,300s/Pattern//gc
This command will search from the
cursor position until line 300 for
"Pattern". At the match, you can type
'q' to stop, or 'n' to find the next
match.

If there marks say a and b, then the search can be restricted to the region between a and b using
/\%>'a\%<'bSearchText
This can be simplified with a cmap
cmap vmsab /\%>'a\%<'b
cmap vmscd /\%>'c\%<'d

If you would like to search to the end of the file, use $:
:3,$s/pattern//gn
This will search from 3-d line to the end

Using Narrow Region plugin we can open a temporary buffer with the range we need to search or change
:900,1000NarrowRegion
Then we can meke a search
/thing
Or a change and write the buffer back
:%s/this/that/g
:wq

Related

Vim: substitute once, starting from CURSOR position (not line)

Is there a simple way (without too many keystrokes), to substitute the next occurrence of a pattern (in the line or in the whole document, both would be interesting), starting from the cursor position?
So far, I've only come up with selecting onwards, going to normal mode, and doing :s/\%Vpattern/rep. It's too cumbersome. Perhaps there's a nice way to select the next occurrance of a pattern, and then one can "change" the selection?
Thanks
You can use the confirm option with your substitution. Use the command
:%s/pattern/replacement/gc
It will take you through each occurrence of the pattern. Type y to replace, n to move on without replacing, and q to quit the search.
You can use gn
Just search for the pattern, using /
Select using gn
Once selected, you can perform any action on it. Like y to copy, c to change etc
:h gn

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"

Vim - Commands to visually select a regex match?

Can't figure out how to automatically select a regex match in visual mode.
For example, manually, I could search for a word
/word
It lands the cursor on the first character of the match "word".
Then I press v to enter Visual mode, and press llll to select the whole "word".
Now I want to do this by a macro, and I don't know the length of the match ahead of time.
I expected that Vim would automatically define some built-in marks at the beginning and end of the current match, so that I could ` to them. But I couldn't find any information on that.
What I want is to reassign Ctrl+n to a macro to take me to the next match and select it in visual mode, i.e. not just highlight the match. (To parallel how n takes you to the next match.)
If you're wondering why, its because I want to create folds based on regex matches (like Ctrl+n, zf), but I'm sure it will come in handy in other cases too.
//e takes you to the end character of last search.
More info -- :help {offset}.
You can find how to restore old search buffer here.
For the benefit of folks who stumble across this question in the future, vim now has this feature built-in: gn (and its close sibling gN) will jump to the next (respectively, previous) match and visually select it. It can also be used as a motion; e.g. cgn will jump to the next match and change it.
:noremap <C-n> //s<CR>v//e+1<CR>
Edit summary: was //e, but //e+1 worked for me (selected the last character of the match too).

How to do column editing in vim?

Vim is pretty powerful when editing by line - the search/replace tools are modeled to work linewise.
But what if I want to change a particular column across all lines? For example, how can I change the 80th column in my file to a # easily?
To edit a column, follow these steps:
Stand on the beginning of the column
Press Ctrl+v, then mark across the column you want to edit.
Press Shift+i to insert text at the beginning of the column, Shift+a to append text, r to replace highlighted text, d to delete, c to change... etc.
Hit ESC when done.
I think people (me) sometimes map the column editing keys to Ctrl+Q so it won't collide with visual select line (V) or paste-text if you mapped it as such.
...I couldn't follow the steps of sa125 (facepalm) so I looked someplace else and found a simpler explanation at: https://blog.pivotal.io/labs/labs/column-edit-mode-in-vi
Ctrl+v [ and select what ever you want]
Shift+i [and write whatever...(check out ** below)]
Esc
*c without Shift can be used instead of step 2, to delete selection before insert. And also r to replace.
**!! Attention Don't be discouraged by the fact that only the first row is changed when you 'write whatever...'!!
Hope it helps!
You can use a substitution where the pattern matches a specific column (\%c):
:%s/\%80c/#/<CR>
Or you can use block-wise visual mode:
gg80|CTRL+vGr#
The 'virtualedit' option can be used to allow positioning the cursor to
positions where there is no actual character:
:set virtualedit
I may be totally off topic here, but if your idea is to avoid long lines, you could have a look at the colorcolumn option of vim 7.3.
For column-wise editing, vis.vim is really useful. You can block-select your column of interest, and manipulate it with normal commands, and even arbitrary Ex commands. From the example on that page, I have often used the pattern:
:'<,'>B s/abc/ABC/g
You can Vundle/Pathogen install vis.vim from github:
Plugin 'taku-o/vim-vis'

Move to start and end of search lookup

In vim/gvim I would like to be able to move to the front and end of the current search lookup. Is this possible?
For example, with this file:
A dog and a cat
A hat and a bat
I would like to be able to perform a search, for example /dog\sand and then be able to move from the beginning of the 'dog and' expression to the end and back so that my cursor starts on column 3, under the letter 'd' of the word 'dog' and then moves to column 9 under the letter 'd' or the word 'and'.
The reason I want to be able to do this is so that I can search for an expression and then use the change command, c, combined with a movement command to replace that particular search expression. I don't want to use substitue and replace here, I want to perform this operation using the change command and a movement key.
You are looking for cgn. From :help gn:
Search forward for the last used search pattern, like
with `n`, and start Visual mode to select the match.
If the cursor is on the match, visually selects it.
If an operator is pending, operates on the match.
E.g., "dgn" deletes the text of the next match.
If Visual mode is active, extends the selection
until the end of the next match.
The beauty of this is you can do cgn and then repeat the command with . and, yes, it will do the same operation on the next search pattern match. This becomes extraordinarily useful when you start searching with complicated regular expressions.
Try ths:
/pattern<cr> to place the cursor at the start of search pattern
/pattern/e<cr> to place the cursor at the end of search pattern
You can change to the end of the match with c//e<CR>. And //<CR> will take you to the beginning of the next match. You could probably work out some kind of bindings for those that make sense. For instance I just tried the following, and it seems to work nicely:
:onoremap <silent>m //e<CR>
So that you can say cm for change match. I'm not sure what I would map //<CR> to, though. I tried mapping it to n, and it seemed to work fine. Don't know if that would be a problem for you.
:nnoremap <silent>n //<CR>
Since you know just how many words you're searching for, why not just move there and then back using word movement commands? I.e., "2w" to go to end of current search result, and "2b" to go back.

Resources