0

I'm following a Neovim tutorial on LaraCasts from Jess Archer, setting up Neovim.
I've used Vim for a while, but I'm new with Neovim. You can see Jess Archers dotfiles here: Jess Archers Dotfiles.

I would like to set it up, so whenever I open a new file using Telescope, that it automatically saves and closes the file (buffer) I have open, to avoid a bunch of open buffers like this:

Neovim - open buffers

Here is my current telescope-settings file:

local telescope = require('telescope')
local actions = require('telescope.actions')

vim.cmd([[
  highlight link TelescopePromptTitle PMenuSel
  highlight link TelescopePreviewTitle PMenuSel
  highlight link TelescopePromptNormal NormalFloat
  highlight link TelescopePromptBorder FloatBorder
  highlight link TelescopeNormal CursorLine
  highlight link TelescopeBorder CursorLineBg
]])

telescope.setup({
  defaults = {
    path_display = { truncate = 1 },
    prompt_prefix = '   ',
    selection_caret = '  ',
    layout_config = {
      prompt_position = 'top',
    },
    sorting_strategy = 'ascending',
    mappings = {
      i = {
        ['<esc>'] = actions.close,
        ['<C-Down>'] = actions.cycle_history_next,
        ['<C-Up>'] = actions.cycle_history_prev,
      },
    },
    file_ignore_patterns = { '.git/', 'dist/', 'node%_modules/' },
  },
  pickers = {
    find_files = {
      hidden = true,
    },
    buffers = {
      previewer = false,
      layout_config = {
        width = 80,
      },
    },
    oldfiles = {
      prompt_title = 'History',
    },
    lsp_references = {
      previewer = false,
    },
  },
})

require('telescope').load_extension('fzf')
require('telescope').load_extension('live_grep_args')

vim.keymap.set('n', '<leader>f', [[<cmd>lua require('telescope.builtin').find_files()<CR>]])
vim.keymap.set('n', '<leader>F', [[<cmd>lua require('telescope.builtin').find_files({ no_ignore = true, prompt_title = 'All Files' })<CR>]]) -- luacheck: no max line length
vim.keymap.set('n', '<leader>b', [[<cmd>lua require('telescope.builtin').buffers()<CR>]])
vim.keymap.set('n', '<leader>g', [[<cmd>lua require('telescope').extensions.live_grep_args.live_grep_args()<CR>]])
vim.keymap.set('n', '<leader>h', [[<cmd>lua require('telescope.builtin').oldfiles()<CR>]])
vim.keymap.set('n', '<leader>s', [[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]])

Solution attempt 1: Read Telescope's Github page and Telescope's help-pages

I read here: Telescope on Github.

And I also read this: :help telescope. But there is a lot! I can't find what I'm looking for. I was searching for these possibilities:

  • 'Save and close buffer when opening new file'
  • 'Disable buffers'
  • 'Open new file i current buffer' (I know that this is now actually what happens, but that's what if kind of feels like for the user).

Solution attempt 2: Setup (another) auto-save plugin

I looked at auto-save.nvim. It just seems a bit extensive to add an entire plugin for something that might be handled directly by Telescope.

1
  • I use :LeaderfBuffer to navigate buffers. Probably telscope has this feature as well. (leaderf plugin)
    – eyal karni
    Mar 8 at 0:11

2 Answers 2

0

You don't close a buffer (see :help buffers and esp. :help buffer-hidden). But you can delete or wipe it out, if you don't want to see it :ls or tabline-configured-to-show-buffers. Saving everything is just :wall (:wa for short).


If the tabline bothers you, I would recommend displaying buffers differently; say, only on demand via :ls[!].

If having lots of modified buffers around bothers you, try running :wall or play with :set nohidden (you have to write to load a different buffer in the window, unless you use :hide).

Also consider that having lots of buffers around is not necessarily a bad thing; if you find that it slows you down trying to navigate to a file, consider any of the following alternatives:

  • :args &co.
  • the quickfix and location lists (esp. commands like :grep)
  • tags
  • :sbuffer and :buffer with command-completion (<Tab> by default of wildchar)
1
  • It is sometimes bad to keep very big files open.
    – eyal karni
    Mar 8 at 0:12
0

When using telescope to find files, I close all buffers other than unsaved or current buffer:

command! BufOnly execute '%bdelete|edit #|normal `"'  "close other buffers (silent! to try-catch error)


autocmd FileType nerdtree nmap <silent>zp :lua require('telescope.builtin').find_files{ find_command = {'rg','--files','-g','!node_modules/**','-g','!media/**'} }<CR>


nmap <silent>zp :silent! BufOnly<CR>:lua require('telescope.builtin').find_files{ find_command = {'rg','--files','-g','!node_modules/**','-g','!media/**'} }<CR>
1
  • Welcome to Vi and Vim! I find that having buffers I like open actually helps (easier to navigate to them, completion, etc.).
    – D. Ben Knoble
    Sep 14 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.