3

The ex command $ navigates to the first character of the last line. Therefore, when doing a backwards search it skips the line it's currently on. How do I navigate to the last character with ex, or alternatively search for the last occurrence of a regular expression?

Example:

$ cd -- "$(mktemp --directory)"
$ printf '%s\n' foo bar foo bar > test.txt
$ ex -c '$' -c '?bar' -c 'visual' test.txt

At this point the cursor is on line 2 instead of line 4, as expected. Using ex -c '?bar' -c 'visual' test.txt produces the same result, even though I'd expect it to find the last line when wrapping around (vim does).

Using Vim 7.3.

terdon
  • 242,166
l0b0
  • 51,350

3 Answers3

2

Looks like ex starts at the last line - ex -c '1' -c '?bar' -c 'visual' test.txt moves the cursor to the last line.

l0b0
  • 51,350
2

With my version of ex, $+1?... appears to start searching backward after the last line:

sh$ ex -version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Jul 12 2010 12:49:33)

sh$ ex -c '$+1?bar' -c visual test.txt
Sylvain Leroux
  • 648
  • 4
  • 14
  • I first thought this would just wrap, but ex -c '$+1' -c visual test.txt shows that it's at the bottom of the file. Nice! – l0b0 Jun 07 '13 at 15:19
0

Another solution:

ex -c '?bar' -c '/bar' -c 'visual' test.txt

By default, '?bar' will search for string bar from the last line of file but not search for string in last line. so when hit the first bar from last line, /bar make it correct, back to the bar in last line.

This will fail when the last line not contain string bar.

cuonglm
  • 153,898