如何查询 neovim API 当前工作目录?
How to query neovim API for the current working directory?
neovim API提供了nvim_set_current_dir()
,但显然没有公开查询cwd
的方法。我该怎么做呢?
常规 Vim API 中有一个函数可用于此:
getcwd()
老问题,但我找不到答案,所以...
这里是; :lua print(vim.fn.getcwd())
ps; if you're using lsp, you may want to put it on on_attach; vim.api.nvim_set_current_dir(client.config.root_dir)
事实证明 user12542635 的回答是正确的,但它没有将答案放在 python 插件的正确上下文中,所以我将在这里这样做。我怀疑 Rafael Quintela 的回答也是正确的,但我没有设置 lua 测试环境,所以我会留给其他人评估。
在 nvim python remote plugin 的上下文中,以下代码将在 nvim 进程中执行 pwd
,收集其结果, 并且 return 它到插件进程,然后我们可以在其中指示 nvim 进程回显它:
import pynvim
from pynvim.api.nvim import Nvim
from typing import List
@pynvim.plugin
class CwdPrinter:
def __init__(self, vim: Nvim) -> None:
self.vim = vim
@pynvim.function("PrintCwd")
def print_cwd(self, args: List) -> None:
cwd = self.vim.command_output("pwd")
self.vim.command(f"echo 'cwd: {cwd}'")
PrintCwd
可以从 nvim 调用为 :call PrintCwd()
。这显然是人为的;如果需要打印当前工作目录,当然应该简单地键入 :pwd
,但它通常说明了如何在插件中调用远程 vim 命令。
neovim API提供了nvim_set_current_dir()
,但显然没有公开查询cwd
的方法。我该怎么做呢?
常规 Vim API 中有一个函数可用于此:
getcwd()
老问题,但我找不到答案,所以...
这里是; :lua print(vim.fn.getcwd())
ps; if you're using lsp, you may want to put it on on_attach;
vim.api.nvim_set_current_dir(client.config.root_dir)
事实证明 user12542635 的回答是正确的,但它没有将答案放在 python 插件的正确上下文中,所以我将在这里这样做。我怀疑 Rafael Quintela 的回答也是正确的,但我没有设置 lua 测试环境,所以我会留给其他人评估。
在 nvim python remote plugin 的上下文中,以下代码将在 nvim 进程中执行 pwd
,收集其结果, 并且 return 它到插件进程,然后我们可以在其中指示 nvim 进程回显它:
import pynvim
from pynvim.api.nvim import Nvim
from typing import List
@pynvim.plugin
class CwdPrinter:
def __init__(self, vim: Nvim) -> None:
self.vim = vim
@pynvim.function("PrintCwd")
def print_cwd(self, args: List) -> None:
cwd = self.vim.command_output("pwd")
self.vim.command(f"echo 'cwd: {cwd}'")
PrintCwd
可以从 nvim 调用为 :call PrintCwd()
。这显然是人为的;如果需要打印当前工作目录,当然应该简单地键入 :pwd
,但它通常说明了如何在插件中调用远程 vim 命令。