8

When within vim (the editor I use mostly because of the terminal integration), I want to run my script (e.g. Python or Javascript) from within vim.

I do not mind if it opens a new window, I would just like to know if there is an editor command for save/run. TNX1.0E6,

3 Answers 3

12

You can run commands inside of vim with !

:!echo "hello"

The symbol % is a shortcut for the name of the current open file, so you can execute your current file with

:!%

The console output will appear, and you press enter to return to editing your file

You can add a keybind to save and run in your ~/.vimrc

nnoremap <F5> <esc>:w<enter>:!%:p<enter>
inoremap <F5> <esc>:w<enter>:!%:p<enter>
1
  • I had to use :!%:p which p stands for full path otherwise I got the error: "shell returned 127"
    – alchemy
    Apr 3, 2022 at 23:04
1

I specifically use this to compile and view LaTeX documents in vim:

map     <F5>            :! pdflatex "%"<CR><CR>
map     <F6>            :! xdg-open "%<.pdf";<CR>

the "%<" gets the name of the current file in buffer without the extension, (i.e., when I'm editing book.tex, it returns book so I can attach my own ending.

0

You can set up a Makefile in your project folder and then use vim's built-in :make command. Since Makefiles are ridiculously powerful and aren't limited to compiling C applications, you can set it up to call your script and do any additional processing that is needed.

The contents of the Makefile would look like:

all:
        yourscript.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .