On 20/11/11 22:24, Gary Johnson wrote:
On 2011-11-20, porphyry5 wrote:
In a script, how can I get repeated searches always to begin at the start of
the buffer?
If I precede the search with gg  or :cursor(1, 1) I get E492, with 1G I get
E464.

:map p$ ggdd:while @" != ""<CR>:b#<CR>:cursor (1, 1)<CR>:silent!
/^R"<CR>0i$<Space><Esc>:b#<CR>dd:endwhile

I haven't looked closely at your mapping, but to use a normal-mode
command such as gg in a script, that is, as an ex command, you must
precede it with ":normal", as

     :normal gg

Similarly, to use a function as an ex command, you must precede it
with ":call", as

     :call cursor(1, 1)

See

     :help :normal
     :help :call

Another way to move the cursor to the first line of the buffer is to
simply put the number 1 on a line by itself, or in a mapping like
yours, as ":1<CR>".

HTH,
Gary


Yes, and why not use bar-separated commands? You _are_ running in 'nocompatible' mode aren't you? The following (untested) assumes that this mapping definition is part of a script (of your vimrc, maybe):

        map p$
          \ :1d <Bar>
          \ while @" != "" <Bar>
            \ b # <Bar>
            \ 1call search(@") <Bar>
            \ s/^/$ / <Bar>
            \ b # <Bar>
            \ d <Bar>
          \ endwhile<CR>

See
        :help 'nocompatible'
        :help :bar
        :help map_bar
        :help line-continuation
        :help :d
        :help :call
        :help search()
        :help :s
        :help :map-<buffer>
        :help 'runtimepath'
        :help :autocmd
        :help FileType

This would add a dollar and a space in front of the first line other than the first in the alternate file which matches as a pattern any line in the current file before the first empty line (or until end-of-file if there is no empty line), and remove the matched lines (and the terminating empty line, if any) from the current file. Repeated lines cause repeated dollar-space insertion. If there is no match, add dollar-space to the first line of the alternate file. If there are several unmatched lines, add '$ ' that many times to the first line. If a line in the current file starts with '$ ', the matching is done after the changes for all preceding patterns have been made.

I mention :map-<buffer> and what I listed after it because if you need this mapping for only one filetype (or a small number of them), it should be defined with <buffer>, either in an after-ftplugin, or alternatively at the FileType event but still with <buffer>.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
222. You send more than 20 personal e-mails a day.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to