1

I'm writing a Fortran parallel code using MPI. I mapped ctrl-C to run make, in order to compile the program with proper options and dependencies. I also mapped ctrl-< to run the program executing the command mpirun -np 8 program, but this is limiting, since it runs the program on 8 processes. I would like to map the command so that I can press ctrl-< and then type n enter to run the program on n processes.

  • 1
    What range are you looking for for n? Will it ever be more than one-digit long? – James Jun 24 '16 at 20:55
  • Yes, it will be! – Enrico Maria De Angelis Jun 25 '16 at 7:38
2

Try this:

function! Mpirn()
  let n = nr2char(getchar())
  exe "!mpirun -np ".n." program"
endfunction

nnoremap <C-c> :<C-u>call Mpirn()<cr>

(Note, I had trouble mapping to <C-<> so I left it mapped to <C-c> You can change this to whatever mapping you already have.)

Pretty straightforward answer. Takes a single number as input, and runs that command. If you ever need to run this with a two digit or more number, you can do this instead:

function! MpirnArg(n)
  exe "!mpirun -np ".a:n." program"
endfunction

nnoremap <C-c> :<C-u>call MpirnArg(v:count)<cr>

However, the way you call this is in reverse. e.g, 17<C-c> Rather than <C-c>17

Also, I'm not familiar with fortran or MPI, so I don't know if 0 is valid. If 0 is not valid, change that last mapping to this:

nnoremap <C-c> :<C-u>call MpirnArg(v:count1)<cr>

Otherwise, you can leave it the same.

  • I come back to this question to ask for an explanation on a particular: why is <C-u> needed here? – Enrico Maria De Angelis Jul 16 '16 at 14:54
  • 1
    @EnricoMariaDeAngelis I personally have never run into issues while not using <C-u>. It's just kindof a "best practice" that I have learned and try to stick to. Essentially, it clears the command line before running, just in case vim inserts :'<,'> or something else. A better explanation is found here – James Jul 16 '16 at 14:58
0

I think what you're looking for is v:count. You can use this in your mapping to see what number was entered before the mapping was pressed. From :h v:count:

:map _x :echo "the count is " . v:count

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.