在LaTeX环境中进行Vim操作

WiF*_*215 6 vim latex

我想知道当我在使用vim编写代码/乳胶时在某个特定环境中时我怎么能触发某个动作.例如,假设我正在编写tex并在对齐环境中,那么我希望我输入的每个等号转换为&=.

我现在想的是相当麻烦.这是我的vimrc中的伪代码.这是它的样子

Shortcut for environment opens start and end tag for align environment (Using imap)
The above action also initiates a counter variable, sets it to 1
If counter is 1, every equals sign I enter should be entered as an &=.
Shortcut to force counter to zero when I move out of the environment. (Using imap)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更简单的方法来解决这个问题.这只是一个例子.这里的主要想法是让vim具有环境意识.

期待您的建议.

Con*_*ner 2

我通常会在事后解决此类问题。例如,我用正常的=符号编写它,然后直观地选择它的区域并使用:s/=/\&=/g。如果您想在事后对“环境”执行多项操作,您可以创建一个函数并触发一个键来执行它。例如:

vnoremap <leader>la :call <SID>latexAlign()<cr>

function! s:latexAlign() range
  exe ":'<,'>s/=/\&=/g"
  " add more stuff here...
endfunction
Run Code Online (Sandbox Code Playgroud)

但是,我认为您的解决方案更好,以防您稍后返回并进行编辑。vim 中的 htmlcomplete 文件处理这种情况如下:

let stylestart = searchpair('<style\>', '', '<\/style\>', "bnW")
let styleend   = searchpair('<style\>', '', '<\/style\>', "nW")
if stylestart != 0 && styleend != 0
   if stylestart <= curline && styleend >= curline
      let start = col('.') - 1
      let b:csscompl = 1
      while start >= 0 && line[start - 1] =~ '\(\k\|-\)'
         let start -= 1
      endwhile
   endif
endif
Run Code Online (Sandbox Code Playgroud)

您可以看到它如何使用 searchpair 来确定它是否在标签内<style></style>。请参阅:help searchpair()获取更多信息。