Vim: Run selected code in a persistent REPL-environment - vim

R, Python, Scala etc. all come with REPL-environments, which I don't want to miss, however, most of the time editing text in them sucks, so I edit the code in vim, paste it and look at the output and edit the code in vim again.
I can run the current file with !python % and I can run the current line with even more vim magic, however, this will start a new process of the interpreter.
Is it possible to start a REPL and send lines of code to the running REPL (and get the results back, obviously)?

Maybe one of these two plugins is what you need:
slime is screen-based.
tslime is a tmux-based version of slime.

Try Conque:
"""
Conque is a Vim plugin which allows you to run interactive programs, such as bash on linux or powershell.exe on Windows, inside a Vim buffer.
"""
It can easily be configured to open a Python interpreter, and a key mapping can be used to transfer the current line to it to be executed (F9 for the current line, F10 for the current file etc.).

Not for plain-python alone, but if you're using IPython 0.11 or later, take a look at vim-ipython.
Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object?<enter> and object.<tab> in IPython. Additionally, vim-ipython has a "shell" mode, where as you send lines to IPython, you get to see the results those lines produced back in the specialize buffer. See the second screencast on this post

I recently wrote a plugin for a very similar purpose: vim-notebook which allows the user to keep a background process alive and to make it evaluate part of the current document (and to write the output in the document). It is intended to be used on notebook-style documents containing pieces of code to be evaluated.

Maybe you can try my plugin vim-repl. It provides a convince repl environment for vim using the vim8 terminal feature.
here is the github homepage: https://github.com/sillybun/vim-repl.
To open the repl environment, just run :REPLToggle or you can even bind it witk key like:
nnoremap <leader>r :REPLToggle<Cr>
To interact with repl, you just select the code and press <leader>w. And the code will be transmitted to the repl environment.
Look into the github homepage for more details, it will worth your time.

Related

Vim autocompletion in terminal mode

When in insert mode, a list of completion candidates can be shown by pressing Ctrln (for insert mode completion), or CtrlxCtrlo (for Omni completion when omnifunc is set). However, these keybindings do not work in terminal mode (:term).
Question: how can I get a list of completion candidates when in terminal mode? I was expecting to be able to make vim display completion candidates like grep, kill, cd, python3, and file paths like /home/user/Desktop, /home/user/Documents, etc.
I am using vim 8.0.
In this case a terminal is shown in vim, so your input goes to the terminal not to vim. That means we have to use the completion your terminal offers you. tab should be working like always, for a vim style completion you could add that to your .inputrc:
# cycle forward
Control-n: menu-complete
# cycle backward
Control-p: menu-complete-backward
A :terminal is just a view into an interactive shell (or another command started by or in place of the shell). Vim offers some integration (pass on typed keys, show output), but it's the shell who's running the show.
As Vim does not know what's going on there (you could have launched a command where completions for file system paths or commands, as you've suggested, would not make any sense; you could have ssh'd into another system that does not have python3 installed), it cannot and should not offer you these things. Instead, as others have already commented, this is the job of the shell (and most of them provide completion, some (like Bash) even very powerful and extensible ones that go far beyond your suggestions) or the command that is running.

Can I use vim-like commands in the WIndows Command line in Windows 10?

Because I use vim (and obviously like it) while working in the command line, I often go through a few keystrokes before I realize I'm not in vim anymore. That's not a huge problem, but when entering a lot of commands on one line, vim-like navigation would really speed things up on the extremely rare occasion cough I make a typo. It would be great it there were a way to make command line typing act at least a little like vim (e.g. Normal mode, Insert mode, movement from the home keys, etc.)
I am open to using a different shell, but would prefer something I can use without learning a ton of new commands (e.g. command line or BASH like). I have else compiled many of my frequent command sequences into .bat files, which I would prefer not to have to change too much to get to work properly.
When using Windows, a good option would be to install Git, which gives you "Git Bash", which is their distribution of mingw.
Now that you have bash, you can use set -o vi which puts your readline in Vim mode.
https://sanctum.geek.nz/arabesque/vi-mode-in-bash/
I'm surprised nobody has mentioned Clink. It works with cmd.exe, and has support for Vim (well, technically, Vi) key mappings. Sadly, the documentation seems unfinished (to say the least); that said, I have found it to be quite stable, and have used it for a few years now. Doing a quick search yields information on how to set the default editing mode to Vi (versus Emacs).
In short Control+Alt+J switches to Vi mode (assuming one is currently in the default Emacs mode), and a standard Readline inputrc in the right place (my file path is %HOMEPATH%\AppData\Local\clink\clink_inputrc), with the right directive (set editing-mode vi) ensures that a new cmd.exe invocation starts off in Vi mode.
I am open to using a different shell, but would prefer something I can use without learning a ton of new commands (e.g. command line or BASH like)
In bash you can enter vi-mode with the command:
set -o vi
This will make shell-editing very similar to Vim-editing. For a cheat-sheet of vim-mode commands see: https://github.com/pkrumins/bash-vi-editing-mode-cheat-sheet/blob/master/bash-vi-editing-mode-cheat-sheet.txt

Return focus to vim when using screens plugin after ScreenShellFocus?

The screens plugin turns vim into a kind of dreampie, only its a whole lot better because you have more control over what gets send even when you have multiple buffers and specific text within those buffers.
To follow my environment setup, merely install screens with vim, and then at the command line, do screen, reach the shell, then type vim in. Edit a shell script of some kind with it, then do :ScreenShell *shell type here, eg python/bash/irb* which spawns a shell below. You can use :ScreenShellSend to send the visually selected text to the shell or all of the file if none is selected.
My problem is, say I want to restart the shell that I'm running using this plugin and sending it some text. I need to do :ScreenShellFocus, call exit, and then start it up again. But then I need to be able to return to the same vim session that is directly above the shell that I'm affecting. I realize that I can use screen to just reach the vim session, but the only way I can do that now is to select it with ^a 1, which really only replaces the lower portion, the one that supposed to be the shell, with the vim buffer being edited. This is stupid because now you have vim buffer on top of vim buffer.
So my question is, how do I return the cursor to the vim session above after doing :ScreenShellFocus?
(yes I know my example of killing and restarting a shell may be circumvented by other logic, but I'm not that acquainted with the GNU screen util, and I think there's a way to do it like this, which I think preserves the workflow of the programmer)
<C-a><Tab>
is the mapping used in screen to cycle through "regions". Since you have only two, it moves the focus to the other region.
Since you are effectively using screen, you should learn how to use it to make this plugin really useful.
$ man screen

is there a way to see output of previous ":! g++ %" without rerunning?

When coding, I like to check the code by running :! g++ %. I map the command to <F5>. sometimes it takes a while to compile and I want to see the errors without spending time recompiling. Also, sometimes I want to compare the new output to previous one.
Is there a way to see the previous output of :! ...?
Provided you have g++ configured with makeprg, you can use :copen to reopen the last list of errors from the :make command.
set makeprg=g++\ %
Then, to compile, use
:make
When the compile completes, any errors will be listed in the quickfix window, which can (assuming errorformat is correctly configured) be used to jump to the lines on which errors occur. This usually works out of the box for C/C++.
If you dismiss the quickfix window, retrieve the last error list with
:copen
Review :help quickfix and :help makeprg for full gory details on how this works.
If you are using Vim from the terminal just suspend vim with Ctrl+z.
This will send you back to your terminal. You should see the commands that you just ran via :!. To get back to vim issue the fg. Note: This maybe different depending upon your shell.
:sh starts you a shell within Vim. If you're using Vim in a terminal, this switches back to what's known as the "primary buffer", which means that you see the history from the terminal before you started Vim, as well as any of the :! commands you ran in Vim.
You can get back to your Vim session with Ctrl-d.
Michael gave you the right answer: :makeis the way to go. The old :!compilation-command is to be forgotten. Vi is, well, ... history.
Now to clarify : you'd almost never have to tweak &makeprg. The default value is the one you want to use, unless:
you're using something else to compile (ant, ...)
you want to compile a mono file program, and you don't want write a Makefile, and you're under mingw whose gnumake installation s*cks (all other installations of gnumake do permit to transform foo.c/cpp into foo(.exe) without having to write any single Makefile)
you want to play with the compilation directory, or with things that can't be injected through $CXXFLAGS, $LDFLAGS, $CFLAGS, etc.

Vim: running a command in the editor instead of shelling out

When I do something like !git init inside of vim it switches over to bash and runs the command there then says "Press Enter to continue".
From what I've read and seen I thought it would run the command in the bottom area of vim (in a buffer maybe?).
Am I doing something wrong? Is there a setting I messed up on?
While that git plugin does look interesting I'm watching a TekPub demo where he is doing things like !bundle install as well and the result is just displayed at the bottom (under where he typed the command) in vim directly.
Also I am using vim in Ubuntu if that matters.
If you're specifically wanting to use Git, checkout Tim Pope's fugitive plugin.
http://www.vim.org/scripts/script.php?script_id=2975
This is the behavior I have (and have always had) when using console Vim. The behavior that you were expecting is what I get when using GUI Vim (e.g., MacVim or GVim). So, to answer your question: no, you are not doing anything wrong, and there are no messed up settings.
Use
:read !command
to insert output in current buffer. So, for example, if you want to insert the current date:
:r !date

Resources