1

I often find myself operating in the vim terminal needing to edit a few files. In this case I might type vim *.h. This opens a new vim instance nested inside my vim terminal. Is there a way to make a command that instead triggers the parent vim instance to load the files in buffers?

  • I suppose you mean :args *.h – Matt Oct 9 '19 at 13:43
  • 1
    If I understand your question properly, it is a duplicate of this one: Start vim with vim --servername SERVER and from within the terminal buffer use vim --servername SERVER --remote *.h. However I think your workflow could be improved: you don't need to open a terminal to open new files, instead you could use builtin commands like :h :args or :h :argadd or even a fuzzy finder like fzf – statox Oct 9 '19 at 21:03
  • 1
    If you're using neovim, github.com/mhinz/neovim-remote does what you want. Other options are covered in question linked by @statox. – Thunderbeef Oct 9 '19 at 22:17
  • @statox - I think the example I chose is too simplified. I often want to use complex pipes of greps to make a file list. For example vim $( ... something ... ). Which I can do easily, but I would like it not create a session nested inside the terminal, and instead pop up a series of buffers. – Joel Holdsworth Oct 10 '19 at 9:46
3

You can do that using the :terminal mode in vim 8. My answer is a modification of this answer.

  1. Create a special function in your vimrc that's callable from terminal, its name must start with Tapi_.
function! Tapi_vit(bufnum, arglist)
   let currfile = get(a:arglist, 0, '')
   if empty(currfile)
     return
   endif
   execute 'e' currfile
endfunction
  1. Create a function and then an alias in your .zshrc or .bashrc. If you are in vim's terminal, then the file(s) would be opened in current vim. Works with wildcards(*) too.
 vit()
 {
   if [[ ! -z "$VIM_TERMINAL" ]]; then
     if [ $# -eq 0 ]; then
       echo "You are already inside Vim. Provide filenames as arguments"
     else
       readlink -f $@ | xargs printf '\033]51;["call", "Tapi_vit", ["%s"]]\007'
     fi
   else
     vim $@
   fi
 }
alias vim=vit

The leading \033]51; and the trailing \007 are special escape sequence recognized by vim. check :h terminal-api for more detail.

Series of actions.

  • vim
  • :terminal
  • Go to desired directory in :terminal buffer
  • vim *.h in the :terminal buffer. All *.h will open in current vim instance.
  • Just vim without arguments in the :terminal buffer will give the message "You are already inside Vim. Provide filenames as arguments"
  • If you are in a normal terminal, its as usual vim (a new vim instance is started)

I use Cygwin on Windows. So, this works there as well. I probably have to write a different version if you need it working with 'cmd' on windows. I will update this answer if I manage to do that.

Hope it helps.

| improve this answer | |
3

I found it more intuitive (YMMV!) to create a bash script called :e, containing this:

#!/bin/bash

for f; do
    echo -e "\033]51;[\"drop\", \"$f\"]\007"
done

So :e file works both at the vim command line, as well as in a terminal-within-vim

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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