Serge Vinogradoff
Serge Vinogradoff

Reputation: 2272

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

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.

Upvotes: 4

Views: 390

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

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>

Upvotes: 4

Related Questions