当我需要在许多文件中执行重复性任务时,我会即时创建宏。就我而言,我在VIM中安装了NERDtree(http://www.vim.org/scripts/script.php?script_id=1658),这会创建一个vsplit
窗口,在左侧,我列出了其中的所有文件当前目录。在一个示例中,我需要MY_TEST
在文件中找到字符串的第一个实例,然后删除CUSTOM_PREFIX-
该行中标记为的前缀(如果存在),保存文件,然后移至下一个。
这是我在当前目录中的大约2000个文件上使用此宏的方式。我可以编写一个bash脚本来执行此操作,但就我而言,这在VIM中更快。所述+
被按压的键表示SIMUL
# Start recording a macro sequence to register 'A'
q,a
# Search silently for the string "MY_TEST"; DO NOT report an error
# if the string could not be found.
:silent! /MY_TEST
# Replace, on just the current line, the string "CUSTOM_PREFIX-", with
# nothing (ie: delete it), and suppress any warning messages if the
# string could not be found on the current line.
:s/CUSTOM_PREFIX-//e
# Now issue some UI commands. CTRL-W then and arrow key lets you hope
# between tabs/splits/windows within vim.
CTRL+ W,←
# Scroll down to the next file in the list in NERDtree
↓
# Open the next file
ENTER
# End the macro
q
# Run the macro a thousand times in VIM
1000 @,a
希望这可以帮助!