Background: I'm using this mapping map ,r :! bundle exec ruby %<CR>
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>
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 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 <Cr>
:
: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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.