2

For example, if I want to find all bar lines and print them.

$ cat file
line foo
line bar 1
line bar 2
$ cat cmd
norm gg
/bar
p
norm n
p
$ cat cmd | vim -u NONE -es file
line bar 1
line bar 1
$

As it outputs, normal n does not move to line bar 2.

1
  • Using normal nn does work for me, but I'm not entirely sure why 🤔 – Martin Tournoij Feb 8 at 10:22
2

normal n works for me in Ex mode.

The problem is that you mix normal mode /search with ex-mode range :/search. In your case :/search is executed and it doesn't populate @/ register thus with :normal n you try to search something else (previous interactive search?)

UPD:

i want to find all bar lines and print them

:g/bar/p

or

for line in getline(1, '$')
    if line =~ 'bar'
        echo line
    endif
endfor

or to mimic what you have in your question:

norm! gg
let @/ = 'bar'
norm! n
p
norm! n
p
3
  • do you mean i should use norm /bar^M? – pynexj Feb 8 at 9:09
  • I don't know what is your end goal, so can't recommend. But there is call search(), or let @/ = "searchthing" or smth else idk. – Maxim Kim Feb 8 at 10:15
  • updated the question. i want to find all bar lines and print them. – pynexj Feb 8 at 10:34
1

Figured out what's happening.

Here /bar actually means :/bar which is a range spec meaning "go to next line which has bar in it". It's just like :1 which goes to line #1.

And :/bar works in a line-wise way. It goes to the the line and put cursor at column #1. So the next norm n would move cursor to bar 1, and another norm n would move to bar 2.

And p has a side effect. It also moves cursor to column #1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.