GithubHelp home page GithubHelp logo

artfulrobot / bufjump.nvim Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kwkarlwang/bufjump.nvim

0.0 0.0 0.0 22 KB

A neovim plugin that jump to previous and next buffer of the jumplist.

License: MIT License

Lua 100.00%

bufjump.nvim's Introduction

bufjump.nvim

bufjump.mov

Have you ever had to temporarily go to another file, perhaps previewing some changes with lsp go to refrenece, fiddle a bit, and have to jump back to the main file that you were working with? Instead of aimlessly smashing CTRL-o and CTRL-i, bufjump.nvim allows you to jump to previous or next buffer in the vim native jumplist with one single command.

Prerequistes

  • Neovim 0.5 or higher

Installing

with vim-plug

Plug 'kwkarlwang/bufjump.nvim'

with packer.nvim

use {
    "kwkarlwang/bufjump.nvim",
    config = function()
        require("bufjump").setup()
    end
}

Configuration

bufjump.nvim provides three options, forward, backward and on_success. forward and backward are the keymappings to jump to the next and previous buffer in the jumplist respectively. The default keymappings for forward and backward are CTRL-n and CTRL-p respectively.

Default configuration:

use({
    "kwkarlwang/bufjump.nvim",
    config = function()
        require("bufjump").setup({
            forward = "<C-n>",
            backward = "<C-p>",
            on_success = nil
        })
    end,
})

You can also bind the function forward and backward as followed

local opts = { silent=true, noremap=true }
vim.api.nvim_set_keymap("n", "<M-o>", ":lua require('bufjump').backward()<cr>", opts)
vim.api.nvim_set_keymap("n", "<M-i>", ":lua require('bufjump').forward()<cr>", opts)

on_success

on_success is a callback function that only executes after a successful backward or forward jump, which means that if there are no previous buffers in the jumplist, then on_success will not be executed.

Suppose that you want to jump to the last cursor position after exiting the buffer instead of the last cursor position in the jumplist stack, you can set the on_success function as followed:

use({
    "kwkarlwang/bufjump.nvim",
    config = function()
        require("bufjump").setup({
            forward = "<C-n>",
            backward = "<C-p>",
            on_success = function()
                vim.cmd([[execute "normal! g`\"zz"]])
            end,
        })
    end,
})

This will jump to the last cursor position before you left the buffer while also center the cursor to the middle of the screen. You can check :h last-position-jump for more information.

How it works

Under the hood, this plugin uses native CTRL-o and CTRL-i to jump until the buffer is different from the current buffer. If there are no previous or next buffer, then the command does not jump at all.

backward

When jumping to the previous buffer, it will jump to the last occurance in the jumplist that is different from the current buffer. Below is a simple illustration of the before and after position in the jumplist stack.

Before

Buffer 1    line 1
Buffer 1    line 2
Buffer 1    line 3
Buffer 2    line 10
Buffer 2    line 20     <--
Buffer 2    line 30

After

Buffer 1    line 1
Buffer 1    line 2
Buffer 1    line 3      <--
Buffer 2    line 10
Buffer 2    line 20
Buffer 2    line 30

forward

When jumping to the next buffer, it will jump to the last occurance in the jumplist that is different from the current buffer. Below is a simple illustration of the before and after position in the jumplist stack.

Before

Buffer 1    line 1      <--
Buffer 1    line 2
Buffer 1    line 3
Buffer 2    line 10
Buffer 2    line 20
Buffer 2    line 30

After

Buffer 1    line 1
Buffer 1    line 2
Buffer 1    line 3
Buffer 2    line 10
Buffer 2    line 20
Buffer 2    line 30     <--

bufjump.nvim's People

Contributors

kwkarlwang avatar david-kunz avatar fabiomcosta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.