Lisp in Vim (2019) The article discusses the development of Lisp programming support in the Vim text editor, focusing on two plugins: Slimv and Vlime. Slimv has been available for over a decade, while Vlime is a newer plugin less than three years old, and both enable interactive Lisp development within Vim. The author provides a comparison of the two plugins, covering their features, setup, and usage, but notes the article is not a full tutorial. Lisp in Vim Susam Pal on 07 Dec 2019 Introduction Fifteen years ago, writing Lisp code in Vim was an odd adventure. There were no good plugins for Vim that assisted in structured editing of Lisp s-expressions or allowed interactive programming by embedding a Lisp Read-Eval-Print-Loop REPL or a debugger within the editor. The situation has improved a lot since then. In the last ten years, we have seen active development of two Vim plugins named Slimv https://github.com/kovisoft/slimv and Vlime https://github.com/l04m33/vlime . Slimv is over 10 years old now. Vlime is more recent and less than 3 years old right now. Both support interactive programming in Lisp. I am going to discuss and compare both Slimv and Vlime in this article. I will show how to get started with both plugins and introduce some of their basic features. I will not cover everything though. This is not a tutorial. For tutorials, see the References references section. If you are looking only for a comparison of the two plugins or a quick recommendation, jump directly to the Comparison of Slimv and Vlime comparison-of-slimv-and-vlime section or the section. quick-recommendation Quick Recommendation Contents Introduction introduction Background background Vim Plugin Management vim-plugin-management Software Versions software-versions Get Started get-started Use Debugger and Inspector use-debugger-and-inspector Trace Function trace-function Nifty Features nifty-features Other Common Lisp Implementations other-common-lisp-implementations Other Lisp Dialects other-lisp-dialects Comparison of Slimv and Vlime comparison-of-slimv-and-vlime Quick Recommendation quick-recommendation Disclosure disclosure References references Background Before we get started with Slimv and Vlime, it would be nice to take a brief look at the heritage behind these plugins. These plugins provide Lisp development environments for Vim, so their story begins with Lisp. Lisp Lisp is a family of programming languages with a distinctive, fully parenthesised prefix notation. It is quite unlike most of the other popular programming languages today like C, Python, Ruby, etc. Its homoiconic nature and its powerful macro system that can transform arbitrary Lisp expressions make it such a flexible, versatile, extensible and introspective language that articles describing Lisp often have the word "enlightenment" in them. For example, see the following articles: - by Paul Graham Beating the Averages - by Slava Akhmechet The Nature of Lisp - by Sinclair Target How Lisp Became God's Own Programming Language Lisp has been described in various ways by various eminent personalities in the history of computing. Alan Kay has famously described Lisp as: The greatest single programming language ever designed. John Foderaro has written this about Lisp: Lisp is a programmable programming language. Eric S. Raymond has expressed the enlightenment one experiences by learning Lisp in his famous article titled How To Become A Hacker http://www.catb.org/~esr/faqs/hacker-howto.html : Lisp is worth learning for the profound enlightenment experience you will have when you finally get it. That experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. Randall Munroe, the creator of the XKCD https://xkcd.com/ webcomic has dedicated two comic strips to Lisp: Developed in 1958 by John McCarthy, Lisp is the second oldest programming language in use today. Only Fortran is older, by one year. Some of the popular Lisp dialects today include Common Lisp, Scheme and Clojure. Most of this article would focus on Common Lisp. Scheme and Clojure would be discussed briefly towards the end of this article. Emacs: SLIME Many Lisp programmers immediately think of Emacs when they think of writing Lisp code. Emacs is a family of text editors. An Emacs editor itself is typically implemented in a dialect of Lisp. There is an Emacs mode named SLIME that provides excellent support for programming in Lisp. SLIME stands for Superior Lisp Interaction Mode for Emacs. First released in August 2003, SLIME was created by Eric Marsden and then later developed further by Luke Gorrie and Helmut Eller. It offers a Read-Eval-Print-Loop REPL , integrated debugging and interactive evaluation of expressions, all available right within the editor. There are several nifty key bindings available to compile and evaluate parts or whole of the code in the current buffer. SLIME works by launching a Swank TCP server. Swank is a backend server program written in Common Lisp that listens on a socket to receive SLIME commands from Emacs and execute them. SLIME is so useful that it is considered to be indispensible by many Lisp programmers who write Lisp code in Emacs. Vim: Slimv/Vlime Is there anything similar to SLIME for Vim? Yes, there are two popular options: - Slimv: It stands for Superior Lisp Interaction Mode for Vim. It is a Vim plugin created by Tamas Kovacs that was first released in January 2009. - Vlime: It is a Vim plugin created by Kay Z that was first released in May 2017. It is much more recent than Slimv. Vlime is younger than Slimv by eight years. Both plugins use a client-server architecture like SLIME does in Emacs. Both plugins rely on Swank server to be started. In fact, Slimv bundles a slightly modified version of Swank with it, so that it can launch it and connect to it to send expressions to be evaluated. Vlime does not bundle Swank server with itself but it provides a wrapper that automatically downloads Swank server when needed. Vim Plugin Management When I started using Vim fifteen years ago, we used to just download a Vim plugin and copy/extract it to ~/.vim directory. These days, there are a few plugin management tools for Vim such as Pathogen https://github.com/tpope/vim-pathogen , Vundle https://github.com/VundleVim/Vundle.Vim , vim-plug https://github.com/junegunn/vim-plug , etc. I am not going to use any of them because I don't know which one of them you use and I don't want to write down steps for each one of them. In fact, I have never used any Vim plugin manager myself. Until Vim 7, I used to create a ~/.vim/bundle directory, then copy each plugin to its own directory within it and add the plugin's directory path to Vim's runtimepath option. Vim 8 has native support for packages which makes installing plugins and loading them simpler. With Vim 8, we can copy each plugin to its own directory within ~/.vim/pack/plugins/start and they are loaded automatically when Vim starts. The directory name plugins in this path is only an example. It could be any arbitrary name and Vim would still load the plugins fine. In this article, I will use Vim 8's native support for packages to set up Vim plugins. The only exception to this would be installing Vlime. The top-level directory of Vlime is not the plugin directory. The plugin directory is contained in a subdirectory named vim . This does not conform to the directory structure of plugins in a Vim package. Therefore, in this article, I will set up Vlime in the old fashioned way by copying it to ~/.vim/bundle and then adding the path to its plugin directory to Vim's runtimepath option. Software Versions Since this article involves several layers of software, some of what is written here may not hold good in future as these various pieces of software change and evolve over time. Therefore, in this section, I will note down the versions of various software tools I used while writing this article. Here they are: - Debian GNU/Linux 10.1 buster - Vim 8.1 - Slimv Git repo last updated on 30 Nov 2019 with commit 47a0070 https://github.com/kovisoft/slimv/commits/47a0070 - Vlime Git repo last updated on 16 Oct 2017 with commit 065b95f https://github.com/l04m33/vlime/commits/065b95f - Paredit Git repo last updated on 30 Nov 2019 with commit d99905a https://github.com/kovisoft/paredit/commits/d99905a - Rainbow Parentheses Git repo last updated on 29 Oct 2015 with commit 27e7cd7 https://github.com/junegunn/rainbow parentheses.vim/commits/27e7cd7 - SBCL 1.4.16.debian - GNU CLISP 2.49.92 - ECL 16.1.3 - MIT/GNU Scheme 9.1.1 on Debian GNU/Linux 9.11 stretch - Clojure 1.10.1 - Quicklisp beta libraries last updated on 30 Nov 2019 - tmux 2.8 You will probably need only a tiny subset of the tools above depending on which sections in this article you would follow. Just pick the sections you want to try out and follow the steps written in them. They will walk you through the procedure to install the tools applicable to the sections you have picked. Regardless of which sections you pick, I recommend that you definitely go through the three "Get Started" subsections below. These subsections go into detail about some of the prerequisites such as support for tmux, Paredit, support for Python interface in Vim, etc. that are not covered in the other sections. The choice of Debian may look like an odd one. I want the commands and steps discussed in this article to be easily reproducible in a free and open source operating system. Debian happens to be my favourite. What works on Debian is easily reproducible on Ubuntu and other derivatives, often without any changes to the steps. I believe, it will not be too difficult to translate the steps provided for Debian to the steps that would work on another operating system. Note that Quicklisp a prerequisite for Vlime is still beta software at the time of writing this article. The actual steps to install Quicklisp may change in future. Check https://www.quicklisp.org/ https://www.quicklisp.org/ for the most up-to-date instructions to install Quicklisp. Get Started Get Started with Slimv and SBCL Here are the steps to set up Slimv and use it: - Install the tools required to set up a Common Lisp development environment with Slimv with this command: sudo apt-get install vim-nox sbcl tmux git The default Vim in Debian is vim.basic provided by the vim package which does not have support for Python interface. Slimv is written in Vim script, Lisp and Python 3, so it does need a Vim package that has support for Python interface. One such package is vim-nox that provides the vim.nox command. Installing it automatically updates the vim command to run vim.nox . Another such package is vim-gtk which additionally provides GUI support. The graphical Vim known as GVim can be launched with the gvim command. It runs in the desktop environment. For the purpose of this article, I will stick to vim-nox because it is lightweight. All steps meant for Slimv would run equally well on vim-gtk , MacVim and GVim.Installing tmux is optional. Slimv can launch Swank server automatically if Vim is running within tmux, GNU Screen or a desktop environment, so if you are using GNU Screen already, you don't need to install tmux. Also, if you are running Slimv in a desktop environment, you don't really need to install either tmux or GNU Screen, although you could if you would like to see Swank running in a separate tmux or GNU Screen window rather than a separate terminal window. In this article, I am going to assume that Vim is running within tmux. If you are going to run Slimv in a terminal without a desktop environment, tmux or GNU Screen, Swank server has to be run manually. Point 4 below explains how to do it. - Installing Slimv is pretty simple. Here is one way to do it: git clone https://github.com/kovisoft/slimv.git ~/.vim/pack/plugins/start/slimv vim +'helptags ~/.vim/pack/plugins/start/slimv/doc' +q That is it Slimv is set up. It's that straightforward. The commands above show how to set up Slimv with just two shell commands. You could also use a Vim plugin manager to install Slimv for you but I am not going to cover that here. - This is an optional step. Slimv supports starting Swank server automatically if you are running Vim in tmux, GNU Screen or a desktop environment. To start tmux, enter this command: tmux If you use GNU screen or a desktop environment, you don't have to run tmux. If you do not use tmux, GNU Screen or a desktop environment, then you must start Swank server manually as explained in the next point. - This step is necessary only if you are not using tmux, GNU Screen or a desktop environment. The following command shows how to start Swank server manually: sbcl --load ~/.vim/pack/plugins/start/slimv/slime/start-swank.lisp If you are using tmux, GNU Screen or a desktop environment, Slimv can start Swank server automatically when needed and you don't need to perform this step. - Create a new Lisp source code file, say, foo.lisp with this command: vim foo.lisp - To connect to Swank server, enter the following command in normal mode: , c If Vim is running within tmux, GNU Screen or desktop environment, Slimv would automatically launch Swank server and connect to it. After Slimv connects to Swank successfully, Vim window should split into two and the following prompt should appear in the new split window: CL-USER This is the integrated REPL. It is now alive and ready for interactive programming. We assume here that Slimv is using the default Slimv leader key , . If you have overridden the Vim leader key, then the Slimv leader key might be same as the Vim leader key. Enter the command :echo g:slimv leader in Vim command-line mode to find the leader key being used by Slimv. - Type some code into the buffer for the new file. To do so, first type i to enter insert mode and type this code: format t "hello, world~%" Type esc to return to normal mode. - To evaluate the current expression under the cursor, enter the following command in normal mode: , e Both the current expression and its result should appear in the REPL window. - The REPL is interactive. Type ctrl + w w to switch to the REPL window. Then type i to enter insert mode and type this code: + 1 2 Type enter to evaluate the expression just like you would do in a real REPL. The result should then appear in the REPL.Type esc to return to normal mode again. Use the normal mode command ctrl + w w to switch between the split windows. - Now that you have got started with Slimv, here is a brief note on uninstallation, in case you ever need it. If Slimv is installed as described in point 2 above, enter the following command to uninstall it: rm -rf ~/.vim/pack/plugins/start/slimv In steps 7 and 9, you may have noticed that as soon as you type an opening parenthesis or double quotation mark, a matching closing one is automatically inserted. That is done by the Paredit https://github.com/kovisoft/paredit plugin which is bundled along with Slimv. Paredit ensures structured editing of Lisp s-expressions and keeps all matched characters parentheses, brackets, braces, quotes balanced. It also provides many new keybindings to edit s-expressions conveniently. We will look into Paredit in a little more detail in the Get Started with Paredit get-started-with-paredit subsection later. Get Started with Vlime and SBCL Here are the steps to set up Vlime and use it: - Install the tools required to set up a Common Lisp development environment with Vlime with this command: sudo apt-get install vim sbcl git curl Note that unlike Slimv, Vlime can work with the default Vim in Debian, i.e. vim.basic . Vlime does not require Vim with Python interface. - Install Quicklisp with these commands: curl -O https://beta.quicklisp.org/quicklisp.lisp sbcl --load quicklisp.lisp --eval ' quicklisp-quickstart:install ' --eval ' exit ' sbcl --load ~/quicklisp/setup.lisp --eval ' ql:add-to-init-file ' --eval ' exit ' Type enter in the end, when prompted, to complete the installation. - Install Vlime and Paredit with these commands: git clone https://github.com/l04m33/vlime.git ~/.vim/bundle/vlime git clone https://github.com/kovisoft/paredit ~/.vim/pack/plugins/start/paredit echo 'set runtimepath^=~/.vim/bundle/vlime/vim' ~/.vimrc vim +'helptags ~/.vim/bundle/vlime/vim/doc' +'helptags ~/.vim/pack/plugins/start/paredit/doc' +q Unlike Slimv, Vlime does not bundle Paredit https://github.com/kovisoft/paredit along with itself. As explained in the previous section, it helps us with structured editing of Lisp s-expressions.I recommend that you install Paredit but in case you choose not to, ensure that loading of filetype plugins is enabled by entering the :filetype command in command-line mode. The output should contain plugin:ON . If it is off, add the command filetype plugin on to ~/.vimrc to ensure that this is always on. Vlime won't work without this being enabled. If you install Paredit, you don't have to bother about this because Paredit takes care of enabling this by default. - Create a new Lisp source code file, say, foo.lisp with this command: vim foo.lisp - To start Vlime server a wrapper around Swank server and connect to it automatically, enter the following command in normal mode: \ r r We assume here that Vim