lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

neovim integration

Open timsofteng opened this issue 3 years ago • 22 comments

Hey. Do you have plans to develop plugin for vim? I'm using lazygit in floaterm now but I guess native solution from developer would be better option with full ingegration and less buggy.

timsofteng avatar Sep 11 '20 15:09 timsofteng

good question, I believe there is a plugin for neovim, not sure about vim itself

jesseduffield avatar Sep 17 '20 21:09 jesseduffield

For me, using lazygit with floaterm has been an awesome experience and I haven't looked back since.

But to be honest, I'm not sure if there's much to be added by having an full integration.

fsmiamoto avatar Sep 27 '20 14:09 fsmiamoto

I agree with @fsmiamoto

infinite-ops avatar Sep 29 '20 14:09 infinite-ops

For me, using lazygit with floaterm has been an awesome experience and I haven't looked back since.

But to be honest, I'm not sure if there's much to be added by having an full integration.

What do you do if you want to start edit file in lazygit? I have tried do it by pressing e key but few times I caught bugs.

timsofteng avatar Sep 30 '20 11:09 timsofteng

@timsofteng which bugs have you encountered?

jesseduffield avatar Oct 06 '20 10:10 jesseduffield

@timsofteng which bugs have you encountered?

For example, if I press q in floaterm with lazygit it sometimes still in empty floaterm window and I have to close this window too. When I press e to edit file it opens in floaterm. I think open file in vim ordinary buffer is better idea. And once I cought bug when I resolved conflicts in lazygit with floaterm.

timsofteng avatar Oct 06 '20 15:10 timsofteng

This is what I use to run lazygit from 'within' vim:

nnoremap <C-g> :!lazygit<cr><cr>

It works incredibly well. (The extra <cr> there fixes an issue where the screen may not be redrawn after returning from lazygit)

craftyguy avatar Oct 10 '20 05:10 craftyguy

This is what I use to run lazygit from 'within' vim:

nnoremap <C-g> :!lazygit<cr><cr>

It works incredibly well. (The extra <cr> there fixes an issue where the screen may not be redrawn after returning from lazygit)

@craftyguy Which version of (neo)vim and lazygit are you using? I tried :!lazygit <cr> in Neovim 0.4.4, and with lazygit 0.23.2 (from conda-forge) and I got

:!lazygit                                                                                                                                                                                                                                                                                                 
2020/10/12 15:54:01 An error occurred! Please create an issue at https://github.com/jesseduffield/lazygit/issues                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                          
*os.PathError open /dev/tty: no such device or address                                                                                                                                                                                                                                                    
/home/conda/feedstock_root/build_artifacts/lazygit_1602459989867/gopath/src/github.com/jesseduffield/lazygit/main.go:133 (0xa02ab3)                                                                                                                                                                       
/home/conda/feedstock_root/build_artifacts/lazygit_1602459989867/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla/go/src/runtime/proc.go:203 (0x42f6a6)     
/home/conda/feedstock_root/build_artifacts/lazygit_1602459989867/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla/go/src/runtime/asm_amd64.s:1357 (0x45b001)
                                                                                                                                                                                                                                                                                                          
shell returned 1                                                                                                                                                                                                                                                                                          

gpcureton avatar Oct 12 '20 20:10 gpcureton

@gpcureton

I'm using vim:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 17 2020 21:54:53)

with lazygit 0.22.8:

commit=307d051ec2ec03a2fcd347ce94f205aa2dca75fc, build date=2020-09-27T18:14:36Z, build source=binaryRelease, version=0.22.8, os=linux, arch=amd64

This is on Arch Linux.

craftyguy avatar Oct 13 '20 04:10 craftyguy

I built/installed the latest lazygit from git master, and it still works for me.

craftyguy avatar Oct 13 '20 04:10 craftyguy

@craftyguy Thanks for the info. I also successfully ran the same version of lazygit in vim 8.* on Arch. And also vim 7.* on RHEL 7. I suspect the issue is with Neovim. Running in :terminal in Neovim works, but that's cheating ;-)

gpcureton avatar Oct 13 '20 04:10 gpcureton

If you're running lazygit inside of floaterm in neovim, and you want to open a file in the host neovim instance from lazygit by pressing e, you can install the neovim-remote package and add this to your lazygit config.yaml file:

os:
  editCommand: 'floaterm'

apazzolini avatar Sep 23 '21 05:09 apazzolini

@gpcureton I've since moved to neovim, and could only get it working with :terminal. Per the neovim faq, it doesn't support interaction with commands when using !: https://github.com/neovim/neovim/wiki/FAQ#-and-system-do-weird-things-with-interactive-processes

So I have this: nnoremap <C-g> :terminal lazygit<cr>

It works reasonably well. But not as well as it did with vim.

craftyguy avatar Sep 23 '21 17:09 craftyguy

For a smooth integration neovim - lazygit (commitChangesWithEditor, edit, etc):

  • first install neovim-remote (pip install neovim-remote, or nvr from OS package manager)
  • put these in bashrc: export EDITOR=nvim export VISUAL="nvr --remote-wait +'set bufhidden=wipe'"
  • and in lazygit config.yml:
    os:
      editCommand: "nvim"
    

basilgood avatar Sep 26 '21 12:09 basilgood

The bugs encountered are most likely due to floaterm, and not lazygit itself.

I haven't had many issues using it for a better part of a year just by invoking it in a new terminal:

:tab term ++close lazygit<CR>

Note: this is vim.

mark2185 avatar Aug 07 '22 20:08 mark2185

You can use the :LazyGitConfig option from within neovim to make lazygit work within the terminal of neovim with the kdheepak/lazygit.nvim plugin. I did not have to do anything execpt add this to the lazygit/config.yml file within windows powershell:

os:
    editCommand: 'nvim'

See kdheepak/lazygit.nvim #91

rohit-kumar-j avatar Apr 09 '23 21:04 rohit-kumar-j

This seems to work well and doesn't require additional dependencies:

os:
  edit: 'nvim --server "$NVIM" --remote-tab {{filename}}'
  editAtLine: 'nvim --server "$NVIM" --remote-tab {{filename}}; [ -z "$NVIM" ] || nvim --server "$NVIM" --remote-send ":{{line}}<CR>"'
  editAtLineAndWait: 'nvim +{{line}} {{filename}}'
promptToReturnFromSubprocess: false

editAtLineAndWait can't use --remote (https://github.com/neovim/neovim/pull/17856).

frangio avatar Jul 06 '23 21:07 frangio

This seems to work well and doesn't require additional dependencies:

os:
  edit: 'nvim --server "$NVIM" --remote-tab {{filename}}'
  editAtLine: 'nvim --server "$NVIM" --remote-tab {{filename}}; [ -z "$NVIM" ] || nvim --server "$NVIM" --remote-send ":{{line}}<CR>"'
  editAtLineAndWait: 'nvim +{{line}} {{filename}}'
promptToReturnFromSubprocess: false

editAtLineAndWait can't use --remote (neovim/neovim#17856).

When you have a tab page open, pressing "e" in lazygit does not seem to exit floaterm.

chaozwn avatar Jul 16 '23 05:07 chaozwn

@frangio thanks for the config example, works fine for me. however, every time i press e it open the file in a new tab. not the end of the world but i would rather have it open in the same(existing) tab. Any ideas on how to make it? I would guess the nvim option --remote-tab is the one opening the file in a new tab, while --remote would open in the existing. however in my case if i only use --remote it opens the fine in a new floated window

leikoilja avatar Aug 08 '23 11:08 leikoilja

@leikoilja Here's a workaround, just toggle/hide your float terminal first.

os:
  edit: '[[ -z "$NVIM" ]] && { nvim {{filename}} &>/dev/tty; true; } || { nvim -u NONE --server $NVIM --remote-expr "execute(\"ToggleTerm\")" && nvim -u NONE --server $NVIM --remote {{filename}}; }'
  editAtLine: '[[ -z "$NVIM" ]] && { nvim +{{line}} {{filename}} &>/dev/tty; true; } || { nvim -u NONE --server $NVIM --remote-expr "execute(\"ToggleTerm\")" && nvim -u NONE --server $NVIM --remote {{filename}} && nvim -u NONE --server $NVIM --remote-expr "execute(\"normal {{line}}gg\")"; }'

ToggleTerm can be replaced by FloatermToggle or whatever vim command you use to toggle your float terminal windows.

phanen avatar Apr 08 '24 17:04 phanen

@phanen , unfortunately it doesn't work as expected with FloatermToggle. It just opens a new floating window on top of my current editor session with a new editor instance

leikoilja avatar Apr 11 '24 08:04 leikoilja

@leikoilja I try vim-floatterm just now (basically default config), and it works well for me. So I have no idea now, maybe you can try the following solution:

  • Use the default config in lazygit
os: 
  editPreset: nvim-remote

Although it may launch a new tab, it can also work on my machine (https://github.com/jesseduffield/lazygit//blob/145fb6191c108f7e7af8180106b82f0503a7994c/pkg/config/editor_presets.go#L55).

  • Or try https://github.com/willothy/flatten.nvim, which seems a generic solution for neovim embeded terminal.

phanen avatar Apr 12 '24 19:04 phanen