7

I am trying to configure Neovim for Windows. One of the key things of this configuration is getting access to the terminal-emulator (it's very comfortable to switch and use the terminal with Vim). But I can't manage to get it to work. I use Neovim binaries/Neovim-qt. I can run shell commands using ! without issue.

I also tried to experiment with shell and shellquote vars, but it doesn't work as well.

I tried to use Bash (MSYS2) instead of cmd.exe, but still no luck. (! still works)

The problem is probably the shell vars or some permissions.

I am very thankful for any help!

herrbischoff
  • 2,673
  • 24
  • 46
Dmytro
  • 71
  • 1
  • 3

2 Answers2

1

I think using let $PATH .= ';C:\cygwin64\bin' in init.vim might be enough to deal with shell issues.

I tried the vim style as suggested in
http://vim.wikia.com/wiki/Use_cygwin_shell . And this breaks vim-fugitive and vim-plug with error prompt like /usr/bin/bash command not found (by git push, git log) or cannot create leading directory (by vim-plug when cloning repos). After I disabled them, fugitive and plug works again.

And this $TERM option might be of interest: https://neovim.io/doc/user/term.html#$TERM

Hustlion
  • 1,535
  • 13
  • 19
0

It took me ages but I have finally got Git Bash both opening as a buffer, and the ! command. I've only tested this on windows 7, but this probably works just as well for you.

Install Git for windows, which also provides a Git Bash environment, which I will assume is called C:\Program Files (x86)\Git\bin\bash.exe. If you have a 32 bit system or want to use 64 bit Git for Windows, you will need to change ~2 to ~1 in the scripts below.

You should now be able to start git bash from a regular windows CMD prompt with this command:

cmd.exe /c "C:\Progra~2\Git\bin\bash.exe --login -i"

We're going to use that in the following mappings.

Add the code below to your vimrc file. Assuming you've installed NeoVim using the windows installer, your init.vim script should be located at %APPDATA%/../Local/nvim. See this answer for how to source a vimrc file from elsewhere for configuring NeoVim. In fact, I use a hierarchy of .vim scripts in a git repository, of which this is terminal.vim:

" Terminal settings
" cc for commandline, cs for split first, ce to exit
:tnoremap <ESC> <C-\><C-n>
if has("win32")
  " Note, you need to empty the file Git\etc\motd
  " to get rid of the 'Welcome to Git' message
  set shell=cmd.exe
  nnoremap <Leader>cc :term<CR>adoskey.cmd<CR>cmd.exe /c "C:\\Progra~1\Git\bin\bash.exe --login -i"<CR>clear<CR>
  nnoremap <Leader>cs :sp<CR>:wincmd j<CR>:term<CR>adoskey.cmd<CR>cls<CR>cmd.exe /c "C:\\Progra~1\Git\bin\bash.exe --login -i"<CR>clear<CR>
  nnoremap <Leader>cd :term<CR>adoskey.cmd<CR>cls<CR>
  nnoremap <Leader>csd :sp<CR>:wincmd j<CR>:term<CR>adoskey.cmd<CR>cls<CR>
else
  nnoremap <Leader>cc :term<CR>A
  nnoremap <Leader>cs :sp<CR>:wincmd j<CR>:term<CR>A
endif

augroup TerminalMappings
  autocmd!
  if has("win32")
    autocmd TermOpen * nnoremap <buffer> <C-E> aexit<CR>exit<CR>
    autocmd TermOpen * tnoremap <buffer> <C-E> exit<CR>exit<CR>
  else
    autocmd TermOpen * nnoremap <buffer> <C-E> aexit<CR>
    autocmd TermOpen * tnoremap <buffer> <C-E> exit<CR>
  endif
augroup END

" Make the split bigger
nnoremap <Leader>b 8<C-W>+<CR>
nnoremap <Leader>B 50<C-W>+<CR>
" Make the split wider
nnoremap <Leader>w 8<C-W>><CR>
nnoremap <Leader>W 56<C-W>><CR>

The first command remaps Control-[ and ESC to exit terminal mode, which seems very natural but isn't true by default - normal mode allows you to, for example, scroll back using cntrl-B, jump with usual commands (or with easy-motion) to a line containing a shell command and yypa<Enter> to execute it.

Setting shell=cmd.exe isn't actually necessary, since it is the default when vim is installed for windows, but stating it explicitly helps if you want to change this later on.

I use these mnemonics in the bindings; 'c' is for commandline, 's' means split, 'd' means DOS (windows CMD), 'e' means exit. That gives these options:

<leader>cc  full screen bash terminal
<leader>cs - split screen bash terminal (used 99% of the time by me)
<leader>cd - full screen DOS terminal
<leader>csd - split screen DOS terminal
<leader>e - exit terminal

I also load doskey.cmd which is somewhere accesible through PATH and in my case contains:

@echo off

DOSKEY ls=dir
DOSKEY which=where
DOSKEY mv=move
DOSKEY c=cls
DOSKEY clear=cls
DOSKEY bash=cmd.exe /c "C:\\Progra~1\Git\bin\bash.exe --login -i"
DOSKEY cp=copy

DOSKEY ..=cd ..
DOSKEY ...=cd ..\..
DOSKEY ....=cd ..\..\..
DOSKEY .....=cd ..\..\..\..
DOSKEY ......=cd ..\..\..\..\..
DOSKEY .......=cd ..\..\..\..\..\..
DOSKEY ........=cd ..\..\..\..\..\..\..
DOSKEY .........=cd ..\..\..\..\..\..\..\..

which is analoguous to the ~/.bashrc file in its aliases, and also allows me to do <leader>cd and enter bash to switch to bash.

TamaMcGlinn
  • 1,937
  • 13
  • 26