2

@: repeats the last colon command I entered, but also seems to include :write

so often I go through this kind of workflow:

:s/foo/bar
:w

Now I go to another line and want to run :s/foo/bar again, but @: runs :w instead.

Can I get @: to ignore :w?

3
  • IIRC “: is readonly; that said, you might (might) be able to accomplish something with a remapping of @: by saving the command string in a global var on Enter at command line (but only for : lines, and only if not write (of which there are many variants))
    – D. Ben Knoble
    Nov 27, 2018 at 4:39
  • Write a function named ExecuteLastNotWrite() and map @: :call ExecuteLastNotWrite()<CR>. In the function get the last entry from the history(histget("cmd", -1)). If :w, get the second to last. Then execute the fetched command. See :help histget().
    – Ralf
    Nov 27, 2018 at 5:12
  • 1
    Do you know that you can repeat the last :s with & (current line) and the last :%s with g& (run substitute over complete file)?
    – Hotschke
    Nov 27, 2018 at 8:13

1 Answer 1

6

It seems you basically understand correctly: ": is a readonly register that contains the last executed command. @: will execute this register. Feel free to read up on this: :help ": and :help @.

Further, your issue is that you want to repeat a command but you are issuing a new command in between, :write, which obviously overwrites ":. You ask: "Can I get @: to ignore :w?". Of course, yes, you can define a new command that uses histget() and map it to @:, as is suggested by @Ralf in the comments. However, I will advice that you instead reconsider your own workflow. Instead of changing @:, try to compose repeatable commands. For instance, instead of

:s/foo/bar
:w

write

:s/foo/bar/e | update

Here I've added the e flag, which prevents the substitute command from failing if there are no foos found. Then I use | (:help bar) to add a new command to be executed after, and I use :update instead of :write to only write the file if there is a change.

In my opinion, this is a better solution than changing how Vim works.

1
  • that's so long to type out every time, and it relies on me knowing that I will want to repeat a substitute in advance
    – minseong
    Nov 29, 2018 at 3:29

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.