Here I got simple task for skilled vimmers. I need to reformat my css file. There are commands to do this:
%s/}/&\r/g
%s/ / /g
retab!
echo "You done did it!"
But I don’t want to type these commands every time I need to format my css file (I get it after convert less file by WinLess program). Now I put these commands into cssformat.vim file, and put this file into vim runtime folder. In my vimrc I set:
autocmd Filetype css nmap :so $VIM/vim73/cssformat.vim
It’s works, of course. But I wonder how can I do this task better? In the begginig I want to put these commands in my vimrc (to create a simple function), but I don’t know how to do this correctly.
p.s. Sorry for my bad English.
Just put the commands from your script into a function:
And move the stuff into your
.vimrc. Now you can invoke this via:call ReformatCss().To top it off and make it even simpler, define your own command:
Now you can invoke via
:ReformatCss. Voila!You can learn more at
:help usr_40.txtand:help :command. For example, if you only need this for CSS files, you can turn this into a buffer-local command throughcommand -bufferand moving the function and command definition to~/.vim/ftplugin/css_reformat.vim