voldikss / vim-floaterm

:computer: Terminal manager for (neo)vim
MIT License
2.5k stars 79 forks source link

File browsers won't open in floaterm if the file in the currently focused buffer is owned by root #293

Open laszloszurok opened 3 years ago

laszloszurok commented 3 years ago

floaterm health check


health#floaterm#check
========================================================================
## common
  - INFO: Platform: linux
  - INFO: Nvim: NVIM v0.4.4
  - INFO: Plugin: 1c63ac0

## terminal
  - OK: Terminal emulator is available

## floating
  - OK: Floating window is available

My Configurations related to vim-floaterm

let g:floaterm_gitcommit='floaterm'
let g:floaterm_autoinsert=1
let g:floaterm_width=0.8
let g:floaterm_height=0.8
let g:floaterm_wintitle=0
let g:floaterm_autoclose=1
let g:floaterm_opener='edit'

Description of the bug:

File browsers like vifm, nnn, lf, etc. won't open in floaterm if the file in the currently focused buffer is owned by root. If I run FloatermNew vifm, i get the following error messages:

Error detected while processing function floaterm#run[7]..floaterm#new[19]..floaterm#wrapper#vifm#: line 3: E472: Command failed Error detected while processing function floaterm#run: line 7: E171: Missing :endif

Steps to reproduce

  1. Open for example /etc/resolve.conf or any other file owned by root.
  2. Run FloatermNew vifm or FloatermNew nnn, etc.

A solution

I skimmed through the code and found the line which causes the problem. In the autoload/floaterm/wrapper/vifm.vim file this line

lcd %:p:h

fails if the owner is root. This is the case with the other file manager wrappers as well.

Fortunately I also found a solution. Instead of lcd %:p:h we can use this:

let path_to_open = expand('%:p:h')
exec "lcd " . path_to_open

The functionality is the same, but now file managers will correctly work with any file.

I use a plugin called suda.vim which makes it easier to edit files owned by root. This plugin renames readonly buffers if let g:suda_smart_edit = 1 is set and this will make my solution above not to work. To provide compatibility with the suda plugin I extended my solution above like this:

let path_to_open = expand('%:p:h')

" This check provides compatibility with the suda.vim plugin
if !isdirectory(path_to_open) && !filereadable(path_to_open)
  let path_to_open = path_to_open[6:-1]
endif

exec "lcd " . path_to_open

Just thought I leave this here if anyone else uses suda.vim

adigitoleo commented 2 years ago

If you are still using vifm, maybe you would like to implement this and send a PR. I would fix this but I don't use the file manager extensions so it will take longer for me to test it. It looks like this will need to be fixed for all of the file manager wrappers, right?