Search first character in a file

8 views
Skip to first unread message

Cristian

unread,
8:58 AM (5 hours ago) 8:58 AM
to vim_use
Hello all,

1. I have this file:

--- begin testfile.txt ---
a
a
a
b
--- end testfile.txt ---

2. I run the command:

$ vim testfile.txt

3. Inside when the cursor is positioned on the first character I type:
/a<ENTER>

The first "a" is not found. Unless I type "n" until I get to the end of the file and then another "n".

Is it possible to search the first character in the file immediately after you open the file?
Is this a bug or a design decision ?

Thank you.
Cristian 



Tony Mechelynck

unread,
9:04 AM (5 hours ago) 9:04 AM
to vim...@googlegroups.com
I think it's a feature, not a bug: if by hitting n when the cursor is
on a match, you wouldn't make it move, then repeatedly hitting n would
never move the cursor. To find the first a when already on it, then
hit n followed by N. By doing that when not on a match, you would find
the last match, however.

Best regards,
Tony.

Tim Chase

unread,
9:08 AM (5 hours ago) 9:08 AM
to vim...@googlegroups.com
On 2026-01-21 05:57, Cristian wrote:
> --- begin testfile.txt ---
> a
> a
> a
> b
> --- end testfile.txt ---
[snip]
> 3. Inside when the cursor is positioned on the first character I type:
> /a<ENTER>
>
> The first "a" is not found. Unless I type "n" until I get to the end of the
> file and then another "n".
>
> Is it possible to search the first character in the file immediately after
> you open the file?

Typically the way I've resolved this is to start the search from the
last line (as long as 'wrapscan' is set, which it is by default so you'd
only need to mess with it if you've unset it explicitly)

:set wrapscan
:$/a

> Is this a bug or a design decision ?

As Tony notes, I'm pretty sure this was a design decision because it
starts the search after the cursor so you don't just keep finding the
same thing.

-tim
--





Cristian

unread,
10:21 AM (4 hours ago) 10:21 AM
to vim_use

Thank you for the explanation — that makes sense for the behavior of the n key.

That said, I was wondering whether, for the initial search (the one
NOT triggered by n), it might also be reasonable for vi to start
searching from the character currently under the cursor.

This could make the behavior a bit more intuitive when the cursor
is already on a match.

Best regards,
Cristian
Reply all
Reply to author
Forward
0 new messages