Re-use existing jobs when opening files

0

2

The question is best given by examples:

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

Ctrl+Z

vim someother.txt

Ctrl+Z

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

Is this possible?

Alex

Posted 2012-05-14T02:30:20.537

Reputation: 973

Consider using screen or switching virtual terminals. – LawrenceC – 2012-05-14T03:50:29.820

Answers

0

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
}

Enjoy!

Alex

Posted 2012-05-14T02:30:20.537

Reputation: 973

0

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

Ignacio Vazquez-Abrams

Posted 2012-05-14T02:30:20.537

Reputation: 100 516

actually some application (including vim) can communicate with bash they are started from – Alex – 2012-05-14T09:30:38.427

@Alex: How so ? – Ignacio Vazquez-Abrams – 2012-05-14T11:36:07.273

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 – 2012-05-15T02:26:48.263

@Alex: Those don't "communicate with bash", they run a whole new shell. – Ignacio Vazquez-Abrams – 2012-05-15T02:28:33.767

0

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.

Bram

Posted 2012-05-14T02:30:20.537

Reputation: 582

I know this method but it's too application specific + requires one to know all files they will be working on – Alex – 2012-05-14T09:29:46.197

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 – 2012-05-14T12:23:13.283

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 – 2012-05-15T01:58:33.290

0

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.

psusi

Posted 2012-05-14T02:30:20.537

Reputation: 7 195