Execute an open default editor command inside vim - linux

I am trying to make a habit of never leaving my gvim, not even for executing commands in the terminal. This works fine with :!<command>. However if this command wants to open default editor (vim) it ironically does not work from gvim (it works from command line vim). Example:
:!git commit
error: Terminal is dumb, but EDITOR unset
Please supply the message using either -m or -F option.
I can run:
:!git commit -m "message"
But I like the vim window when I am inspecting files and write commit messages. Is it possible to make the commit window appear in gvim as with command line vim?

As an alternative to shelling out using :! command, you may want to try Tim Pope's Fugitive plugin. Quote from documentation:
I'm not going to lie to you; fugitive.vim may very well be the best
Git wrapper of all time.
Once installed you can simply run the :Gcommit command and the commit buffer will be opened up.
Over at Drew Neil's Vimcasts he a series on using Fugitive that is worth a look:
The Fugitive Series - a retrospective - overview of series
Fugitive.vim - a complement to command line git - specific screencast using :Gcommit

Git commit checks for the environment value $EDITOR in order to decide which editor it should to open to type the commit messages unless the messages has been specified using the -m optin on the commandline.
Use:
:!EDITOR=vim git commit
This will set the $EDITOR environment var to vim.
Further explanation:
(from man git-commit)
The editor used to edit the commit log message will be chosen from the
GIT_EDITOR environment variable, the core.editor configuration
variable, the VISUAL environment variable, or the EDITOR environment
variable (in that order). See git-var(1) for details.
This means beside from setting environment values you can use git config to specify the editor. (I would do so)
git config core.editor vim
If you once done so than git commit will not check the $EDITOR env var anymore.

Related

How can I prepopulate a terminal prompt with a command or partial command?

I would like to write a command/function that will prepopulate my terminal prompt with a partial command or text. I need to prefix my git commit messages and I was thinking it would be useful for this (and other uses) if I could prefill the prompt with a partial command rather than always needing to retype the same thing.
> gencommit
> git commit -m "ABC-123:
In the code snippet above, git commit -m "ABC-123: is placed on the command line and I would simply be able to add a commit message and execute the command.
echo is the only thing that comes to mind but that does not do what I want, If gencommit used echo it would look something like this.
> gencommit
git commit -m "ABC-123:
>
As far as I know, bash doesn't provide any way to write to the Readline buffer programatically. (zsh does, as an aside.) One alternative is to make gencommit a Readline macro. For example, add this to your .inputrc file (creating the file if necessary):
"gencommit": "git commit -m \"ABC-123:"
One thing about his approach that I find disconcerting is that because a macro is text that gets replaced immediately, Readline won't actually echo the text you are typing to the screen until it has determined what macro you are typing, so while you are typing gencommit, nothing appears on your screen until you finish, then the expansion text appears. Worse, if you make a mistake while typing, you have to cancel the command and start over.
Better is to define an alias
alias gencommit='git commit -m "ABC-123:'
and, after typing gencommit on the command line, use the shell-expand-line Readline function (bound to M-! by default; M is the meta key, which is either Alt or Esc, depending on your platform and terminal emulator settings) to expand it to the replacement text. (This tries to expand anything on the command line: aliases, parameters, history expansions, etc. There is a dedicated alias-expand-line function that only expands aliases, but it is unbound by default. I leave it as an exercise to bind this command to an unused key sequence if you like.)
As I alluded to zsh, the command in that shell would simply be
print -z 'git commit -m "ABC-123:'

How to save exit nvim in git bash commit

I'm using git bash on Win 10 and nvim as the editor. When I commit with no message it opens a blank screen (where blank means BLANK, not mostly blank with a line number at the bottom), I type "stuff", I Esc:wq but "wq" is inserted. I <C-c> and get the obligatory you're-an-idiot-and-didn't-give-a-commit-message error. I start nvim directly and get the same blank screen and the same effect of only the first character of any command after <Esc> is not inserted then we go right back to insert mode so :q acts like iq ... totally at a loss. Is git bash incompatible with nvim?
Edit: In case a visual helps
This neovim/neovim issue 9585 illustrates that nvim can be used with Git.
Check with a git config core.editor how nvim was declared as Git editor.
It can be:
git config --global core.editor '"nvim -u NORC"'
On Windows, this issue suggests:
git config --global core.editor "set LC_CTYPE='en_US.UTF-8';nvim"
Using mhinz/neovim-remote can help:
git config --global core.editor 'nvr --remote-wait-silent'

Pipe in ZSH is stripping colors from Git log output

I'm trying to utilize GRB's git helpers (https://github.com/garybernhardt/dotfiles/blob/master/.githelpers) for pretty printing my git log.
The git log --graph --pretty="tformat:${LOG_FORMAT}" command from line #62 works just fine; printing with colors in iTerm. But as soon as it's piped to pretty_git_format there are no colors displayed.
This has been working for several years until just a few weeks ago. Was there a change in zsh that I now need to configure differently?
I'm using iTerm2 and zsh version zsh 5.4.2 (x86_64-apple-darwin16.7.0).
The default behavior for git is to produce color if the output is being printed to a terminal (directly or through a git-spawned pager). When your output goes somewhere else, like a pipe, git turns off colors.
You can set the color.ui option to always on the command line like this: git -c color.ui=always log --graph --pretty="tformat:${LOG_FORMAT}" (and yes, that's where the -c option goes). If you want to do this frequently, it can be done with a shell alias in .gitconfig.
While you can set this in your .gitconfig as well, you probably don't want to. Most external programs assume color is turned off, and you can break other parts of git, as well as tools like editor integrations, if you set this in .gitconfig.
This started happening to me today, after updating to OSX High Sierra and updating my homebrew packages.
I suspect it is something to do with the latest Git version (2.15.0), as those release notes mention changing how the git log colors work:
Fixes since v2.14
-----------------
* "%C(color name)" in the pretty print format always produced ANSI
color escape codes, which was an early design mistake. They now
honor the configuration (e.g. "color.ui = never") and also tty-ness
of the output medium.
So I think Brian's solution is probably the best one, but I do note that it does seem to affect the column formatting of pretty_git_format, which I mentioned in a comment above.

p4 change not working with gvim

I use perforce as source code repository. p4 change command is used to create a changelist of opened files. If I set setenv EDITOR gvim and then run this command then gvim opens and I add some description and then save and quit. I get below error. Same error does not come if EDITOR is not set, I mean in that case vim opens. Any idea to fix this issue?
sachina#inn-sachina-vm[285] p4 change
Error in change specification.
Error detected at line 29.
Change description missing. You must enter one.
Hit return to continue...
Applications that invoke EDITOR assume that the command blocks until editing is done and the editor was closed. While true for vim, the GUI version gvim launches in the background; i.e. the command returns immediately.
You can avoid this via the :help -f command-line option:
setenv EDITOR 'gvim -f'

Calling command from `-c` command line argument doesn't work in Vim

I'm using the Fugitive plugin.
It has a command(?) Git! which executes a command and opens the result in a new buffer.
Example:
:Git! diff --cached
I have a function which calls this, and does some other things after that.
And I have this command declaration:
command! Hello execute ":Git! diff"
If I run :Hello from within vim, it works as it should. But when I run vim -c Hello, it throws this error:
Not an editor command :Git! diff
How can I do this?
(PS: How can I make this error message to stay until I press ? It appears for about a second and disappears.)
The reason is that Fugitive only defines its commands for buffers whose files are under Git version control. Precisely, the code in plugin/fugitive.vim only sets up autocmds that detect files under Git control, and only then defines buffer-local commands.
So at least you need to pass a Git-controlled file to your Vim invocation. If that still doesn't work, try explicitly triggering the detection via
:doautocmd User Fugitive

Resources