Vim:在基于光标下的字符的函数中做某些事情? [英] Vim: do something in a function based on character under a cursor?

查看:230
本文介绍了Vim:在基于光标下的字符的函数中做某些事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个函数来编辑LaTeX中的某个环境。



环境基本上看起来像这样:

  \begin {quicktikz} 
...一些东西...
\end {quicktikz}

或像这样:

  \ begin * {quicktikz} 
...一些东西...
\end {quicktikz}


b $ b

我想写一个函数,当在环境中调用时,在两者之间切换。由于我的Vim知识不是所有的,我想出了一个简单的解决方案:


  1. 获取光标位置 let save_cursor = getpos(。)

  2. 向后搜索 \begin {quicktikz} 使用:?\\begin {quicktikz} \ | \\begin\ * {quicktikz}

  3. 搜索 {并向左移动:正常0f {h

  4. 检查光标下的项目是否等于 *

    • 如果确实正常x

    • 如果不执行正常a *< esc>


  5. 使用调用setpos('。',save_cursor)还原光标位置



    1. 我知道如何做所有这些,除了步骤3.如何检查游标下的字符是否等于 *



      如果你知道一个更好的方法这样做,欢迎分享这个。

      解决方案

      我认为检索游标下的字符的最简单方法是:

        getline(。)[col(。) -  1] 

      或者,您可以使用 strpart()

        strpart(getline(。),col(。) -  1,1)






      第一个表达式首先调用函数 getline() 传递
      参数,光标所在的位置将被返回。
      然后我们使用所谓的 expr8 expr - [] (参见 help )来检索
      单个字符。传递的数字来自另一个函数, col()
      返回当前光标列。

       <$> 

      您可以使用

      c $ c> if getline(。)[col(。) - 1] =='*'
      ...


      I'm writing a function that edits a certain environment in LaTeX.

      The environment basically looks like this:

      \begin{quicktikz}
          ...some stuff...
      \end{quicktikz}
      

      or like this:

      \begin*{quicktikz}
          ...some stuff...
      \end{quicktikz}
      

      I want to write a function that toggles between the two, when called from within the environment. Since my Vim knowledge ain't all that, I'm coming up with a simple solution:

      1. Get cursor position with let save_cursor=getpos(".")
      2. Backward search for \begin{quicktikz} using: ?\\begin{quicktikz}\|\\begin\*{quicktikz}
      3. Search for the { and move left using: normal 0f{h
      4. Check if the item under cursor equals *
        • if it does do normal x
        • if it doesn't do normal a*<esc>
      5. Restore cursor position using call setpos('.',save_cursor)

      I know how to do all of this except for step 3. How can I check if the char under the cursor equals to * or not?

      If you know a better way of doing this, sharing this would be welcome.

      解决方案

      I think the easiest way to retrieve the char under cursor is:

      getline(".")[col(".")-1]
      

      Alternatively, you can do it with strpart()

      strpart(getline("."), col(".")-1, 1)
      


      The first expression first calls the function getline() passing "." as argument which means the line where the cursor is positioned will be returned. Then we use the so called expr8 or expr-[] (see the help) to retrieve a single character. The number passed comes from another function, col() which returns the current cursor column. As indexes start in 0, one is subtracted.

      You can use it like

      if getline(".")[col(".")-1] == '*'
              ...
      

      这篇关于Vim:在基于光标下的字符的函数中做某些事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆