Saving Tab names and ConqueShells along with Vim sessions

前端 未结 1 1962
半阙折子戏
半阙折子戏 2021-01-14 12:36

Is there any way to get vim to save the tab names (assigned via the Tab Name script) and/or a terminal emulator (set up via the Conque Shell script) upon issuing the :

相关标签:
1条回答
  • 2021-01-14 12:55

    After learning some basic VimScript I just gave up and used Python instead (to cite one example, you can't save global information to a session if it is a list). Here is a solution I found for saving tab names (will post a solution for ConqueShell if I find one)

    Put the following in your .vimrc file and use whatever mapping you want to quickly save and load your sessions

    "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>
    

    Now create the following text file and put it somewhere in your PATH variable (echo $PATH to get it, mine is at /home/user/bin/mksession.py) and make sure to make it executable (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()
    
    0 讨论(0)
提交回复
热议问题