gpt4 book ai didi

shell - 保存选项卡名称和 ConqueShells 以及 Vim session

转载 作者:行者123 更新时间:2023-12-04 03:06:45 27 4
gpt4 key购买 nike

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

观察下面(放大),我在左侧有一个工作 session ,并且通过 vim -S fileName 加载了相同的 session 。命令,在右边。分配的选项卡标签恢复为绝对路径,ConqueShell 终端被解释为文件。

Failed Session

最佳答案

在学习了一些基本的 VimScript 之后,我就放弃了,转而使用 Python(举一个例子,如果它是一个列表,你不能将全局信息保存到一个 session 中)。这是我找到的用于保存选项卡名称的解决方案(如果找到,将发布 ConqueShell 的解决方案)

将以下内容放入您的 .vimrc文件并使用您想要快速保存和加载 session 的任何映射

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

关于shell - 保存选项卡名称和 ConqueShells 以及 Vim session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com