0

Initial window:

vim opened first,then vert term split the left window

My expectation: to copy whole or a part of python code in the right window into the left window's python terminal and execute.

My .vimrc setting:

function! CopyPasteBuffer() 
    normal  gg"*yG
    wincmd p
    call feedkeys("\<C-W>\"*")
endfunction
nnoremap <leader>pp  :call CopyPasteBuffer()<CR>

function! CopyRun() range
    exe  a:firstline . "," . a:lastline . "y*"
    wincmd p
    call feedkeys("\<C-W>\"*")
endfunction
command! -range PassRange <line1>,<line2>call CopyRun()
nnoremap ,pr :CopyRun<cr>

In vim's normal mode in the right window, I type ,pp (leader configured as ,), all python statements in the right pushed into python terminal in the left and executed.

enter image description here

Now I want to execute a range of python statements in the right, move cursor in the right window and enter into ex mode, and type 1,3pr, the lines are executed but not in my desired python terminal such as the above image shows.

How to fix my vimscript?

After following Maxim Kim's suggestion,1,3PassRange works, I rewrite the mapping as nnoremap ,pr :PassRange<cr>, why can't 1,3pr get the same effect as 1,3PassRange?

enter image description here

Following Maxim Kim's instruction, I want to make more progress, toggle from uppercase to lowercase and from lowercase to uppercase, it is inconvenient.

Without range as argument:

nnoremap <leader>pp  :call CopyPasteBuffer()<CR>

I can type ,pp without uppercase to get desired result.
With range as argument:

command! -range  Xr <line1>,<line2>call CopyRun()

1,3Xr can work,i want to make ,1,3re map as 1,3Xr and call CopyRun,so add new mapping

map <leader>,re :Xr<cr>

It can't work, how to fix it?

0

You have to create a vim command that process the range and you actually have it in your code snippet: PassRange

So try :1,3PassRange

When you do :1,3pr you run builtin print ex command to print lines in range (See :h :pr).

You can't use normal mappings as an ex commands like you tried to do.

Shorten your :PassRange command to :Pr:

 command! -range Pr <line1>,<line2>call CopyRun()

And run it with :1,3Pr.

7
  • I rewrite the mapping as nnoremap ,pr :PassRange<cr>,why 1,3pr can't get the same effect as 1,3PassRange?
    – showkey
    Feb 17 at 8:49
  • @showkey, because mapping is not an ex command. You can't use it like this.
    – Maxim Kim
    Feb 17 at 9:36
  • @showkey rename PassRange to Pr and use it like :1,3Pr
    – Maxim Kim
    Feb 17 at 9:37
  • Please help make a little progress to use all lowercase characters ,i map as nnoremap ,re :Xr<cr>,why 1,3re can't work?
    – showkey
    Feb 17 at 10:44
  • @showkey you should definitely read about vimscript, mappings, commands somewhere... probably in vim user manual or some book. You are mixing mappings and user commands. Mappings do not work the same way as user commands. User commands can not start with lowercase letters. It is in vim help and user manual.
    – Maxim Kim
    Feb 17 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.