Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 988
thornomad Avatar asked Feb 24 '10 13:02

thornomad


People also ask

How to map a key to a command in Vim?

To map a sequence of keys to execute another sequence of keys, use the ':map' command. For example, the following command maps the <F2> key to display the current date and time. The ':map' command creates a key map that works in normal, visual, select and operator pending modes.

How do I save Ctrl S in Vim?

The second line says: pressing Ctrl - S within a document while in 'insert' mode will escape to normal mode, perform a :w <enter , then press a to get back into insert mode.

What is noremap in Vim?

noremap is the "root" of all non-recursive mapping commands. The root form applies to the same modes as map . (Think of the nore prefix to mean "non-recursive".) (Note that there are also the ! modes like map! that apply to insert & command-line.)

What does Vimrc stand for?

It's either short for "runcom", which is an abbreviation for "run commands"; or it's "run control". Other uses for the abbreviation are: runtime configuration. resource control. release candidate.


1 Answers

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>
like image 193
nickd Avatar answered Sep 21 '22 11:09

nickd