repeat last command but not write











up vote
2
down vote

favorite












@: 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?










share|improve this question






















  • 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
    2 days ago










  • 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
    2 days ago






  • 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
    2 days ago















up vote
2
down vote

favorite












@: 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?










share|improve this question






















  • 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
    2 days ago










  • 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
    2 days ago






  • 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
    2 days ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











@: 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?










share|improve this question













@: 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?







command-line






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 at 1:38









theonlygusti

300113




300113












  • 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
    2 days ago










  • 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
    2 days ago






  • 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
    2 days ago


















  • 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
    2 days ago










  • 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
    2 days ago






  • 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
    2 days ago
















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
2 days ago




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
2 days ago












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
2 days ago




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
2 days ago




1




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
2 days ago




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
2 days ago










1 Answer
1






active

oldest

votes

















up vote
5
down vote













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.






share|improve this answer





















  • 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
    – theonlygusti
    yesterday











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "599"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f18078%2frepeat-last-command-but-not-write%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote













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.






share|improve this answer





















  • 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
    – theonlygusti
    yesterday















up vote
5
down vote













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.






share|improve this answer





















  • 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
    – theonlygusti
    yesterday













up vote
5
down vote










up vote
5
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Karl Yngve Lervåg

6,2571029




6,2571029












  • 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
    – theonlygusti
    yesterday


















  • 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
    – theonlygusti
    yesterday
















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
– theonlygusti
yesterday




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
– theonlygusti
yesterday


















draft saved

draft discarded




















































Thanks for contributing an answer to Vi and Vim Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f18078%2frepeat-last-command-but-not-write%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]