Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Vi: Search for word under cursor (or copy word to command line)

737 views
Skip to first unread message

Mirko K.

unread,
Jul 4, 2012, 4:18:35 PM7/4/12
to
Hi!

A friend of mine has to use plain Vi 3.10 on AIX, and he asked me
how to search for the word under the cursor like Vim's % or NVi and
Elvis' CTRL-A.

I don't have access to AIX, but after reading its online docs, and
try with Levee and Vile, I came to the conclusion, that it is not
possible, mainly because there's no way to yank the current word (or
any other part of the file) to the command line, so you can't
constructing a /current_word command or mapping.

Is that correct, or did I overlook something?

Brian Masinick

unread,
Jul 14, 2012, 4:50:09 PM7/14/12
to
On Wednesday, July 4, 2012 4:18:35 PM UTC-4, Mirko K. wrote:
> Hi!
>
> A friend of mine has to use plain Vi 3.10 on AIX, and he asked me
> how to search for the word under the cursor like Vim's % or NVi and
> Elvis' CTRL-A.
>
> I don't have access to AIX, but after reading its online docs, and
> try with Levee and Vile, I came to the conclusion, that it is not
> possible, mainly because there's no way to yank the current word (or
> any other part of the file) to the command line, so you can't
> constructing a /current_word command or mapping.
>
> Is that correct, or did I overlook something?

To the best of my knowledge - but I do defer to others, if they happen to know anything more - the original Vi had forward and backward searches, and stuff like search again, but as far as I can recall, / - forward search; with text, searches for the text you provide; without text, repeats search, ? - reverse search; with text, searches backward for the text you provide; without text, searches backward, n - Repeat previous search, N - Repeat previous search, but it opposite direction.

I found some of the doc for the UNIX Vi editor at http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm - perhaps that, or searches for similar documents, will fill in other missing gaps or questions. Hope that is helpful.

Bob Harris

unread,
Jul 15, 2012, 12:04:08 AM7/15/12
to
In article
<799a92c9-5280-423c...@googlegroups.com>,
"------------------------------------
"
" Cntl/N - Find the word under the cursor. NOTE: This
" modifies the file. This works similar to the Vim
" '*' command (except Vim does not modify the file
" in the process and * is a heck of a lot shorter,
" and you do not need to define a key mapping :-)
"
map ^V^N ebywo^V^[pI/\<^V^[:s/[ ]*$/\\>/^V^M"qddb@q
" ^^^^^^^^^
" |||||||||
" <space><tab>
"
" Here is a translation of the command:
"
" ^V^N - This is a real Control/V and a real
" Control/N. The Control/V allows the
" definition of Control/N as a mapped key.
"
" NOTE: Entering a real Control/V into the .exrc
" generally requires typing Control/V
" twice. Entering Control/N into the
" file generally requires typing
" Control/V Control/N. Typing
" Control/V before any Control
" character you want to enter into
" the .exrc is typically how you
" enter a control character.
" e - go to end of current word (this will fail
" if the cursor happens to be on the very
" last character of the current word;
" generally not a problem).
" b - now go to the beginning of the word. 'eb'
" makes sure that we are at the beginning of
" the word to find.
" yw - yank a copy of the word.
" o - open a new line below the cursor. This is
" where we are going to build a search
" command macro. It is also where we end up
" causing the buffer to be modified even if
" we were only viewing the text read only :-(
" ^V^[ - ^[ is ESC to exit insert mode, the
" Control/V is so that it can be entered via
" the .exrc file.
" p - put the yanked word into the new line.
" I - Enter Insert mode in front of the yanked
" word in the new line.
" /\< - Text to be inserted. / is the start of a
" search command and \< is the notation to
" anchor the search to the beginning of
" matching words instead of finding string
" matches in the middle of larger words.
" ^V^[ - ^[ is ESC to exit insert mode, the
" Control/V is so that it can be entered via
" the .exrc file.
" :s/// Use the ex substitute command to remove
" any trailing white space (blanks and tabs)
" and then append \> to the end of the
" search command. The \> is the notation to
" anchor the search to the end of matching
" words instead of finding string matches in
" the middle of longer words.
" ^V^M - is the Carriage Return that executes the
" ex substitute command.
" "qdd - The 'dd' deletes the new line into the
" named register 'q'. So now the new line
" we created is no longer in the file and we
" now have a search macro that looks like:
" /\<search_word\>
" stored in the named register 'q'.
" b - when we deleted the new line, the cursor
" was sitting on the beginning of the next
" line. 'b' moves the cursor back to the
" line with our word in it, only it is
" positioned on the last word on our
" starting line. If you want to change this
" to some other position on the line, just
" adjust the movement command which is
" currently a 'b'.
" @q - execute the macro stored in the named
" register 'q'. This will be the search
" command looking for the word that was
" under the cursor when Control/N was
" issued.
"
" What could be easier :-) OK, I had tried to write
" one of these on my own, but I finally found this in
" a posting on the comp.editors news group. The good
" news is that I can actually understand it.
"
"------------------------------------

Eli the Bearded

unread,
Jul 15, 2012, 12:56:58 AM7/15/12
to
In comp.editors, Mirko K. <mirkok...@googlemail.com> wrote:
> A friend of mine has to use plain Vi 3.10 on AIX, and he asked me
> how to search for the word under the cursor like Vim's % or NVi and
> Elvis' CTRL-A.

% jumps to matching (paren), {brace}, or [bracket], not search for
words. I think you mean # or *, which both do that in Vim (# is
a backwards search, while * is forwards).

> I don't have access to AIX, but after reading its online docs, and
> try with Levee and Vile, I came to the conclusion, that it is not
> possible, mainly because there's no way to yank the current word (or
> any other part of the file) to the command line, so you can't
> constructing a /current_word command or mapping.

Bwahhahah. If AIX has real vi, it can be done, albeit with a complex
macro. In the following line the <ESC> is supposed to be a real
escape. You can type it in vi with <ctrl-v><escape>.

" emulate * search as it exists in vim
" uses register w
:map * b"wyeo/\<\><ESC>F<"wp"wdd@w

The first 'b' is there to jump to the beginning of the word. You may
or may not like having it there. Breaking it down:

b jump to start of word
"w use register w
ye yank to end of word
o start new line
/\<\> put blank search command in (\< and \> for word boundaries)
<ESC> stop insert
F< jump backwards to the <
"w use register w
p put the word we yanked
"w use register w
dd delete the line
@w run the command in register w

Elijah
------
emulating # left as an exercise for the reader

Eli the Bearded

unread,
Jul 15, 2012, 1:00:35 AM7/15/12
to
In comp.editors, Bob Harris <nospam....@remove.Smith-Harris.us> wrote:
> map ^V^N ebywo^V^[pI/\<^V^[:s/[ ]*$/\\>/^V^M"qddb@q

Wow, that's ugly. :^) Mine, posted elsewhere in this thread is
shorter.

:map * b"wyeo/\<\><ESC>F<"wp"wdd@w

Read the explanation in the post I wrote it for.

Elijah
------
you shouldn't need three escaped control characters

Mirko K.

unread,
Aug 8, 2012, 7:19:41 PM8/8/12
to
Sorry for the late reply, waited for response from my friend which
didn't came.

On 15.07.2012 06:56, Eli the Bearded wrote:
> In comp.editors, Mirko K. <mirkok...@googlemail.com> wrote:
>> A friend of mine has to use plain Vi 3.10 on AIX, and he asked me
>> how to search for the word under the cursor like Vim's % or NVi and
>> Elvis' CTRL-A.
>
> % jumps to matching (paren), {brace}, or [bracket], not search for
> words. I think you mean # or *, which both do that in Vim (# is
> a backwards search, while * is forwards).

Correct, not sure how I could mistype * with % (some Parsecs away on
a German keyboard) ;-)


>> I don't have access to AIX, but after reading its online docs, and
>> try with Levee and Vile, I came to the conclusion, that it is not
>> possible, mainly because there's no way to yank the current word (or
>> any other part of the file) to the command line, so you can't
>> constructing a /current_word command or mapping.
>
> Bwahhahah. If AIX has real vi, it can be done, albeit with a complex
> macro.

Thanks for your efforts (also to Bob Harris and others), but
changing the file isn't really an option. I played with similar ways
(but didn't got it to work), but said friend works with make and
version control systems, accidentally writing such a (not really
changed) file can probably cause problems.

I keep with my original suggestion to somehow get NVi/Vim on that
machine.

Thanks anyway for your solutions, learned something about registers. :-)

toothpik

unread,
Aug 14, 2012, 2:55:45 PM8/14/12
to
Mirko K. wrote:

> Sorry for the late reply, waited for response from my friend which
> didn't came.
>
> On 15.07.2012 06:56, Eli the Bearded wrote:
>> In comp.editors, Mirko K. <mirkok...@googlemail.com> wrote:
>>> A friend of mine has to use plain Vi 3.10 on AIX, and he asked me
>>> how to search for the word under the cursor like Vim's % or NVi and
>>> Elvis' CTRL-A.
>>
>> % jumps to matching (paren), {brace}, or [bracket], not search for
>> words. I think you mean # or *, which both do that in Vim (# is
>> a backwards search, while * is forwards).
>
> Correct, not sure how I could mistype * with % (some Parsecs away on
> a German keyboard) ;-)
>
>
>>> I don't have access to AIX, but after reading its online docs, and
>>> try with Levee and Vile, I came to the conclusion, that it is not
>>> possible, mainly because there's no way to yank the current word (or
>>> any other part of the file) to the command line, so you can't
>>> constructing a /current_word command or mapping.
>>
>> Bwahhahah. If AIX has real vi, it can be done, albeit with a complex
>> macro.
>
> Thanks for your efforts (also to Bob Harris and others), but
> changing the file isn't really an option. I played with similar ways

can't you add an

e!

to restore it and preserve the timestamp?
0 new messages