5

I have a started vim. I suspended it (Ctrl+Z). So I was in terminal session and I would like to open some file with the same vim session (new tab). I can open the file in new vim session. Actually, path to file could be constructed. For example,

vim `find $PWD -name build.xml`

Is it possible to open this file inside the running vim session from hosting terminal session?

3 Answers 3

8

The problem with Ctrl-Z

When you suspend a process with Ctrl-Z, the process gets a SIGTSTP signal, and all execution will stop (i.e., no more CPU cycles), until a SIGCONT signal comes along. You will not be able to send vim any commands or input while it is suspended.

In other words, don't use Ctrl-Z.

Yet if you have vim compiled with the clientserver feature enabled, you can make use of the --servername and --remote-* options:

Use vim --remote

When starting your vim session for the first time, use vim --servername VIM [filename ...] (filename is optional if you want to start with a blank session).

Leave it running in your terminal. Now you can control it from any other terminal window, tab, machine, etc., via vim --remote commands. To open a file (e.g., file.txt in a new tab of your existing vim session:

vim --remote-tab file.txt

To use vim's internal :tabfind functionality (see :help find for more information):

vim --remote-send ":tabfind filename.txt<CR>"

To use your system's find(1) program instead, as you asked in your question:

vim --remote-tab `find $PWD -name build.xml`

Multiple sessions

You can also specify a different --servername, which is useful if you want multiple vim sessions. In that case, you need to supply the --servername argument every time:

vim --servername HAMBURGER # Start new session named "HAMBURGER"
vim --servername HAMBURGER --remote-tab `find $PWD -name BACON`

Of course you can roll this all into a shell script or two to save yourself some typing.

4

From within vim you can use:

:tabe `find $PWD -name build.xml`
2

Though the other answers sound cleaner to me, as a hack, you could use a function like:

for-vim () {
  printf %s "$@" > ~/.for-vim &&
    perl -le 'require "sys/ioctl.ph"; ioctl(STDIN, &TIOCSTI, $_)
     for split "", "\e:tabe `cat ~/.for-vim`\r"' &&
    fg %vim
}

To be used as:

for-vim "$(find "$PWD" ...)"

It stores the file path in a ~/.for-vim file, and injects a <ESC>:tabe ~/.for-vm<CR> in the terminal's input buffer using the TIOCSTI ioctl() before resuming vim.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .