views:

190

answers:

5

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. 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).

Thanks!

A: 

I've not tried it, but can you not put them on the same line separated by "<CR>"?

Dan
+1  A: 
:map <F3> :%s!<!\&lt;!<cr>:%s!>!\&gt;!<cr>    

of course can be replaced with whatever key you wish

kemp
This didn't work if I had a couple in a row ... for example: `>>>` it would only replace the first one and then move on to the next line.
thornomad
That's only because the 'g' modifier is missing.
nickd
Ah sorry, I'm just too used to have `set gdefault` in my .vimrc
kemp
+1  A: 

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 <F3> :%s!<!\&lt;!g\|:%s!>!\&gt;!g<CR>
nickd
+1  A: 

If you are using this search/replace pattern to HTML encode entities, you might want to check out the unimpaired plugin. Amongst other things, this provides shortcuts for encoding and decoding XML, URL and C strings.

nelstrom
+1  A: 
:TOhtml

will create a new buffer containing your previous buffer HTML-ized, including entity escaping (and syntax highlighting, if you had that enabled). See :h TOhtml for more information.

ephemient