0

I'm trying to write a Vim script function to toggle between -> and . but searching for . is seemingly impossible.

I've tried to search using ASCII (decimal and hex) and it doesn't work. It all works from normal mode and when I do my experiments it let's me cycle between the dots using n etc but the cursor either doesn't move or goes forward once or goes to the beginning of the line etc.

The closest I got just sets my cursor to the start of the next line with a dot occurring.

How is this meant to be done? I've even upgraded to the latest version to try to fix it assuming it's some bug, but it still doesn't let me just search for a dot. Why would it be this difficult? I can't find anything when I Google. I loaded Vim without my .vimrc and it still results the same.


I just want a search that puts the cursor on the next dot. Something like

:exec "normal /\."

or

:exec "normal /\%d46" 

only that works.

Those are typed from memory and I'm away from the computer with a headache, so I'm not sure those are good examples of what I've been trying but some of what I've tried was to that ilk.

  • You should show us the Vim script code you tried. – Ralf Jan 8 at 22:45
  • I stopped looking at that and I'm just trying to run a command that does the search, I'll worry about the script once it's possible to actually find a dot – yOy Jan 8 at 22:48
  • /\. should be all you need. You definitely don't want to do :exec "normal /\." because then you also need to explicitly include the enter key, which is complicated to add to the command line (you need to type ctrl-v enter to insert a literal enter) – DJMcMayhem Jan 8 at 23:17
  • I tried both ways and including enter key by <CR> and the M^ business or whatever it was – yOy Jan 8 at 23:34
  • 1
    There are several problems here, first, have a look at :h function-search-undo, which explains, why using the normal search command does not work when used as VimScript solution. Second, your useage of double quoted strings is wrong, you need to double the backslashes there, see :h expr-string (and you would have to add the Enter to the command to move to the search result). And finally, if you want to emulate the normal search command, it is usually better to use the explicit search() function. – Christian Brabandt Jan 9 at 6:52
2

Are you looking for search()?

:call search('\.')

See :help search() for all the details.

Or with :exec:

:exec "normal /\\.\<CR>"

But I would prefer search().

Note the differences between single and double quotes. With single quotes a single backslash escapes the dot, with double quotes a double backslash is needed. Also \<CR> does only work in double quotes. See :help string and :help literal-string.

  • both worked! thanks! Still don't understand why the other ways I've tried didn't work, seems like they should – yOy Jan 8 at 23:39
  • can you explain why the additional backslash is needed? – yOy Jan 8 at 23:40
  • I didn't know about the needing double backslash if it's double quotes but the single backslash way doesn't work in single quotes either? also seems the double back slash doesn't work in single quotes... vimscript is frustrating, I need to look into writing scripts in straight c – yOy Jan 8 at 23:42
  • 1
    With double quotes the backslash is used to escape special characters. Like \n for newline. To get a real backslash, you need to escape the backslash. Hence two backslashes. – Ralf Jan 8 at 23:43
  • 1
    see :h expr-string – Christian Brabandt Jan 9 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.