0

Currently, after writing a scala program I have to invoke the three commands:

  1. File save
  2. compile the file
  3. run the program

How to assign a shortcut for steps 2 and 3 above?

1
  • Hi @prateek, are you still searching a solution to your problem? Could you tell us which part of your question is not already answered? Commented Feb 26, 2023 at 5:23

1 Answer 1

0

If your command to compile is: :scalac -classpath . % and your command to run is :terminal scala -classpath . %:r to associate them to the shortcuts F7 and Ctrl-F5 respectively you could add the following lines in your init.vim file:

nnoremap <F7> :scalac -classpath . %<CR>
nnoremap <C-F5> :terminal scala -classpath . %:r<CR>

You could also configure Vim to associate :make to scalac that allow you to get the compiler error in the Neovim Quicklist (and to make use of the autowrite feature)

If you add the following lines to ~/.config/nvim/ftplugin/scala.vim

" Make that calling make automatically save the change in your buffers
setlocal autowrite

setlocal makeprg=scalac\ -classpath\ .
setlocal errorformat=%A--\ [%t%n]\ %m:%f:%l:%c\ -%#,%C%\\d%#\ %#\|%.%#,%C%\\d%\\+\ error%.%#

I had to transform the scalac output with sed (| sed s/\x1B\[[0-9]\+m//g) to remove the color escape characters for the errorformat to work.

And the following line in your init.vim:

nnoremap <F7> :make %<CR>
nnoremap <C-F5> :terminal scala -classpath . %:r<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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