lazygit.nvim icon indicating copy to clipboard operation
lazygit.nvim copied to clipboard

(O)pen file and (e)dit file in the current nvim instance?

Open Nikola-Milovic opened this issue 3 years ago • 41 comments

Currently, the edit hotkey opens the file in the lazygits floating term and makes navigating back to the lazygit impossible. The open shortcut opens the file in the default text editor.

Is it possible for open and edit shortcuts to close the floaterm and just open the buffer in the nvim itself? I am currently on the default settings

Nikola-Milovic avatar Jul 30 '22 06:07 Nikola-Milovic

If I'm understanding correctly, if you install nvr, i.e. neovim remote, this is the default behavior.

kdheepak avatar Jul 30 '22 15:07 kdheepak

Does it work out of the box? To me it doesn't seem to behave as I'd expect it to. Would love to see someones setup

Nikola-Milovic avatar Jul 31 '22 16:07 Nikola-Milovic

I think it should work out of the box but setting GIT_EDITOR env var somewhere might mess with it. I have the following in my settings:

if vim.fn.executable("nvr") == 1 then
  vim.env.GIT_EDITOR = "nvr --remote-tab-wait +'set bufhidden=delete'"
end

All the tutorials out there use just --remote-wait instead of --remote-tab-wait but it completely messes up my nvim UI after writing the commit message. Might be that I've misconfigured something else somewhere or something but the snippet above has worked for me quite nicely.

okuuva avatar Sep 24 '22 12:09 okuuva

@okuuva can you expand a bit upon your setup, I am newish to this ecosystem. Where do you put this function, do you have any other setup going besides this? Do you start your nvim normally or do you use nvim remote?

Nikola-Milovic avatar Sep 25 '22 12:09 Nikola-Milovic

I have the following lines in my dotfiles:

https://github.com/kdheepak/dotfiles/blob/1c844ed92675e9feffa2e3bdf00ec1385611bb65/nvim/lua/kd/config.lua#L313-L318

You can add @okuuva or my snippet into your .vimrc or init.lua and it should work.

kdheepak avatar Sep 25 '22 12:09 kdheepak

I also have the following in my .bashrc:

if [ -n "$NVIM_LISTEN_ADDRESS" ]; then
    export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
else
    export VISUAL="nvim"
fi

export EDITOR="$VISUAL"

I'm not sure if this is completely necessary though. It's been a while since I messed around with this.

kdheepak avatar Sep 25 '22 12:09 kdheepak

Behaviour I'm experiencing:

  • When pressing 'C': Commit message file is opened in the current neovim instance
  • When pressing 'e': A new neovim instance is created inside the floating window and the file is opened there
  • When pressing 'o': The file is opened in my default system editor (For me it is just a browser :D)

I would like to set it up so that all of the above would be opened in the current neovim instance. I should have nvr setup precisely as the readme suggests.

samodostal avatar Oct 22 '22 06:10 samodostal

Same behaviour here. Set up as readme suggested. With latest version of nvr and lazygit.nvim. Let me know if you need more debugging information ^.^

monadplus avatar Oct 25 '22 19:10 monadplus

If anyone is looking for a vanilla way to edit files from lazygit directly in currently open nvim session, I managed to get it working like this:

# ~/.config/jesseduffield/lazygit/config.yml
os:
  editCommand: 'nvim'
  editCommandTemplate: '{{editor}} --server /tmp/nvim-server.pipe --remote-tab "$(pwd)/{{filename}}"'
# ~/.bashrc
alias vim='nvim --listen /tmp/nvim-server.pipe'

lawrence-laz avatar Nov 08 '22 20:11 lawrence-laz

That’s a neat trick! Would you like to submit a PR for that into the README?

kdheepak avatar Nov 08 '22 20:11 kdheepak

That’s a neat trick! Would you like to submit a PR for that into the README?

Sure, see if this makes sense: https://github.com/kdheepak/lazygit.nvim/pull/85

lawrence-laz avatar Nov 08 '22 22:11 lawrence-laz

Hey @lawrence-laz what's the behaviour you're experiencing with this?

When I use (o)pen a new terminal window is opened in regular vim (I see that your above command was only for editing?) And when I use (e)dit it opens up a floating terminal inside the current nvim session

Nikola-Milovic avatar Nov 12 '22 07:11 Nikola-Milovic

Hey @lawrence-laz what's the behaviour you're experiencing with this?

When I use (o)pen a new terminal window is opened in regular vim (I see that your above command was only for editing?)

And when I use (e)dit it opens up a floating terminal inside the current nvim session

I'm currently away from my machine, but I only use (e)diting, not sure about (o)pen.

The editing for me opens up in the same nvim instance full screen.

I can make a gif once I am back to show how it looks.

If it launches a new instance of nvim for you, it might be that your nvim didn't succeed in starting a server. You could try experimenting with it a bit with multiple terminals and the commands from config.

EDIT: Just an update with a GIF of how it works on my machine. The bottom split terminal windows is pinging the same nvim instance and nvim echoes my message. lazygit-edit-in-nvim

lawrence-laz avatar Nov 12 '22 08:11 lawrence-laz

If you are wondering how to make this work with multiple neovim instances, here is my approach:

The issue arises when starting two neovim instances using the same alias:

alias nvim='nvim --listen /tmp/nvim-server.pipe'

This causes both instances to listen to the same pipe, making it impossible to use. When attempting to open a file from lazygit, both instances try to open it.

In my workflow, I use tmux which enables me to create an alias like this:

alias nvim="nvim --listen /tmp/nvim-server-$(tmux display-message -p '#S').pipe"

This creates a pipe specific to the tmux session, solving the previous issue. The next step is to communicate with this pipe. This is my lazygit configuration:

os:
  edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"
  open: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"

With this, we can communicate with the correct pipe. In the --remote-send part, you can execute any neovim command you want. I call a Lua script that closes the floating lazygit window and opens the file in the buffer underneath it.

The current behavior that I am experiencing is:

  • When pressing 'e/o': The lazygit floating window is closed, and the file is opened in the window below it.

If you want to look at details you can check my .dotfiles

samodostal avatar Mar 16 '23 09:03 samodostal

Also, I guess this answers the original question. You can close the floating LazyGit window and open a file in the window underneath. The Lua script you need to call in editCommand and openCommand in LazyGit config file is:

return function(filename, line_number)
	line_number = tonumber(line_number) or 1

	vim.api.nvim_win_close(0, true)
	vim.api.nvim_command('edit +' .. line_number.. " " .. filename)
end

samodostal avatar Mar 16 '23 10:03 samodostal

Hey @lawrence-laz what's the behaviour you're experiencing with this?

When I use (o)pen a new terminal window is opened in regular vim (I see that your above command was only for editing?) And when I use (e)dit it opens up a floating terminal inside the current nvim session

when I use (e)dit it opens up a floating terminal inside the current nvim session, but when I use (o)pen a new terminal window is opened in vscode, did i miss sth?

setting showed below

# zshrc
if [ -n "$NVIM_LISTEN_ADDRESS" ]; then
    export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
else
    export VISUAL="nvim"
fi
export EDITOR="$VISUAL"

alias ni='nvim --listen /tmp/nvim-server.pipe'

neovim related settings are

-- init.lua
if vim.fn.executable("nvr") == 1 then
  vim.env.GIT_EDITOR = "nvr --remote-wait +'set bufhidden=delete'"
end

lazygit related settings are ( /Users/jensen/Library/Application Support/lazygit/config.xml)

os:
  editCommand: 'nvim'
  editCommandTemplate: '{{editor}} --server /tmp/nvim-server.pipe --remote-tab "$(pwd)/{{filename}}"'

any advice? i want open file in current neovim instance as a buffer @lawrence-laz @Nikola-Milovic @kdheepak

jensenojs avatar Sep 21 '23 16:09 jensenojs

I would recommend not using 'nvr', but the 'listen and send command via pipe' feature in neovim. Look at the @lawrence-laz comment. With that approach I have it working as intended ('o'pen and 'e'dit both open new tab in current neovim session)

samodostal avatar Sep 22 '23 09:09 samodostal

Also, I guess this answers the original question. You can close the floating LazyGit window and open a file in the window underneath. The Lua script you need to call in editCommand and openCommand in LazyGit config file is:

return function(filename, line_number)
	line_number = tonumber(line_number) or 1

	vim.api.nvim_win_close(0, true)
	vim.api.nvim_command('edit +' .. line_number.. " " .. filename)
end

Could you elaborate on this one @samodostal ? LazyGit accepts lua code in the config ? or where does that function should be placed ?

searleser97 avatar Oct 21 '23 19:10 searleser97

If you are wondering how to make this work with multiple neovim instances, here is my approach:

The issue arises when starting two neovim instances using the same alias:

alias nvim='nvim --listen /tmp/nvim-server.pipe'

This causes both instances to listen to the same pipe, making it impossible to use. When attempting to open a file from lazygit, both instances try to open it.

In my workflow, I use tmux which enables me to create an alias like this:

alias nvim="nvim --listen /tmp/nvim-server-$(tmux display-message -p '#S').pipe"

This creates a pipe specific to the tmux session, solving the previous issue. The next step is to communicate with this pipe. This is my lazygit configuration:

os:
  edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"
  open: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"

With this, we can communicate with the correct pipe. In the --remote-send part, you can execute any neovim command you want. I call a Lua script that closes the floating lazygit window and opens the file in the buffer underneath it.

The current behavior that I am experiencing is:

  • When pressing 'e/o': The lazygit floating window is closed, and the file is opened in the window below it.

If you want to look at details you can check my .dotfiles

@samodostal Do you know what does (tmux display-message -p '#S' does ? So that we can see if we can replicate the behavior in other terminal emulator ? Thanks

searleser97 avatar Oct 22 '23 01:10 searleser97

Could you elaborate on this one @samodostal ? LazyGit accepts lua code in the config ? or where does that function should be placed ?

In neovim you can execute lua code (functions). With the --remote-send command you can trigger any neovim command (like executing a lua function). Here it is used in the lazygit config:

os:
  edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"
  open: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"

@samodostal Do you know what does (tmux display-message -p '#S' does ? So that we can see if we can replicate the behavior in other terminal emulator ? Thanks

It prints the name of the current tmux session to stdout. I name my neovim pipes based on the tmux session name, making it possible to use this feature with more neovim instances. If you want this to work only for one neovim instance, use the @lawrence-laz solution.

samodostal avatar Oct 22 '23 15:10 samodostal

If you’d like to make this part of the README that’d be awesome!

kdheepak avatar Oct 22 '23 22:10 kdheepak

Also, I guess this answers the original question. You can close the floating LazyGit window and open a file in the window underneath. The Lua script you need to call in editCommand and openCommand in LazyGit config file is:

return function(filename, line_number)
	line_number = tonumber(line_number) or 1

	vim.api.nvim_win_close(0, true)
	vim.api.nvim_command('edit +' .. line_number.. " " .. filename)
end

Could you elaborate on this one @samodostal ? LazyGit accepts lua code in the config ? or where does that function should be placed ?

Did you figure out where to use that function?

Chaitanya-Shahare avatar Apr 30 '24 15:04 Chaitanya-Shahare

You can put that lua function in any file, the important part is how you set up the 'edit' and 'open' commands in your lazygit configuration. I'll give an example. You have you init.lua file somewhere in your file system, let's say ~/.dotfiles/neovim/init.lua. This is your neovim configuration. Now put that lua function into a file ~/.dotfiles/neovim/lua/lazygit-open-file.lua and modify the commands in your lazygit configuration:

os:
  edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('lazygit-open-file')('{{filename}}', '{{line}}')<CR>"
  open: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S').pipe --remote-send "<cmd>lua require('lazygit-open-file')('{{filename}}', '{{line}}')<CR>"

This should work, because by default when you call lua require(...) in neovim, it looks for the file in this lua/ directory next to your init.lua file. Hope this makes it clearer

samodostal avatar Apr 30 '24 17:04 samodostal

Thanks for the reply, I just got it working & it works like a charm. Thank you!

Chaitanya-Shahare avatar Apr 30 '24 17:04 Chaitanya-Shahare