VIM: Check if the file is open on the current tab? window? (and activate it)

In vim, you can check if a file is open in the current buffer using bufexists . For the short file name (not the full path), you can check if it is open with bufexists(bufname('filename')) .

Is there a way to check if a file is open on a tab?

My nearest solution is to do something like:

 :tabdo if bufnr(bufname('filename')) in tabpagebuflist(): echo "Yes" 

However, this kind of pythonic pseudocode ... I'm not sure how to get this to work in vim. My goal is that the external applescript checks to see if the file is open and if it jumps to a line in that file.

Ideally, I would also like to be able to search in different GUI windows, but I have collected (for example, Open the vim tab in a new (GUI) window? ) That working with different GUI windows is very difficult / impossible in VIM.

+4
source share
2 answers

My impatience and good documentation have improved me ... here is a solution (helps a lot to Check if there is a current tab in vim and Open vim tab in a new window (GUI)? ). Source located at https://github.com/keflavich/macvim-skim

 function! WhichTab(filename) " Try to determine whether file is open in any tab. " Return number of tab it open in let buffername = bufname(a:filename) if buffername == "" return 0 endif let buffernumber = bufnr(buffername) " tabdo will loop through pages and leave you on the last one; " this is to make sure we don't leave the current page let currenttab = tabpagenr() let tab_arr = [] tabdo let tab_arr += tabpagebuflist() " return to current page exec "tabnext ".currenttab " Start checking tab numbers for matches let i = 0 for tnum in tab_arr let i += 1 echo "tnum: ".tnum." buff: ".buffernumber." i: ".i if tnum == buffernumber return i endif endfor endfunction function! WhichWindow(filename) " Try to determine whether the file is open in any GVIM *window* let serverlist = split(serverlist(),"\n") "let currentserver = ???? for server in serverlist let remotetabnum = remote_expr(server, \"WhichTab('".a:filename."')") if remotetabnum != 0 return server endif endfor endfunction 

then use this:

 exec "tabnext ".WhichTab('my_filename') echo remote_foreground( WhichWindow('my_filename') ) 

or, from the command line, here is a script to go to a specific line of the file using WhichTab :

 #!/bin/bash file="$1" line="$2" for server in `mvim --serverlist` do foundfile=`mvim --servername $server --remote-expr "WhichTab('$file')"` if [[ $foundfile > 0 ]] then mvim --servername $server --remote-expr "foreground()" mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" <CR>" mvim --servername $server --remote-send ":$line <CR>" fi done 
+6
source

I would answer Keflavich, but I still can’t ...

I worked on a similar issue when I wanted to imitate the behavior of gvim -remote-tab-silent when opening files inside gvim. I found this WhatTab script of yours, but ran into problems when several windows are open on any given tab. If you split the windows inside the tabs, you will have more than one buffer returned by tabpagebuflist (), so your method of using the position of the buffer number in the list does not work. Here is my solution that takes this opportunity into account.

 " Note: returns a list of tabnos where the buf is found or 0 for none. " tabnos start at 1, so 0 is always invalid function! WhichTabNo(bufNo) let tabNos = [] for tabNo in range(1, tabpagenr("$")) for bufInTab in tabpagebuflist(tabNo) if (bufInTab == a:bufNo) call add(tabNos, tabNo) endif endfor endfor let numBufsFound = len(tabNos) return (numBufsFound == 0) ? 0 : tabNos endfunction 

I think that I can just return tabNos, which will be an empty list that will be evaluated as scalar 0, but I just recognized vimscript, and I'm still not comfortable with the features of its dynamic behavior when typing, so I leave it like this now.

+3
source

Source: https://habr.com/ru/post/1390694/


All Articles