📜 ⬆️ ⬇️

Simplify the life of a programmer with vim + vim-slime + tmux

This publication tells how to save time when developing for Clojure and NodeJS, as well as Bash scripts, sending text from vim to the REPL, using tmux + vim + vim-slime. Also given are recipes from nodemon.

Most likely, vim-slime will work for other interpreted languages ​​(Ruby / Python / PHP / Perl ...). vim-slime also works with screen.
On Habré, both vim and tmux were sufficiently covered. I just wanted to show what can be obtained from their combination.
Adding from Fikys : works with Ruby.

If you know vim and tmux, and you are only interested in vim-slime - jump immediately to the second section.
')

Introduction


We all strive to be productive. After the code is written, we want to know if it works and get feedback. We came up with many ways to speed up feedback: static type inference during compilation and in IDE, unit tests, integration tests, REPL, LiveReload, etc.

For my small projects I use a bunch of REPL and unit tests, which allows me to receive feedback instantly.

I am a web developer. In my work and in my project, I usually make the frontend and the part of the back-end joining it. During the working session, I write PHP, phtml, Stylus, css, Coffeescript, Javascript, + sql queries and pushes to git; which is provided by a bunch of tmux and vim. There are also a couple of small CoffeeScript projects that use the combo tmux + vim + vim-slime + Coffeescript REPL. In the project session Clojure, CoffeeScript, Stylus are linked; tmux + vim + vim-slime + Clojure REPL. Under the cut, I will talk about three setups for three environments.

Setup 1 - “Work”


At work, I write phtml templates, connect data from Zend controllers to them, attach scripts and styles to templates. In order not to be confused in several different contexts, I spread everything across several tmux windows. One window and one running for php / phtml, stylus, coffee, js. Another four auxiliary windows for ssh, mysql, git and watching compilers (coffee, stylus).



Previously, I kept all the tabs in a single copy vim. tmux allowed to run several instances of vim and not be confused by them.

Setup 2 - “Coffeescript”


I am writing my own set of functions for js. This is an AMD module that turns into CommonJS, with the help of "30 lines of code." Freshly written CoffeeScript is elementarily tested with wim-slime and then goes under unit tests.

vim-slime

The vim-slime project, as described by its author Jonathan Palardi (jpalardy @ github), is the combination of a good editor (vim) with all the coolness of the REPL. I very remotely imagine how a real SLIME works, so I can’t give help on the original. In addition, for vim there is a solution closer to SLIME , but not so easy to install. All I need vim-slime for is send text from vim to the REPL. You open tmux, you divide the screen into two tabs - on the left you put vim, on the right your favorite REPL. You write code, press <Cc> <Cc> - the current paragraph flows into the REPL and is executed. Beauty.



Install and configure vim-slime

Owners of Vundle just put add to vimrc, and run PluginInstall:

Plugin 'jpalardy/vim-slime' 


The owners of Pathogen, respectively, clone their repository with a githaba.

By default, the plugin is configured to work with the screen. In order for it to work with tmux, you need to add a second line to vimrc:

 let g:slime_target = "tmux" 


When preparing the publication, it turned out that the plugin does not work with CoffeeScript out of the box (with JS all the rules). In my case, this was decided by including the second plug-in:

 Plugin 'kchmck/vim-coffee-script' 


Vim-slime adjustment

At work, it looks like this. Run tmux, divide the screen in two, open your favorite file in vim. On the second panel, run the REPL, for example, Coffee or NodeJS.

Here you have ready-made pieces of code in case the "laziness":

 // js function greeter(name) { console.log("Hey, " + name + "!") } greeter("Jude") # coffee greeter = (name) -> console.log "Hey, #{name}!" greeter("Jude") 


Select the piece of code that interests us through vim visual mode and <Cc> <Cc>. If you do not select anything, <Cc> <Cc> will send a paragraph (block of text without empty lines) to the REPL under the cursor.

When you first run the command for a session, vim-slime asks the name of the tmux socket and to which `tmux session: window, panel` to send text. The first question is answered with `default`, the second one is`: 0.1`. Enter! And the code appears in the REPL.

The configuration dialog can be called independently via `: SlimeConfig`. You can also send code to another terminal window, which can be convenient for people working with multiple monitors. Start the second window with tmux, open your REPL and in the first window with vim run:

 :SlimeConfig default 2:0,0 


Where the third line shows `session-name-tmux: window, tab`.



Added: If <Cc> <Cc> seems uncomfortable to you

You can take advice from dkiyatkin
The official documentation has a tip on how to remap the combination in the wim style:
 let g:slime_no_mappings = 1 xmap <leader>s <Plug>SlimeRegionSend nmap <leader>s <Plug>SlimeMotionSend nmap <leader>ss <Plug>SlimeLineSend 


Unit testing

I covered the most complex and root (used by other functions) functions of the library with the simplest (but sufficient) assert tests.

To see the test results in seconds and not be distracted by the routine, there is a nodemon. This is an npm package that feeds the js / coffee node at the start and every time the monitored files change. In the end, I learn about the test results every time I save the file.



To get such a lively second tab, something like it runs on it:

nodemon run-all-my-tests.coffee --watch my-changeable-files/*2


Setup 3 - Clojure


This publication would not have appeared if it had not stumbled upon some guy who described his setup .

Also for Clojure there is a vim-fireplace , which among other things is able to get documentation.

I preferred vim-slime for one simple reason - REPL.

The Kluchurov REPL server (nrepl) and the client (REPL-y, bundled with Leiningen) are able to do a lot of useful things by themselves. The behavior of vim-slime is as primitive and predictable as possible. Moreover, when the REPL-y is running in the next tab, you see the whole history and error traces, and besides the REPL-y capabilities themselves, tmux functionality for working with text is also available. You can switch to the REPL and perform there what you do not want to write and erase in the VIM, and switch back.



In general, the experience with Clojure REPL is the same as with NodeJS. The main difference from NodeJS REPL is that Clojure REPL has the concept of a namespace. Therefore, each time you want to send a specific piece of code to the REPL, you also have to manage to switch the namespace. This can be automated by adding to .vimrc:

 function! SendNs () let cpos = getpos('.') exec "1 SlimeSend" call setpos('.', cpos) endfunction autocmd BufWinEnter *.clj :call SendNs() autocmd TabEnter *.clj :call SendNs() 


Thus, each time you switch tabs in the REPL, the current namespace is set. Must be specified as the first line in the file. Most likely this can be automated more elegantly.

The remaining buns and little things


Bash & ssh

I am still delighted by the opportunity to open an ssh connection with Stuttgart (hetzner) on a nearby panel and transfer text from Moscow to it. Thus, you can simultaneously write code and debug remotely. Also this thing works with bash. In the screenshot in the left hand local vim, in the right hand deleted bash:



tmuxinator

To simplify the creation of tmux-sessions, I use a Ruby-project muxinator.

My .vimrc and tmux.conf

1. .vimrc
2. .tmux.conf

Honor


Not to mention the minimalist habrovich d3m1gd , who made his vim-slime . I would also like to say thanks to all the enlighteners telling about vim, tmux, and other * -NIX tools.

Source: https://habr.com/ru/post/236981/


All Articles