You can use %
to expand to the current filename.
See :help :!
, which goes into quite some level of detail about that!
If {cmd}
contains %
it is expanded to the current file name. Special characters are not escaped, use quotes to avoid their special meaning:
:!ls "%"
Note also that :!
will already run the command in a shell for you, so you don't really need a sh -c '...'
around it.
The reference to the file will be relative to the current directory, so using cd
here doesn't make much sense, since this will only work in ~/vscode
. I'll assume that's the case and I'll remove that as well.
Putting it all together:
:map <F5> :!set -x; g++ "%" && ./a.out<cr>
Vim also has powerful features for building software. Those can be most useful when compiling projects spanning hundreds of source code files, but you could leverage them in your particular case too.
They can be quite complex, so it might take a while to get the hang of them. You can start at :help :make
, though you might also try to find easier to digest tutorials about how to use :make
for your specific language, compiler, build system, etc.