保存标签页名称和ConqueShells以及Vim会话

5

有没有办法让vim在执行:mksession [fileName]命令时保存选项卡名称(通过Tab Name script分配)和/或终端仿真器(通过Conque Shell script设置)?

请注意下面的截图(放大),左侧是一个正常工作的会话,右侧是通过vim -S fileName命令加载的相同会话。分配的标签标签恢复为绝对路径,ConqueShell终端被解释为文件。

Failed Session

1个回答

5

在学习了一些基础的VimScript后,我放弃了并转而使用Python(举个例子,如果是列表,则无法将全局信息保存到会话中)。下面是我找到的一种保存选项卡名称的解决方案(如果我找到了ConqueShell的解决方案,我会发布一篇文章)

将以下内容放入您的.vimrc文件中,并使用任何映射来快速保存和加载您的会话

"Tokenize it so it has the following form (without spaces)
"Label1 JJ Label2 JJ Label3 JJ Label4
"Or if you prefer use something other than 'JJ' but DO NOT
"use symbols as they could interfere with the shell command
"line
function RecordTabNames()
   "Start at the first tab with no tab names assigned
   let g:TabNames = ''
   tabfirst

   "Iterate over all the tabs and determine whether g:TabNames
   "needs to be updated
   for i in range(1, tabpagenr('$'))
      "If tabnames.vim created the variable 't:tab_name', append it
      "to g:TabNames, otherwise, append nothing, but the delimiter 
      if exists('t:tab_name')
         let g:TabNames = g:TabNames . t:tab_name  . 'JJ'
      else
         let g:TabNames = g:TabNames . 'JJ'
      endif

      "iterate to next tab
      tabnext
   endfor
endfunction

func! MakeFullSession()
   call RecordTabNames()
   mksession! ~/.vim/sessions/Session.vim
   "Call the Pythin script, passing to it as an argument, all the 
   "tab names. Make sure to put g:TabNames in double quotes, o.w.
   "a tab label with spaces will be passed as two separate arguments
   execute "!mksession.py '" . g:TabNames . "'"
endfunc

func! LoadFullSession()
   source ~/.vim/sessions/Session.vim
endfunc

nnoremap <leader>mks :call MakeFullSession()<CR>
nnoremap <leader>lks :call LoadFullSession()<CR>

现在创建以下文本文件并将其放在您的PATH变量中(echo $PATH获取它,我的在/home/user/bin/mksession.py),确保将其设为可执行(chmod 0700 /home/user/bin/mksession.py)。
#!/usr/bin/env python

"""This script attempts to fix the Session.vim file by saving the 
   tab names. The tab names must be passed at the command line, 
   delimitted by a unique string (in this case 'JJ'). Also, although
   spaces are handled, symbols such as '!' can lead to problems.
   Steer clear of symbols and file names with 'JJ' in them (Sorry JJ
   Abrams, that's what you get for making the worst TV show in history,
   you jerk)
"""
import sys
import copy

if __name__ == "__main__":
   labels = sys.argv[1].split('JJ')
   labels = labels[:len(labels)-1]

   """read the session file to add commands after tabedit
   " "(replace 'USER' with your username)
   "
   f = open('/home/USER/.vim/sessions/Session.vim', 'r')
   text = f.read()
   f.close()

   """If the text file does not contain the word "tabedit" that means there
   " "are no tabs. Therefore, do not continue
   """
   if text.find('tabedit') >=0:
      text = text.split('\n')

      """Must start at index 1 as the first "tab" is technically not a tab
      " "until the second tab is added
      """
      labelIndex = 1
      newText = ''
      for i, line in enumerate(text):
         newText +=line + '\n'
         """Remember that vim is not very smart with tabs. It does not understand
         " "the concept of a single tab. Therefore, the first "tab" is opened 
         " "as a buffer. In other words, first look for the keyword 'edit', then
         " "subsequently look for 'tabedit'. However, when being sourced, the 
         " "first tab opened is still a buffer, therefore, at the end we will
         " "have to return and take care of the first "tab"
         """
         if line.startswith('tabedit'):
            """If the labelIndex is empty that means it was never set,
            " "therefore, do nothing
            """
            if labels[labelIndex] != '':
               newText += 'TName "%s"\n'%(labels[labelIndex])
            labelIndex += 1

      """Now that the tabbed windowing environment has been established,
      " "we can return to the first "tab" and set its name. This serves 
      " "the double purpose of selecting the first tab (if it has not 
      " "already been selected)
      """
      newText += "tabfirst\n"
      newText += 'TName "%s"\n'%(labels[0])

      #(replace 'USER' with your username)
      f = open('/home/USER/.vim/sessions/Session.vim', 'w')
      f.write(newText)
      f.close()

如果有人知道如何设置.vimrc文件的当前文件格式,请随意这样做。它现在的高亮显示效果很差。 - puk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接