Vim action inside LaTeX environment

I would like to know how I can trigger a specific action when I am in a certain environment when writing code / latex using vim. For example, suppose I write tex inside the alignment environment too, then I would like every equal sign I entered to convert to & =.

What I'm thinking about is rather cumbersome. This is the pseudo code for my vimrc. That's what it looks like

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) 

I would like to know if there is an easier way around this. This is just an example. The main idea here is for vim to have environmental awareness.

Wait for your offers.

+6
source share
3 answers

I generally solve this problem after the fact. For example, I write it with the usual = signs, and then visually select its area and use :s/=/\&=/g . If you want to do several things in an β€œenvironment” after the fact, you can create a function and run a key to execute it. For instance:

 vnoremap <leader>la :call <SID>latexAlign()<cr> function! s:latexAlign() range exe ":'<,'>s/=/\&=/g" " add more stuff here... endfunction 

However, I think your solution is better if you come back and edit it later. The html complete file in vim handles this situation as follows:

 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 

You can see how it uses searchpair to determine if it is in the <style></style> tags. See :help searchpair() .

+2
source

This is a very interesting question, but I'm not sure if your proposed approach is the best way! I tried to answer, but this first iteration may not be perfect. I assume that the LaTeX environment is simply limited to the \begin and \end commands (my LaTeX is a bit rusty!).

First, you need a function to return the environment to the cursor position. My function below goes to the previous \begin command, and then goes to the corresponding \end command, calculates whether the cursor is inside this area, and continues the process until a containing environment is found. It should handle nested environments. If not in LaTeX, an empty string is returned.

 function! GetTeXEnv() let pos = getpos('.') let win = winsaveview() let env = '' let carry_on = 1 let search_ops = 'bWc' let b_start = line('.') while carry_on keepjumps let b_start = search('\\begin{',search_ops) let search_ops = 'bW' " Only accept a match at the cursor position on the " first cycle, otherwise we wouldn't go anywhere! let env = matchstr(getline('.'),'\\begin{\zs.\{-}\ze}') let env_esc = escape(env,'*') keepjumps let b_end = search('\\end{\s*' . env_esc . '\s*}','Wn') if b_start == 0 " finished searching; stop let carry_on = 0 elseif b_end > b_start && b_end < pos[1] " not inside this env; carry on let env = '' elseif b_end > b_start && b_end >= pos[1] && b_start <= pos[1] " found it; stop let carry_on = 0 endif endwhile call setpos('.',pos) call winrestview(win) return env endfunction 

Now you can query the cursor environment using :echo GetTeXEnv() .

To change the behavior of the = key, you need to create another function that returns &= in the alignment environment, and = otherwise:

 function! TeXEquals() return GetTeXEnv() =~ 'align\*\?' ? "&=" : "=" endfunction 

Then you can reassign = in insert mode (only for TeX files)

 autocmd FileType tex inoremap <silent> = <cr>=TeXEquals()<CR> 

This seems to work in my LaTeX file example. Let me know if you find any problems or any ideas for improvement.

+2
source

If you are looking for "environmental awareness" and if by that you mean "am I in a latex file?" you can enable the behavior using autocommand .

Vim recognizes many types of files, and you can make sure that the type of file you are editing is recognized by default:

 :echo &filetype 

When editing a Python file, I get python as a result. If you only need this for latex, and you get a satisfactory answer from this Vim variable, you can set autocommand , which uses this, otherwise you can specify extensions in it, it will look like this:

 autocmd BufReadPost,BufNewFile,BufEnter *.tex, *.latex call MyFunction() 

If RedPost, NewFile and Enter are possible Vim interactions that activate your MyFunction when file extensions end in .text or .latex

If you really need some example code that will do what is stated in your description, then Conner's answer might be better.

0
source

Source: https://habr.com/ru/post/922231/


All Articles