Okay, the reason is the extra new line \n
returned by system(..)
, check :help press-enter
for more details :
So the solutions is to "suppress" the extra content ( new lines ), here are multiple way can handle:
1. via trim()
: trim string
mentioned by @D.BenKnoble :
" .vimrc
let g:python3_host_prog = expand( trim(system('command -v python3')) )
2. via systemlist()
: return list of system()
thanks @D.BenKnoble
let g:python3_host_prog = expand( systemlist('command -v python3')[0] )
" or
let g:python3_host_prog = expand( get(systemlist('command -v python3'), 0) )
" or with default value if not found from cmd
let g:python3_host_prog = expand( get(systemlist('command -v python3'), 0, '/usr/local/bin/python3') )
3. via substitute()
: find and replace
Got clue from https://stackoverflow.com/a/52983182/2940319 and tomtom/tlib_vim :
let g:python3_host_prog = expand( substitute(system('command -v python3'), '\n\+$', '', '') )
" ---------------------------- ------ --
" | | + replace to ''
" | + search patten: find one or more new line(s)
" + string
" or you can use two lines:
let python3_dynamic_path = substitute(system('command -v python3'), '\n\+$', '', '')
let g:python3_host_prog = expand(python3_dynamic_path)
Search and replace
The :substitute
command searches for a text pattern, and replaces it with a text string.
:help substitute()
substitute({string}, {pat}, {sub}, {flags})
the :checkhealth provider.python
result:
==============================================================================
provider.python: require("provider.python.health").check()
Python 3 provider (optional) ~
- Using: g:python3_host_prog = "/Users/marslo/.venv/tools/bin/python3"
- Executable: /Users/marslo/.venv/tools/bin/python3
- Python version: 3.12.3
- pynvim version: 0.5.0
- OK Latest pynvim is installed.
Python virtualenv ~
- $VIRTUAL_ENV is set to: /Users/marslo/.venv/tools
- Python version: 3.12.3
- OK $VIRTUAL_ENV provides :!python.
trim()
)