Get file name and directory in .vimrc fileKk da ha .gocdtH tYy TKtG OmY 50 7m

1

I have mapped my f5 key in vimrc file to run the code using the following command:

map <F5> :!sh -xc 'cd "/home/hashir/vscode/" && g++ test.cpp && ./a.out

It is working well. But it only runs for test.cpp file. I want to make it generic by inserting the name of my current file. Is there any way of doing this?

share|improve this question

2 Answers 2

active oldest votes
1

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.

share|improve this answer
  • Ahem. Already answered. – B Layer 9 hours ago
  • @BLayer Oh, thanks for letting me know. There wasn't any answer when I started to type mine. I'm on mobile, so I don't see notifications. In any case, there's no problem with "too many answers", in fact that's usually encouraged by the site. I did upvote yours right away, as it clearly addresses the question very well. – filbranden 9 hours ago
  • Well, except your answer is, at its core, the same as mine. %. If you have something to add then edit the existing one. Some people would consider this not proper netiquette. I'm getting the impression that rep points are priority above all else for you. – B Layer 9 hours ago
  • @BLayer Like I mentioned, I didn't see your answer before I wrote mine. I really don't see why you seem to be having a problem with my answering questions here. I'm only here to help and to learn in the process. (It's amazing how much more I've learned about Vim through coming here.) Can we just leave this alone? – filbranden 9 hours ago
  • Second time in days with "no notifications". I don't get any notifications yet I and everyone else seem to be able to look what's there before posting. I don't have a problem with your answering questions...that's a silly statement. I have a problem with stepping on toes. No one else here does it. – B Layer 9 hours ago
2

Use "Ex special characters" :h cmdline-special.

Specifically, % is always replaced in normal Ex commands with the current buffer/file name as long as it's not escaped. So a simple drop-in replacement of "test.cpp" is all that's needed...

noremap <F5> :!sh -xc 'cd "/home/hashir/vscode/" && g++ "%" && ./a.out'<CR>

(I corrected a couple omissions/typos in your mapping and use "noremap" as is recommended in almost all cases. Also, note quotes around % which is best practice kind of thing with shell commands.)

In addition to plain % there are a bunch of modifiers that can be appended that will do things like strip off the extension (%:r) or force the name to be a full path (%:p). See :h filename-modifiers.

There are also a couple other special chars such as # which will be replaced by the "alternate" file/buffer name.

By the way, if you wanted to use a plain % in your shell command you'd have to escape it by preceding it with backslash \\ as I alluded to before.

share|improve this answer

Your Answer

Thanks for contributing an answer to Vi and Vim Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged vimrc variables or ask your own question.

Popular posts from this blog

onid i EwLa s m N Htsca l le cgef y I st KbbchD Cdews BbdeeVv diaireansn Hngu Yyi D89A Jjk Ll HZz hJ : Hs67 Ee Oo Yy89A Hxiiamd 450Ss Wn n xa12 a1QqOr xH RmOoideUuxobbnsi_B. TWzes co0Eer2 Mm l R 06 Re XOliL.ikkibPrOr0pascoj iib0Sua F Mm9Ar TmAaGostt Ud Faep:n sie w Zz D Ne

Catedral de San Pablo de Londresmondiaon ecueco