将一个linux vimrc函数转换为一个可以在cygwin中工作的函数

7gcisfzg  于 2022-11-11  发布在  Linux
关注(0)|答案(1)|浏览(144)

我经常使用screen和vim。因此,当我打开一个已经在另一个vim会话中打开的文件时,在另一个屏幕窗口中,我讨厌玩“waldo在哪里”,并试图找出该文件恰好在我的22个屏幕窗口中的哪个窗口中打开。
因此,我写了下面的.vimrc函数,这些函数在redhat、debian、ubuntu和centos中都很好用。但是,它在cygwin中不起作用,因为找不到“lsof”。我不得不不时地稍微修改这些函数,只是为了让路径起作用,但总的来说,当我打开一个已经在Vim的另一个窗口打开的文件时,Vim会告诉我它在哪个窗口打开(通过数字),这样我就可以去那里,关闭它,或者在那里编辑它。
有没有人能帮我调整一下这个,这样它就能在Windows10上的cygwin中工作了?

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" for screen use
" opened in another screen? lets not play where's waldo anymore!
" this will politely tell me which screen's window it is open in so that I may
" go close it if I want, but still provide me the normal options
"
augroup NoSimultaneousEdits
    autocmd!
    autocmd SwapExists * :call PrintScreenWindow()
augroup END

function! PrintScreenWindow ()
  let fname = expand("%:p")
  " fix fname here, remove the path and leave only the filename/basename
  let fname =  fnamemodify(fname, ':t')
  " I just added the 'fpath', and 'all' variables,
  " The below my_command USED to use fname, but I found that if you have the
  " file open somewhere else (in a different screen window) AND also, have the
  " same filename, with a different path open, in a different window, it
  " causes an error, I.E.:
  " /root/abc/test.txt -- open in window 0
  " /root/abc/def/test.text -- open in window 1
  " now, in window 2, try to open /root/abc/test.txt, the below my_command
  " USED to have the variable 'fname', where it now has the variable 'all'
  " and this caused an error.
  " Adding this 'fpath', and 'all', fixes this issue.
  let fpath = expand("%:p:h")
  let all = fpath . "/." . fname
  " you might have to fix your path to lsof
  let my_command = "lsof | grep '" . fname . ".swp' | grep " . $USER . " | sed -n 's/^vim\\? \\+\\([0-9]\\+\\).*$/\\1/p' "
  " let my_command = "lsof | grep '" . all . ".swp' | /bin/grep " . $USER . " | sed -n 's/^vim\\? \\+\\([0-9]\\+\\).*$/\\1/p' "
  let result = substitute(system(my_command), '\n','','')
  if result
    let my_cmd2 = "cat /proc/" . result . "/environ | xargs -0 echo | sed -n 's/.*WINDOW=\\([0-9]*\\).*/\\1/p' "
    let res2 = substitute(system(my_cmd2), '\n','','')
    if res2 || res2 == '0'
      echo 'This file is already opened in window: ' . res2
    else
      echo "command failed: " . my_cmd2
    endif
  else
    echo my_command . " : cmd failed"
  endif
endfunction

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nbnkbykc

nbnkbykc1#

好吧,对于CYGWIN,你确实需要安装“busybox”
然后,在我的.vimrc文件中,更改:

let my_command = "lsof | grep '" . fname . ".swp' | grep " . $USER . " | sed -n 's/^vim\\? \\+\\([0-9]\\+\\).*$/\\1/p' "

至:

let my_command = "busybox lsof | grep '" . fname . ".swp' | cut -f1 "

因为没有列出任何用户,并且PID在前面,而不是在进程名称之后。
其次,改变:

let my_cmd2 = "cat /proc/" . result . "/environ | xargs -0 echo | sed -n 's/.*WINDOW=\\([0-9]*\\).*/\\1/p' "

至:

let my_cmd2 = "cat /proc/" . result . "/environ | xargs -0 echo | busybox sed -n 's/.*WINDOW=\\([0-9]*\\).*/\\1/p' "

相关问题