0

Can you help me how this function does what it does, like step by step?

function! GitBranch()
    return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
endfunction

I previously used a plugin. Now I came across this function but can't understand it. Can anyone help me?

1 Answer 1

2

The function calls Vim's system() function. This passes its argument (a pipeline containing a git command) on to the shell and returns its output. See help system().

I'm afraid explaining the way any given shell pipeline works isn't within the scope of this site. I'd recommend reading man git-rev-parse and man tr in your shell, or perhaps you could ask on Stack Overflow.

To be honest, you don't really need to know in detail how the git command works. It returns your current branch: that's all you need to know!

4
  • I'm actually concerned about setting up a disruptive function in vimrc, that's all. Thank you for explaining it. I think it doesn't do anything more than fetching the git branch name, right?
    – 3N4N
    Apr 7, 2018 at 10:51
  • My terminal (st) cursor flickers in vim when this function is used, regardless whether it is a git directory or not. But when I use this plugin the cursor doesn't flicker.Why is that?
    – 3N4N
    Apr 7, 2018 at 13:26
  • @EnanAjmain the plugin does its work entirely inside Vim without using executing external commands, from what I can tell. Maybe you should run the system() function with :silent.
    – muru
    Apr 8, 2018 at 3:34
  • @EnanAjmain That's quite a different question from the one you asked here. You should maybe try asking a new question with more details about what you're doing: what are you trying to achieve? What do you mean by "when this function is used"? When/for how long does the flickering occur? etc.
    – Rich
    Apr 9, 2018 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.