简体   繁体   English

如何在文本文件上执行vim命令列表?

[英]How to execute a list of vim commands on a text file?

I have a text that's around 1000 lines long, and I have a bunch of vim commands I want to apply to it. 我有一个大约1000行的文本,我有一堆我想要应用的vim命令。 Lots of regex find-and-replace stuff. 很多正则表达式找到和替换的东西。 For example: 例如:

  • :%s/^[^\\$\\$]/def
  • :%s/\\$\\$/
  • %s/='/ = ['

I could copy and paste those commands one by one, but that's work I'll have to do again every time I receive a new version of this file. 我可以逐个复制并粘贴这些命令,但每次收到此文件的新版本时我都必须再次这样做。 I'm wondering if there's a way to save a long list of commands that I can then apply to the file? 我想知道是否有办法保存一长串命令然后我可以应用到该文件? Thanks! 谢谢!

registers 寄存器

You can put them all into a register , either by typing those commands in a scratch buffer and then y anking into a register, or via explicit assignment: 你可以把他们都到寄存器中 ,无论是在临时缓冲区中输入这些命令,然后y安庆到寄存器,或者通过明确的分配:

:let @a = "%s///\n%s///"

Then, execute via :@a 然后,执行via :@a

Depending on your Vim configuration, the register contents may persist across sessions (cp. :help viminfo ), but you must be careful to not override them. 根据您的Vim配置,寄存器内容可能会持续跨会话(cp。:help :help viminfo ),但您必须小心不要覆盖它们。

scripts 脚本

A longer-lasting and more scalable solution is putting the commands into a separate script file, ideally with a .vim extension. 更持久且更具可扩展性的解决方案是将命令放入单独的脚本文件中,理想情况下使用.vim扩展名。 You can put them anywhere (except ~/.vim/plugin , unless you want them executed automatically), and then run them via 您可以将它们放在任何位置(除了~/.vim/plugin ,除非您希望它们自动执行),然后通过它们运行它们

:source /path/to/script.vim

custom commands 自定义命令

If you need a set of commands very often, you can give them a name and turn them into a custom command : 如果您经常需要一组命令,可以为它们命名并将它们转换为自定义命令

:command! MyCommands %s/// | %s///

Put this into your ~/.vimrc , or (if it's filetype-specific), make it a :help ftplugin . 把它放到你的~/.vimrc ,或者(如果它是特定于文件类型的),把它变成:help ftplugin

通常,您可以使用shell从shell运行vim命令

vim -c YourCommandHere

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM