Get file name and directory in .vimrc fileWhy is make running against a wrong file?a highlight command resets previously declared highlightsWhy cannot I source vimrc for the current file being editing?Sourcing Vimplug plugins in a separate fileWhy can't I set <Home> or t_kh in my vimrc file?Common vimrc config for unix and windowsCannot set (let) mapleader and use <leader> in global vimrc
Why do aircraft leave cruising altitude long before landing just to circle?
Are there any OR challenges that are similar to kaggle's competitions?
Yes/ No : The sum of two ideals of a ring R is an ideal of R
Number of matrices with bounded products of rows and columns
Do predators tend to have vertical slit pupils versus horizontal for prey animals?
Gofer work in exchange for LoR
From where do electrons gain kinetic energy through a circuit?
The Lucky House
Expressing a chain of boolean ORs using ILP
Heyawacky: Ace of Cups
What's the point of writing that I know will never be used or read?
Adding things to bunches of things vs multiplication
Can I use images from my published papers in my thesis without copyright infringment?
Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)
Trying to understand how Digital Certificates and CA are indeed secure
Ending a line of dialogue with "?!": Allowed or obnoxious?
How to use the passive form to say "This flower was watered."
Why is su world executable?
Have made several mistakes during the course of my PhD. Can't help but feel resentment. Can I get some advice about how to move forward?
Interaction between Leonin Warleader and Divine Visitation
When and which board game was the first to be ever invented?
Do I need to start off my book by describing the character's "normal world"?
How to prevent criminal gangs from making/buying guns?
Unsolved Problems due to Lack of Computational Power
Get file name and directory in .vimrc file
Why is make running against a wrong file?a highlight command resets previously declared highlightsWhy cannot I source vimrc for the current file being editing?Sourcing Vimplug plugins in a separate fileWhy can't I set <Home> or t_kh in my vimrc file?Common vimrc config for unix and windowsCannot set (let) mapleader and use <leader> in global vimrc
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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?
vimrc variables
add a comment |
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?
vimrc variables
add a comment |
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?
vimrc variables
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?
vimrc variables
vimrc variables
asked 8 hours ago
Hashir SarwarHashir Sarwar
114 bronze badges
114 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use % to expand to the current filename.
See :help :!, which goes into quite some level of detail about that!
If
cmdcontains%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.
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
7 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
7 hours ago
|
show 2 more comments
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.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f20924%2fget-file-name-and-directory-in-vimrc-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use % to expand to the current filename.
See :help :!, which goes into quite some level of detail about that!
If
cmdcontains%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.
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
7 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
7 hours ago
|
show 2 more comments
You can use % to expand to the current filename.
See :help :!, which goes into quite some level of detail about that!
If
cmdcontains%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.
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
7 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
7 hours ago
|
show 2 more comments
You can use % to expand to the current filename.
See :help :!, which goes into quite some level of detail about that!
If
cmdcontains%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.
You can use % to expand to the current filename.
See :help :!, which goes into quite some level of detail about that!
If
cmdcontains%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.
answered 8 hours ago
filbrandenfilbranden
2,2555 silver badges15 bronze badges
2,2555 silver badges15 bronze badges
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
7 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
7 hours ago
|
show 2 more comments
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
7 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
7 hours ago
Ahem. Already answered.
– B Layer
8 hours ago
Ahem. Already answered.
– B Layer
8 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
8 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
8 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
8 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
8 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
7 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
7 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
7 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
7 hours ago
|
show 2 more comments
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.
add a comment |
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.
add a comment |
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.
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.
edited 8 hours ago
answered 8 hours ago
B LayerB Layer
7,0961 gold badge6 silver badges25 bronze badges
7,0961 gold badge6 silver badges25 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f20924%2fget-file-name-and-directory-in-vimrc-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown