Try this in your
~/.vim/vimrc file :
:autocmd BufWinEnter * !setxkbmap -option caps:swapescape
:autocmd BufWinLeave * !setxkbmap -option
Instead of the first line, you can use an alias in
alias vim="setxkbmap -option caps:swapescape && vim"
The drawback in these approaches being that as long as you haven't closed the vim, your CapsLock will be your Escape and vice-versa, machine-wide.
Solution to that:
For this, I am using timeout with the key-binding swap only during the required mode (that is, InsertMode):
au CursorHoldI * stopinsert
au InsertEnter * silent! let updaterestore=&updatetime | set updatetime=15000 | execute "!setxkbmap -option caps:swapescape" | redraw!
au InsertLeave * silent! let &updatetime=updaterestore | execute "!setxkbmap -option" | redraw!
Now, during the InsertMode, the CapsLock and Escape will interchange machine-wide, but since I have set a time-out for the InsertMode to be 15000, I need not worry about it as it will automatically be reversed in 15 seconds of inactivity. ;-)