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.

vim.ggyG
  • 201
  • 1
  • 8

2 Answers2

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
Maxim Kim
  • 13,376
  • 2
  • 18
  • 46
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.

vim.ggyG
  • 201
  • 1
  • 8