Use <C-r><C-w>to insert a word under the cursor on the command line:
:%s/<C-r><C-w>/bar/g
If you need to do this a lot, you can easily create a mapping as follows:
nnoremap <F6> :%s/<C-r><C-w>/
The one shown above inserts the first part of the command, ready for you to enter a replacement, any flag and press <CR>:
:%s/foo/
source
share