Vim:在没有逃犯的情况下在细线状态行中显示当前的 git 分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24031715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:05:18  来源:igfitidea点击:

Vim: Show current git branch in lightline status line without fugitive

gitvimvim-fugitive

提问by Robert Audi

I am using lightline.vimand I would like to show the current git branch. At the moment, I am doing so using vim-fugitive, however, I do not use this plugin at all, except for showing the git branch in the status line.

我正在使用lightline.vim并且我想显示当前的 git 分支。目前,我使用vim-fugitive这样做,但是,我根本不使用这个插件,除了在状态行中显示 git 分支。

My question is:

我的问题是:

Is there a way to show the git branch in the status line without using fugitive?

有没有办法在不使用逃犯的情况下在状态行中显示 git 分支?

I tried to create a custom function that I hooked to lightline. The function fetched the current git branch using the systemvimscript function. It (didn't) work well, except that the status line was refreshed on every keystroke, which broke everything else and made vim unusable. Essentially, the git branch was being fetched using the systemfunction on every keystroke...

我试图创建一个我连接到 lightline 的自定义函数。该函数使用systemvimscript 函数获取当前的 git 分支。它(没有)运行良好,除了每次击键时都会刷新状态行,这会破坏其他所有内容并使 vim 无法使用。本质上,system每次击键时都会使用该函数获取 git 分支......

So that's not a viable solution. What I "could" do, is create a global variable which says whether or not the git branch was fetched, and prevent the custom function from fetching the branch name again if it was already fetched. Then I could fetch the branch name again every time MacVim loses focus (not sure how to do that using terminal vim). That is not ideal though...

所以这不是一个可行的解决方案。我“可以”做的是创建一个全局变量,说明是否已获取 git 分支,并防止自定义函数再次获取分支名称(如果它已被获取)。然后我可以在每次 MacVim 失去焦点时再次获取分支名称(不确定如何使用终端 vim 做到这一点)。不过这并不理想...

Any ideas on how to solve this problem please?

关于如何解决这个问题的任何想法?

回答by itchyny

I have created a plugin which focuses on the function you want.

我创建了一个插件,专注于您想要的功能。

https://github.com/itchyny/vim-gitbranch

https://github.com/itchyny/vim-gitbranch

You can get the branch name by gitbranch#name()function.

您可以通过gitbranch#name()函数获取分支名称。

The code is so small that you can read in a minute.

代码很小,你可以在一分钟内阅读。

回答by romainl

Yes, using a global variable is the way to go.

是的,使用全局变量是可行的方法。

The non-obvious part is how/when to update the value of that variable. An autocmdseems to be the most obvious choice but hooked to what event? BufEnterseems to be a good start.

不明显的部分是如何/何时更新该变量的值。Anautocmd似乎是最明显的选择,但与什么事件挂钩?BufEnter似乎是一个好的开始。

Now, since you don't use fugitive, how do you switch branches? Outside of Vim? On Vim's command-line?

现在,既然你不使用逃犯,你如何切换分支?在 Vim 之外?在 Vim 的命令行上?

If you do it on Vim's command-line, :!git checkout foo, you could write a command that takes a branch name as argument to execute the proper shell command andchange the value of g:git_branchin one go, without even using an autocmd.

如果您在 Vim 的命令行中执行此操作,则:!git checkout foo可以编写一个命令,该命令将分支名称作为参数来执行正确的 shell 命令g:git_branch一次性更改 的值,甚至无需使用autocmd.

If you do it outside of Vim (<C-z>, :sh, another terminal window or tmux pane, a GUI app…) you will need to find a solid way to notify Vim of that change oruse your system()call with an autocmd.

如果您在 Vim 之外(<C-z>:sh另一个终端窗口或 tmux 窗格、GUI 应用程序……)执行此操作,您将需要找到一种可靠的方法来通知 Vim 更改使用system()带有autocmd.