Take the 2-minute tour ×

The question is best given by examples:

vim example.txt #Opens Vim on new or existing file


vim someother.txt


vim example.txt #Auto-runs `fg 1` since example.txt is already open

Is this possible?

share|improve this question
Consider using screen or switching virtual terminals. –  ultrasawblade May 14 '12 at 3:50
add comment

4 Answers

No. vim (or any other application for that matter) has no way of communicating to bash that it should take another action instead.

share|improve this answer
actually some application (including vim) can communicate with bash they are started from –  Alex May 14 '12 at 9:30
@Alex: How so ? –  Ignacio Vazquez-Abrams May 14 '12 at 11:36
e.g. as mentioned in the other answer emacs has M-x shell option; in vim there is ! followed by command to execute shell commands, as well as "system()" and other functions that can be used in vimrc. –  Alex May 15 '12 at 2:26
@Alex: Those don't "communicate with bash", they run a whole new shell. –  Ignacio Vazquez-Abrams May 15 '12 at 2:28
add comment

You may want consider using vim buffers to edit multiple files.

Edit both files: vim example.txt someother.txt then swith between them using :buffer <filename>.

See also the easier buffer switching tip and the Vim buffer FAQ.

share|improve this answer
I know this method but it's too application specific + requires one to know all files they will be working on –  Alex May 14 '12 at 9:29
Not sure what you mena by "too application specific". Your example uses vim as well, if you are not lookign for a way to edit multiple files with vim then perhaps you need to specify that in the question. As for not knowing in advance which files to edit, you can add more files to the buffer list with :e <file>. –  Bram May 14 '12 at 12:23
vim is given as example in both the question and the answer. in the answer one can just replace vim $1 with nano $1 or passwordmanagerpro $1 etc. –  Alex May 15 '12 at 1:58
add comment
up vote 0 down vote accepted

An example solution, works under bash (requires paste, grep, sed). It is specific to Vim but can be adapted to another editor.

jim ()  { 
  paste <(jobs) <(jobs -p | xargs -n1 ps o cmd= -p) |
  grep "$1$" > ~/tmp/jm && fg $(cat ~/tmp/jm | sed -r 's/^\[([0-9]+).*/\1/')
  [[ $? == 1 ]] && vim $1


share|improve this answer
add comment

I don't think there is such a generic way to reuse jobs.

For your editing workflow, you might want to switch to emacs. Instead of hitting ctrl-z to return to the shell, you can run a child shell in an emacs buffer with M-x ansi-term or M-x shell, and switch back and forth between the shell and your edit buffers, or even split the view to show both at once. Emacs tracks the working directory of the shell so if you navigate around the filesystem and decide you want to open a file, instead of typing emacs somefile, you can just tell emacs to open it with C-x C-f somefile<RET> and it will open the file in another buffer and switch to it.

You can also start the emacs server and run emacsclient somefile to tell the existing emacs session to open somefile. If it already has it open, it just switches to the existing buffer.

share|improve this answer
add comment

Your Answer


