-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(O)pen file and (e)dit file in the current nvim instance? #67
Comments
If I'm understanding correctly, if you install |
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 |
I think it should work out of the box but setting 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 |
@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? |
I have the following lines in my dotfiles: You can add @okuuva or my snippet into your .vimrc or init.lua and it should work. |
I also have the following in my 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. |
Behaviour I'm experiencing:
I would like to set it up so that all of the above would be opened in the current neovim instance. I should have |
Same behaviour here. Set up as readme suggested. With latest version of |
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' |
That’s a neat trick! Would you like to submit a PR for that into the README? |
Sure, see if this makes sense: #85 |
Hey @lawrence-laz what's the behaviour you're experiencing with this? When I use |
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: |
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 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 The current behavior that I am experiencing is:
If you want to look at details you can check my .dotfiles |
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 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 |
trying to fix lazygit : kdheepak/lazygit.nvim#67
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'
-- init.lua
if vim.fn.executable("nvr") == 1 then
vim.env.GIT_EDITOR = "nvr --remote-wait +'set bufhidden=delete'"
end
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 |
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) |
Could you elaborate on this one @samodostal ? LazyGit accepts lua code in the config ? or where does that function should be placed ? |
@samodostal Do you know what does |
In neovim you can execute lua code (functions). With the
It prints the name of the current tmux session to stdout. I name my neovim |
If you’d like to make this part of the README that’d be awesome! |
Did you figure out where to use that function? |
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. 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 |
Thanks for the reply, I just got it working & it works like a charm. Thank you! |
@samodostal I really like your solution! I did encounter a small issue with it though, as I tend to have multiple windows open, with multiple panes, often each window having at least one nvim instance. I modified your nvim alias to: alias nvim="nvim --listen /tmp/nvim-server-$(tmux display-message -p '#S-#W-#P').pipe" And the os:
edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S-#W-#P').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-#W-#P').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>" And this works much more seamlessly for my workflow. The primary change is modifying the tmux message to use session, window, and pane to generate the temporary server. |
Thanks @joshfullmer – just what I needed. As my window name is changing dynamically, I decided to use the session, window and pane IDs. nvim alias: alias nvim="nvim --listen /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe" Lazygit config: os:
edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>"
open: nvim --server /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}', '{{line}}')<CR>" |
Thanks everyone for the solutions, it really helped me make this work! Just something I want to add - # Command for editing a file. Should contain "{{filename}}".
edit: ""
# Command for editing a file at a given line number. Should contain
# "{{filename}}", and may optionally contain "{{line}}".
editAtLine: ""
# Same as EditAtLine, except that the command needs to wait until the
# window is closed.
editAtLineAndWait: "" If you want editing at the specified line to work (pressing os:
edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S-#W-#P').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}')<CR>"
editAtLine: nvim --server /tmp/nvim-server-$(tmux display-message -p '#S-#W-#P').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-#W-#P').pipe --remote-send "<cmd>lua require('core.scripts.lazygit-open-file')('{{filename}}')<CR>" Also I've found that if you're already using -- Open lazygit in new tmux window
vim.keymap.set(
'n',
'<leader>gg',
'<cmd>silent !tmux set -w popup-border-lines rounded; tmux popup -E -eTERM=screen-256color -xC -yC -w90\\% -h90\\% -sbg=\\#1f2335 -Sbg=\\#1f2335 -d "'
.. vim.fn.getcwd()
.. '" lazygit -ucf $XDG_CONFIG_HOME/lazygit/config_nvim.yml<cr>',
{ desc = 'Lazygit' }
) Inside the config I added (you can make it as a neovim function, and require it, just a personal preference): os:
edit: nvim --server /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe --remote-send "<cmd>e {{filename}}<cr>";tmux popup -C;
editAtLine: nvim --server /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe --remote-send "<cmd>e +{{line}} {{filename}}<cr>";tmux popup -C;
open: nvim --server /tmp/nvim-server-$(tmux display-message -p '\#{session_id}-#{window_id}-#{pane_id}').pipe --remote-send "<cmd>e {{filename}}<cr>";tmux popup -C;
Here's a video of it in action: Screencast.from.2024-07-03.22-51-46.mp4Cheers! |
Thanks for sharing! I’d be open to adding your tmux command to the repo. I’m thinking maybe people will find it useful to do the same with zellij too. |
sorry, I got a little lost in the context. I came back to this question to see what was wrong with my configuration, because I didn't have editAtLine configured, but it seemed to work, and then I tried further, and then I was completely lost, especially after I tried to upgrade lazygit(version=0.42.0), The editAtLine function was gone too i have tired @karamanliev config.yml but i only have then i retired with remove
But I'm not sure if this is another bug, the page returned after The above is my attempt, and thanks everyone for the solutions, in particularly @kdheepak , but since the context is probably too long, maybe we need a centralized place to reorganize things a bit. ps
pps
|
lazygit now has a native integration with nvim remote: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#configuring-file-editing In my case I didn't even have to open nvim with the --listen flag, it just worked out of the box. |
same, and in case anyone want to skip "press enter to return to lazygit" like this plz refer to |
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
The text was updated successfully, but these errors were encountered: