Skip to content

To send python code from vim to ipython running within a tmux split (with either visually selected code or with IPython like cells).

Notifications You must be signed in to change notification settings

jgors/vimux-ipy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 

Repository files navigation

vimux-ipy

This is a vim plugin for added python functionality built on top of vimux; thus, it assumes that the vimux plugin is installed as well. Also, it is adapted from vimux-pyutils, but I add some features & abstracted away many details to get everything up and running quicker.

There are two main uses:

  • First, it allows a block of visual-mode selected python code to be sent from vim to a tmux buffer split running iPython.

  • Second, "cells" of code can be sent from vim to the running iPython tmux split. Regarding this use case, the idea here is to be able to have code block execution similiar to that found in the iPython Notebook, though being able to stay within vim. Additionally, if the cell_delim arg (explained below) is set such that it is the same as what the iPython notebook uses to delimit its code cells, then the script can be uploaded & opened as an iPython notebook, and the iPython NB environment will recognize the delimited cell blocks.

####The workflow for this would be:

  • start a tmux session,
  • open the desired python script with vim,
  • then execute the command to open the iPython tmux split/pane (eg. Leader vip),
  • now execute code in the tmux pane with iPython by either visually selecting code to send, or send a block of code that is delimited into cells.

####A typical session would look like this (using cells for execution by executing the cell by being inside of that cell and hitting Leader+c):

Key mappings

key mappings that are enabled by placing the following code into .vimrc:

To open the iPython tmux split [Leader vip]:

map <Leader>vip :call VimuxIpy()<CR>

  • Optionally, an argument can be passed into VimuxIpy() to start ipython in a preferred way: eg. VimuxIpy("ipython --pylab"), or VimuxIpy("ipython --profile=some_cool_profile"). If no argument is passed to VimuxIpy(), then normal ipython will start without any flags set.

After the iPython tmux split is created, these keybindings are made:

  • Jump down into the tmux pane in copy(/vim) mode

Leader vi

  • Close the vim tmux split

Leader vx

  • Interrupt any command running in the tmux pane

Leader vq

  • Insert the same cell block delimiter that iPython uses to seperate their code blocks in the iPython notebook [# <codeblock>]

Leader vc


To send & execute visually selected code in the iPython tmux split [Leader e]:

vmap <silent> <Leader>e :python run_visual_code()<CR>


To execute the current "cell" in the iPython tmux split [Leader c]:

noremap <silent> <Leader>c :python run_cell(save_position=False, cell_delim='####')<CR>

Note, a cell is similar to an iPython Notebook code cell and is defined as a code block spanning from one cell_delim to the next cell_delim (explained below).

Two arguments can be passed into run_cell:

  • save_position: [default False]

    If set True, then the cursor will stay at the current location after the code cell is executed. If False, then the cursor will jump ahead to the beginning of the next code cell block.

  • cell_delim: [default '####']

    Code cells are delimited by the cell_delim argument -- this specifies what should seperate the code cell blocks. Note, there should be a cell_delim at the beginning of the first code cell, as well as at the end of the last code cell.

    As mentioned above, this arg can be set such that it is the same as what the iPython notebook uses to delimit its code cells: cell_delim='# <codecell>'
    Meaning, if cells are seperated with this as the cell_delim, then the script can be uploaded & opened as an iPython notebook, and the iPython NB environment will recognize the delimited code cell blocks -- NOTE, in order to be able to upload a script delimited in this way to the iPython notebook environment, the first thing at the very top of the script needs to be: # <nbformat>3</nbformat>

    for more details see:

    ipython notebook format stable

    ipython notebook format dev

About

To send python code from vim to ipython running within a tmux split (with either visually selected code or with IPython like cells).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published