0

I have been changing "$OLD" to "~/new" in a lot of files lately, so I am now used to starting vim like this

$ vim file.txt

Enter

Lorem ipsum $OLD
Lorem .OLD ipsum

:%up (brings back %s:\$OLD:\~/new:gc)Enter

:wqEnter

$

So I added a new autocommand in ~/.vimrc, like this

if has("autocmd")
  au BufEnter %s:\$OLD:\~/new:gc
endif

and this

  au BufEnter :%s:\$OLD:\~/new:gc

And next time I tried vim, it wasn't as impressed as I was hoping:

$ vim file.txt

Enter

--- Auto-Commands ---
Press ENTER or type command to continue

Enter

Lorem ipsum $OLD
Lorem .OLD ipsum

:qEnter

$

Why is vim just telling me about the commands, instead of running them?

2
  • 1
    Found some help at "Learn Vimscript the Hard Way"
    – jalanb
    Commented Jun 10, 2016 at 0:36
  • 1
    autocmd FileType sh nnoremap <F3> :%s:\$OLD:\~/new:gc<cr> works for me, but doesn't answer the question of what I did wrong the first time round
    – jalanb
    Commented Jun 10, 2016 at 0:39

2 Answers 2

4

You didn't provide a pattern to au. Note the syntax:

:au[tocmd] [group] {event} {pat} [nested] {cmd}
                        Add {cmd} to the list of commands that Vim will
                        execute automatically on {event} for a file matching
                        {pat} autocmd-patterns.

:au[tocmd] [group] {event} {pat}
                        Show the autocommands associated with {event} and
                        {pat}.

The first adds an autocommand, and the second lists autocommands. That's why you got that output.

4

You should change:

au BufEnter %s:\$OLD:\~/new:gc

to:

au BufEnter * %s:\$OLD:\~/new:gc

or something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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