Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have opened several text files (a.txt, b.txt, c.txt, ...) in different sessions of vi launched from different instances of a bash shell. Having then accessed that machine remotely, I wish to determine which session of vi is associated with a particular text file (e.g., a.txt) and specifically close that session, preferably while saving the current contents of the buffer (as opposed to executing a process kill). How can these actions be performed?

share|improve this question
1  
To find out which file is associated with each session, you can try: file /proc/<pid>/fd/6 This gives you the name of the swap file vi is using, which directly tells you which file it's operating on. It's rather hackish and it doesn't help you save buffer contents. I'm hoping a vi guru can enlighten us on that matter. –  Joseph R. Nov 26 '12 at 17:50
    
@JosephR. Wonderful solution to the first half of my question. In my case, the /proc/<pid>/fd/ folder contains the symlinks 0, 1, 2, and 4. The first three point to /dev/pts/3 and the last one points to the swap file. Many thanks. –  user001 Nov 26 '12 at 17:56
1  
My bad. Turns out the extra file-handles on my system were broken symlinks for some reason... Just goes to show you that this solution isn't so robust. It can be made robust (e.g. for automation) by running file on all the files in the fd directory and parsing the output for a line containing ".swp" –  Joseph R. Nov 26 '12 at 17:58
    
This is turning out to be quite an interesting exercise! I tried printf ":wq\n" > /proc/<pid>/fd/0 but no joy! ":wq" appears in the vi window but vi doesn't seem to respond. –  Joseph R. Nov 26 '12 at 18:10
    
You could try GNU screen. Specifically the stuff command can enter text as though it was typed. I expect tmux has similar features. –  donothingsuccessfully Nov 26 '12 at 18:13
show 4 more comments

1 Answer

To see open files, use lsof(8). To control an existing instance of vim in order to tell it to save a buffer and quit, you can use its server/client functionality. See this question for more info on that: How can I configure vim so that when I send the process a USR1 signal it saves and quits

share|improve this answer
1  
The vim server requires X.org to work. I think the use-case here is using ssh to access a command line without X. –  donothingsuccessfully Nov 26 '12 at 20:11
    
As far as I know, using the server client functionality requires vim to have been compiled to support it in the first place. This may or may not be the case with the OP. Besides, I think the OP was talking about vi not vim. –  Joseph R. Nov 26 '12 at 20:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.