2

Background: I'm using this mapping map ,r :! bundle exec ruby %<CR> all the time to easily run test file I'm currently editing. However I also do:

map ,t :! bundle exec ruby test/integration/some_integration_test.rb<CR>

so that I can run this particular integration test with two key strokes while working on some other file. It has hard-coded path, when I move to some other area of the application I type above command with another integration test file path.

How to make a command that creates such mapping automatically?

map ,T :map ,t :! bundle exec ruby %<CR> won't work because it doesn't expand % while creating the mapping, so ,t will always run current file (while I want it to run file I was editing at the time of creating the mapping).

[EDIT]: Slightly adjusted solution from the answer:

nnoremap ,r :! bundle exec ruby %<CR>
nnoremap ,T :let g:testfile = expand('%:p')
nnoremap ,t :! bundle exec ruby <C-r><C-r>=g:testfile<CR><CR>
  • ,r always runs file under cursor
  • ,T saves current file path under in a variable for running later
  • ,t runs file path stored previously
asked Sep 25, 2012 at 9:06

2 Answers 2

3

Just save the current file name (best with a full absolute path to be immune against changes to the current directory; the :p modifier does that) in a variable, and insert the variable instead of % via <C-r>:

:noremap ,r :let g:testfile = expand('%:p')<Bar>! bundle exec ruby %<CR>
:noremap ,t :! bundle exec ruby <C-r><C-r>=g:testfile<CR><CR>

PS: I used :noremap, because that is generally safer and should be the first choice unless you really need remapping to occur.

answered Sep 25, 2012 at 9:31
0
1
execute ":nnoremap ,t :! bundle exec ruby ".shellescape(expand('%:p'))."<CR>"

creates a ,t mapping that will execute your test on what is the current file at the time of execution.

But each time you do that you'll lose the previous ,t. I'm not sure that's what you want to do.

answered Sep 25, 2012 at 9:34
1
  • "But each time you do that you'll lose the previous ,t. I'm not sure that's what you want to do." - yes that's exactly what I want Commented Sep 25, 2012 at 9:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.