Export Neovim Buffers to VS Code

Published

September 20, 2021

Reading time
9 min read

TL;DR - I wrote a new Neovim plugin called export-to-vscode.nvim that will take all of the active buffers from Neovim and open them as tabs in VS Code. The plugin will maintain the active buffer from Neovim as the active tab in VS Code along with the same line and cursor position.

So, you might have seen my earlier post where I explained my journey to using Neovim and Tmux for JavaScript (and TypeScript) development. Regardless if you did or not, you might be wondering why someone would intentionally export from Neovim to VS Code? Well, let me give you some reasons :)

Reasons You Might Want to Export from Neovim to VS Code

  1. You are just learning Neovim and you need to temporarily eject to VS Code to continue your work
  2. There is some feature that you are missing from VS Code (either it isn't nativley available yet, there is no plugin present, or you haven't learned about it yet)
  3. You are pair programming on some code and would feel more comfortable driving while using VS Code. Maybe you are still getting your feet wet and don't want Neovim to get in the way of your driving session.
  4. Do you have a reason to add to the list? If so, I'd love to hear about it.

Neovim Plugin

I'm new to Lua, but I figured this was a good challenge to write some lua and create my 1st plugin. I took a look at the Neovim Docs and VS Code CLI Docs in order to help me figure out what was needed. It also helped to reference a Lua Cheatsheet since I'm pretty new at the language.

I first developed the logic locally in my init.vim file inside a lua fence and once I felt good about the code, I pulled it out into its own plugin. I watched a helpful video by DevOnDuty called Create Neovim Plugins with Lua.

The Neovim plugin is called export-to-vscode.nvim. It will take all of the active buffers from Neovim and open them as tabs in VS Code. The plugin will maintain the active buffer from Neovim as the active tab in VS Code along with the same line and cursor position.

Code Walkthrough

If you are at all interested in how the underlying code works, I thought I'd give you a little tour of what the code looked like before I pulled it out into a plugin. If that does not interest you, feel free to skip to the Plugin Installation instructions.

Thanks goes out to Rodrigo Pombo (@pomber) for making the awesome Code Hike project. The below code tour uses the @code-hike/mini-editor.

init.vim
if (vim.api.nvim_buf_is_valid(buffer) and vim.bo[buffer].buflisted) then
if (vim.api.nvim_buf_is_valid(buffer) and vim.bo[buffer].buflisted) then
Click Next and Prev Buttons to Navigate Code Tour

Installation

To install the plug you can use your favorite pluging manager. I listed a few options below, but if yours is not listed then follow your normal technique to install the export-to-vscode.nvim plugin.

packer.nvim
use 'elijahmanor/export-to-vscode.nvim'

paq-nvim

paq 'elijahmanor/export-to-vscode.nvim'

vim-plug

Plug 'elijahmanor/export-to-vscode.nvim'

Usage

No default mappings are provided, so you'll either need to manually trigger the export function or create a custom mapping. Here are some examples of how you can use the plugin.

Manually Triggger from Command Mode
:lua require('export-to-vscode').launch()
Vim Script
nnoremap <silent> <leader>code <cmd>lua require('export-to-vscode').launch()<cr>
Lua
vim.api.nvim_set_keymap(
  'n',
  '<leader>code',
  '<cmd>lua require("export-to-vscode").launch()<cr>',
  { noremap = true, silent = true }
)

What About Vim? (not Neovim)

If you don't use Neovim and you would like something similar, I came up with another version (not in the plugin), that you can add to your .vimrc. This version doesn't try to maintain the active tab and line and curosor position. This is actually what I came up with before trying to learn how to write a Lua version, which I persaonlly prefer (see above).

NOTE: I referenced a post on Stack Overflow to help me write this version.

function! ExportToVsCode()
  let buffers = filter(range(0, bufnr('$')), 'buflisted(v:val)')
  silent execute "!code " . getcwd() . " " .
    join(map(buffers, 'fnamemodify(bufname(v:val), ":p")'))
endfunction
nnoremap <silent> <leader>code :call ExportToVsCode()<cr>

Comment and Share

Do you find this plugin handy? If so, why? I'd love to hear about it!

Feel free to share this post on Twitter and join in on the discussion.

Web Mentions
0
0

Tweet about this post and have it show up here!