An autocommand command is executed when one event occurs. You want a command to be executed after a sequence of events has occurred. One way to do that is like this:
autocmd FileType c,cpp,python
\ autocmd BufWritePre <buffer> call StripTrailingWhiteSpaces()
The <buffer> pattern causes the autocommand to be be triggered when the current buffer is written. See
:help autocmd-buflocal
Update
The solution above is pretty simple and has some flaws that were discussed in the Comments. Here is a more complete solution that addresses some of those flaws. It puts the autocommands in a group and deletes the BufWritePre autocommand, if one exists, before creating a new one. It still creates one autocommand per buffer, but only one.
augroup TrailSpace
autocmd FileType c,cpp,python
\ autocmd! TrailSpace BufWritePost <buffer> call SkipTrailingWhiteSpaces()
augroup END
Another solution, similar to the answer posted by lcd047, now deleted, is to recognize that when the FileType event occurs, the 'filetype' option is set. Then you can condition the response to the BufWritePost event on the value of 'filetype', as in the following example. It has the advantage over the other solutions that only one autocommand is created.
autocmd BufWritePre * if count(['c','cpp','python'],&filetype)
\ | call SkipTrailingWhiteSpaces()
\ | endif
FileTypeautocmd in the answer would have already set up the second autocmd (BufWritePre) to fire when you save them. – VanLaser Jul 17 '15 at 15:41FileTypeautocmd above will fire for every file you open with the correct filetype, and will setup a buffer-local event for each of those files. So if you run:wa, vim will run registered events for each buffer, before saving to file. – VanLaser Jul 17 '15 at 15:48:w, the event will run only for the current buffer) – VanLaser Jul 17 '15 at 15:58autocmds instead of a single one, all on write. Then if, say, 3 of these files get hidden, then get shown again,FileTypegets re-triggered so you get 3 moreautocmds, also on write. This is brilliant, I wonder why I didn't come up with this solution. :) – lcd047 Jul 17 '15 at 16:59stripTrailingWhiteSpaces()several times against the same file might have unintended consequences though. Also, the moreautocmds you have for the same event for the same file, the more likely you are to run into some really race conditions. Try searching vim_dev archives to get an idea. Then again, what do I know, it might just work for you, right? – lcd047 Jul 17 '15 at 17:34augroupwith an initialautocmd!would solve all problems) – VanLaser Jul 18 '15 at 15:04vimrc) don't help so much as a short incomplete answer which makes the user sweat a little (which is the real help). – VanLaser Jul 18 '15 at 16:54autocmds. In any case, this shows the nature of this site: the guy who asks and those who visit the site check and give points, and the more knowledgeable guys compete between themselves, in something that the original OP doesn't care or sometimes even understands. – VanLaser Jul 20 '15 at 18:08expected function name. vimL is too cryptic to me to understand it. – Arne Nov 13 '15 at 05:08