Jeromy Anglim
Jeromy Anglim

Reputation: 34947

How to open another file in background Vim from Bash command-line?

I am transitioning from using Gvim to console Vim.

I open a file in Vim, and then suspend Vim, run a few commands on the command-line and then want to return to Vim.

However, when Vim is in the background and I issue vim file, the file opens in a new instance of Vim.

I'm used to using the --remote option with Gvim to open a file in an existing Gvim instance.

Question:

Update:

I just read this answer by @jamessan which provides a few ideas. He shows the following code snippet:

vim --servername foo somefile.txt
:shell
<do stuff in your shell>
vim --servername foo --remote otherfile.txt
fg

However, I'd have to think about how to make it easier to use perhaps with some aliases.

Upvotes: 14

Views: 9315

Answers (4)

solotim
solotim

Reputation: 1866

If you use tmux, and if you always have your vim instance running as the first job in background, you can setup alias like below in csh.

alias v 'tmux send-keys fg Space +1 Enter :e Space `realpath \!:1` Enter'

then you can call it like this

v myfile.txt

If your vim instance is not the first background job, enrich the alias with jobs output.

In Bash, this can be done with a function.

function v() {
    local job=$(jobs | perl -ne 'print $1 if /\[(\d+)\].*vim/')
    if [[ -n $job ]]; then
        tmux send-keys fg Space $job Enter
        for f in $*; do
            tmux send-keys :e Space `realpath $f` Enter
        done
    else
        vim $*
    fi
}

Upvotes: 2

brook hong
brook hong

Reputation: 601

This is also what I need. I found this thread, though no satisfying approach, happy to see people having same requirement like me.

My approach is

add below to .bashrc

v() {
    vim_id=`jobs|sed -n "/vim/s/\[\([0-9]\)\]+.*/\1/p"`
    if [ -n "$vim_id" ]; then
        echo "tabedit $@" > ~/.vim_swap/e.vim && fg $vim_id
    else
        vim $@
    fi
}

add below to .vimrc

nnoremap <silent> <space>e :source $HOME/.vim_swap/e.vim<Bar>:call writefile([], $HOME."/.vim_swap/e.vim")<CR>

Then v foo.c to open first file, editing..., ctrl-z to suspend vim, do shell stuff, v bar.h to bring vim foreground.

And in VIM, press <Space>e to tabedit bar.h.

So the idea is to generate vim command from shell command, save them to a temp .vim file. In VIM, map key to source the .vim file and clear it.

Upvotes: 7

JackLeo
JackLeo

Reputation: 4740

I would just call vim from fg and open new file inside vim since its just seems to be faster (although it may be just faster to me). To work with multiple files inside vim you need to use command edit (in vim): :e [filepath/]filename and you walk true buffers (all files will be as vim buffers) with ^I (ctrl+I) and ^O (ctrl+o)

It works on both GTK and shell versions. There is no such a huge difference on workflow. I prefer shell version since i do most of commands there (compiling launching etc.).

Upvotes: 3

Fred Nurk
Fred Nurk

Reputation: 14212

Instead of running vim again, you need to bring your current vim process to the foreground (with fg) and open the file in vim.

I have not used it much, but you may find the "vim server" feature (see --remote*, --servername, etc. options) lets you open the file from your shell into an existing, backgrounded vim. However, ctrl-z suspends the process instead of allowing it to continue to run in the background, and you will need to put that vim into the background so it can respond as a "vim server". Use the shell's bg command to do that.

Upvotes: 5

Related Questions