Rose Perrone
Rose Perrone

Reputation: 63546

Can I write a script to tell me which files are currently being edited in vim?

I'm using tmux with many windows, and I frequently lose track of which files I'm editing in vim. I'd like to have another shell open that runs a script that tells me the paths of files that vim is currently editing.

I'm running Mac OS.

Upvotes: 4

Views: 171

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172540

The way I would tackle the problem is to query all remote Vim processes for their opened buffers. You can use Vim's clientserver functionality for that. The GVIM server names are usually sequentially named: GVIM, GVIM1, ...; for terminal Vim, you'd have to name them with the --servername argument (e.g. via a shell alias).

You can then query the list of open files via the --remote-expr argument. A simple expression to loop over all listed buffers (like what the :ls command shows) is:

map(filter(range(1, bufnr('$')), 'buflisted(v:val) && ! empty(bufname(v:val))'), 'bufname(v:val)')

As you can see, it's a bit involved and might affect your workflow of launching Vim. Think hard whether you really need this!

Upvotes: 1

cosinuz
cosinuz

Reputation: 41

You could tweak around with the piped commands ps -eF | grep vim for your script. At the end of each line, of the result, you'll see you the different processes dealing with anything related to 'vim'. Therefore you'll find which files are currently being edited by vim('vim foo.txt' for instance), as well as 'grep vim' that was being active to get this result. To have a pretty output, you'd have to filter all of these with a script. I hope this will help you.

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191739

That I know of there is no way to get every open vim buffer from an external process. Instead of using separate tmux layouts and a separate instance of vim to edit multiple files, you could have one instance of vim and edit multiple separate files using :split and :tabnew. Then in that vim instance you can use :ls to see the paths of all open files relative to the current working directory. :pwd also works.

If this isn't your style and you'd still like to use vim in separate layouts, you can use ps to see the arguments to each vim process and check the cwd of these processes. Something like:

paste <(pgrep vim | xargs pwdx) <(pgrep vim | xargs ps -o %a | sed 1d)

Note that if you use multiple buffers in vim the above won't quite work because it will only list the arguments given to each vim command and not list the actual buffers.

Upvotes: 0

Related Questions