Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM - How to append current line number into key mapping?

Tags:

vim

I have this mapping in .vimrc:

nnoremap <F2> :w!<CR>:!rspec %<CR>

Which saves the current file and runs it in console with rspec.

How do I map another key to add current line to the end like this?

nnoremap <F3> :w!<CR>:!rspec %:<current_line_number><CR>

All I could find is CTRL+G which shows current position. But can't figure out how to turn it into mapping.

like image 386
Serge Vinogradoff Avatar asked Oct 16 '25 23:10

Serge Vinogradoff


1 Answers

How would you do that when typing the command-line? Probably with :help c_CTRL-R_= to insert an expression. Well, there you have the first option:

nnoremap <F3> :w!<CR>:!rspec %:<C-r>=line('.')<CR><CR>

Alternatively, you can evaluate the line number into the command with :execute:

nnoremap <F3> :w!<CR>:!execute 'rspec %:' . line('.')<CR>
like image 138
Ingo Karkat Avatar answered Oct 19 '25 20:10

Ingo Karkat