Learn VIM
  • Vim Hands-On Tutorial (Learning Vi and Vims)
  • Vim Hands-On Tutorial(Learning Vim)
    • Chapter 1: Cursor Movement
    • Chapter 2: Opening Files, Finding Content
    • Chapter 3: Modifying and Saving Files
    • Chapter 4: Some Tips
    • Chapter 5: Splitting Windows and Tabs
    • Chapter 6: Block Operations
    • Chapter 7: Vim Macros
    • Vim Modes
    • Vim Plugins
    • plugins
      • Airline & Themes
      • EasyAlign
      • Plugin Recommendations
      • NERDTree
      • surround.vim
  • Vim 实操教程(Learning Vim)
    • 第一章:光标移动
    • 第二章:打开文件、查找内容
    • 第三章:文档的修改与保存
    • 第四章:一些小技巧
    • 第五章:分屏与标签页
    • 第六章:块操作
    • 第七章:Vim 中的宏
    • Vim 的模式
    • Vim 插件
    • plugins
      • Airline & Themes
      • EasyAlign
      • 插件推荐
      • NERDTree
      • surround.vim
Powered by GitBook
On this page

Was this helpful?

  1. Vim Hands-On Tutorial(Learning Vim)

Chapter 7: Vim Macros

PreviousChapter 6: Block OperationsNextVim Modes

Last updated 1 year ago

Was this helpful?

Macros are a more advanced topic in Vim (and any other editor). If you've mastered the previous chapters, you're already a Vim master. So, let's take it to the next level.

Do you remember the example of converting text to an array in the ? We'll do the same thing, but this time with macros.

16gg Jump to the start line where you're ready to start processing, follow the instructions, see the effect first and then explain.

var myArray = [
Press qa to start recording the macro, and then press I<single quote><Esc>A<single quote><comma><Esc>jq7@a
Me too
Me too
Me too
Me too
Me too
Me too
Me too
];

OMG! What happened, did you get a cold sweat? The results of the previous two block operations were completed in an instant, and finally a little finishing work, remove the comma at the end of the last line, indent the collective, done!

Next, let's explain the operation just now:

  • q is to start recording the macro, a is to give the macro recording process a storage location, which can be 0-9 or a-z;

  • Then I<single quote><Esc>A<single quote><comma><Esc>j is the operation process of the entire macro you recorded this time, which means inserting a single quote at the beginning of the line, inserting a single quote and a comma at the end of the line, and jumping to the next line;

  • Pressing q next ends this macro recording;

  • @ is to call up the macro, a is the name (storage location) of the macro to be called up, and the 7 in front of it should be clear, that is, to execute 7 times.

Tips: @@ calls up the last macro executed again.


Thanks again for your interest! If you love, please share. Love life, love VIM!

All the frequently used and not so frequently used in these seven chapters should have been covered. If you encounter any problems in Vim, or if you miss any regular operations in the tutorial, please feel free to raise them in , and I will do my best to answer them or improve them in the tutorial.

previous chapter
issues