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.

3 Answers 3

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.

0
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
1
  • 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 7, 2013 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.

7
  • I get E464: Ambiguous use of user-defined command: G and the cursor on line 2 when using your command. Which ex version are you using?
    – l0b0
    Jun 7, 2013 at 12:29
  • Just type enter, and the result will be right.
    – cuonglm
    Jun 7, 2013 at 12:34
  • No, as I said the cursor is on line 2.
    – l0b0
    Jun 7, 2013 at 12:49
  • Still the same result.
    – l0b0
    Jun 7, 2013 at 13:07
  • Fix bug. My mistake in test.txt. It contains a blank line at the end. So my first solution is failed, because it doesn't search for current line.
    – cuonglm
    Jun 7, 2013 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .