简体   繁体   English

VIM:如何将搜索/替换命令添加到vimrc并映射到快捷方式

[英]VIM: how to add search/replace command to vimrc and map to a shortcut

I have two search/replace commands that I find myself running in vim fairly often to clean up html code so I can copy/paste it online. 我有两个搜索/替换命令,我发现自己经常在vim中运行以清理HTML代码,因此我可以在线复制/粘贴它。 The commands are: 命令是:

:%s!<!\&lt;!g
:%s!>!\&gt;!g

I wanted a way I could map both of these commands to be run together ... I did some searching for how to use the :map commands in vimrc, however, I can't see how to combine the two lines into a single command that is run with a single keystroke (or a single sequence of strokes). 我想要一种方法,我可以将这两个命令映射到一起运行...我做了一些搜索如何在vimrc中使用:map命令,但是,我无法看到如何将两行合并为一个命令用单击键(或单个笔画序列)运行。

Thanks! 谢谢!

You can put the commands on a single line separated with a bar. 您可以将命令放在用条分隔的单行上。

:%s!<!\&lt;!g|%s!>!\&gt;!g

But you'll have to escape it in the map command 但你必须在map命令中将其转义

:map <F3> :%s!<!\&lt;!g\|:%s!>!\&gt;!g<CR>
:TOhtml

will create a new buffer containing your previous buffer HTML-ized, including entity escaping (and syntax highlighting, if you had that enabled). 将创建一个新的缓冲区,其中包含您之前的HTML-ized缓冲区,包括实体转义(以及语法突出显示,如果已启用)。 See :h TOhtml for more information. 有关更多信息,请参阅:h TOhtml

If you are using this search/replace pattern to HTML encode entities, you might want to check out the unimpaired plugin . 如果您将此搜索/替换模式用于HTML编码实体,则可能需要查看未受损的插件 Amongst other things, this provides shortcuts for encoding and decoding XML, URL and C strings. 除此之外,它还提供了 XML,URL和C字符串进行编码和解码的快捷方式。

:map <F3> :%s!<!\&lt;!<cr>:%s!>!\&gt;!<cr>    

当然可以用你想要的任何键替换

我没试过,但是你能不能把它们放在由"<CR>"分隔的同一行上吗?

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

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