Wednesday 4 April 2018

mysql - How do I create a shortcut for a find and replace command in vim?


I have a vim (well, sed really) find and replace pattern I use for resetting the AUTO_INCREMENT property in mySQL CREATE TABLE statements:



:%s/AUTO_INCREMENT=[0-9]*/AUTO_INCREMENT=1/g



I use this command often, and would like to create a shortcut in vim. I know I will have to edit my .vimrc, but I can't figure out the cmap syntax.


My end goal is to be able to run



:reset-auto-increment



And run the above find and replace globally throughout the file.



Answer



For that goal, I think a user-defined command would be better than a cmap. See


:help 40.2
:help user-commands

Vim requires that user-defined commands begin with an upper-case letter and contain only letters and digits. No hyphens. So your command will have to be something like ResetAutoIncrement.


command ResetAutoIncrement %s/AUTO_INCREMENT=[0-9]*/AUTO_INCREMENT=1/g

You can avoid typing the whole command name each time you use it by typing just the first few characters, then hitting Tab.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...