LINUX: Repeat Command/Action N Times in VIM Editor
All too often, I find myself redoing a command over and over. I've often said, there must be a better way to do this. And there is! While writing some automation scripts in an agile "paired-programming" method yesterday, I learned a few neat tricks for VI from my programming partner. I'll share them in the next few posts.
The first is repeating a command. If, for example, you want to delete 100 lines from your file in VIM. Pressing the "d
" key twice deletes a single line. So I used to just keep typing dd
100 times till everything I want was deleted. If you know how many times you want to run the command, you just type the number first, then the command.
100dd
Typing the above in VIM will repeat the single line delete (dd
) command 100 times. How else can it be used? It can even insert text repeatedly! Type the following:
50iTHE END IS NEAR<escape><escape>
The 50i
tells VIM that you are going to insert some text that you want repeated 50 times. The text "THE END IS NEAR
" is what will be repeated. And pressing the escape key twice implements it. You would get:
THE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEAR
Notice it is all one line. In order to make it appear on multiple lines, just press the enter key and then end of the text, before the escape. Very simple.
发表评论
YKDVuj Just a smiling visitor here to share the love (:, btw outstanding pattern.
8TaUlX It as really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
v1EYTE You are my breathing in, I have few blogs and often run out from to brand.
tTY4pQ I'аve recently started a website, the information you provide on this web site has helped me greatly. Thank you for all of your time & work.
JgEg10 Appreciate you sharing, great blog article. Cool.
CWXaYg I think this is a real great blog.Really looking forward to read more. Will read on...
tiQBMc I am so grateful for your post.Really looking forward to read more. Fantastic.
PFDSYw This is one awesome article post.Really looking forward to read more. Much obliged.
8wFhfl Very good post. Keep writing.