m-demare/attempt.nvim

github github
utility
star 45
stars
alert-circle 0
open issues
users 1
subscribers
git-branch 2
forks
CREATED

2022-05-05

UPDATED

23 days ago


attempt.nvim

Manage your temporary buffers

Preview

preview

Features

  • Quickly create scratch files for any filetype, using vim.ui.select, vim.ui.input or any other method
  • Scratch files are saved in a temporary directory for later use, and can be accessed across neovim instances
  • Files are initialized with all the necessary boilerplate to be runnable
  • Reopen closed attempts with Telescope
  • Autosave attempts
  • Run the scratch files
  • Every file operation is asynchronous

Installation

This plugin is for neovim only. Version 0.7+ is required.

packer.nvim:

use {
  'm-demare/attempt.nvim',
  requires = 'nvim-lua/plenary.nvim',
}

-- Optional
use {
  { 'nvim-telescope/telescope.nvim' },
  { 'nvim-telescope/telescope-ui-select.nvim' }
}

vim-plug:

Plug 'nvim-lua/plenary.nvim'
Plug 'm-demare/attempt.nvim'

" Optional
Plug 'nvim-telescope/telescope.nvim'
Plug 'nvim-telescope/telescope-ui-select.nvim'

Usage

If you are ok with the default settings:

require('attempt').setup()

To change them

require('attempt').setup{
  dir = (unix and '/tmp/' or vim.fn.expand '$TEMP\\') .. 'attempt.nvim' .. path_separator,
  autosave = false,
  list_buffers = false,     -- This will make them show on other pickers (like :Telescope buffers)
  initial_content = {
    py = initial_content_fn, -- Either string or function that returns the initial content
    c = initial_content_fn,
    cpp = initial_content_fn,
    java = initial_content_fn,
    rs = initial_content_fn,
    go = initial_content_fn,
    sh = initial_content_fn
  },
  ext_options = { 'lua', 'js', 'py', 'cpp', 'c', '' },  -- Options to choose from
  format_opts = { [''] = '[None]' },                    -- How they'll look
  run = {
    py = { 'w !python' },      -- Either table of strings or lua functions
    js = { 'w !node' },
    lua = { 'w' , 'luafile %' },
    sh = { 'w !bash' },
    pl = { 'w !perl' },
    cpp = { 'w' , '!'.. cpp_compiler ..' %:p -o %:p:r.out && echo "" && %:p:r.out && rm %:p:r.out '},
    c = { 'w' , '!'.. c_compiler ..' %:p -o %:p:r.out && echo "" && %:p:r.out && rm %:p:r.out'},
  }
}
-- (You may omit the settings whose defaults you're ok with)

Telescope picker

To use the telescope picker for opening your attempts (recommended, to have a nice previewer), add this somewhere after your telescope.setup() call:

require('telescope').load_extension 'attempt'

You can customize this picker in the usual telescope way

Keymaps

By default, no keymaps are created. To use the basic presets, you can do:

local attempt = require('attempt')

function M.map(mode, l, r, opts)
    opts = opts or {}
    opts = vim.tbl_extend('force', { silent=true }, opts)
    vim.keymap.set(mode, l, r, opts)
end

map('n', '<leader>an', attempt.new_select)        -- new attempt, selecting extension
map('n', '<leader>ai', attempt.new_input_ext)     -- new attempt, inputing extension
map('n', '<leader>ar', attempt.run)               -- run attempt
map('n', '<leader>ad', attempt.delete_buf)        -- delete attempt from current buffer
map('n', '<leader>ac', attempt.rename_buf)        -- rename attempt from current buffer
map('n', '<leader>al', 'Telescope attempt')       -- search through attempts
--or: map('n', '<leader>al', attempt.open_select) -- use ui.select instead of telescope

See :h attempt-interface for more customized setups