Vim:在基于光标下的字符的函数中做某些事情? [英] Vim: do something in a function based on character under a cursor?
问题描述
我在写一个函数来编辑LaTeX中的某个环境。
环境基本上看起来像这样:
\begin {quicktikz}
...一些东西...
\end {quicktikz}
或像这样:
\ begin * {quicktikz}
...一些东西...
\end {quicktikz}
b $ b
我想写一个函数,当在环境中调用时,在两者之间切换。由于我的Vim知识不是所有的,我想出了一个简单的解决方案:
- 获取光标位置
let save_cursor = getpos(。)
- 向后搜索
\begin {quicktikz}
使用:?\\begin {quicktikz} \ | \\begin\ * {quicktikz}
- 搜索
{
并向左移动:正常0f {h
- 检查光标下的项目是否等于
*
- 如果确实
正常x
- 如果不执行
正常a *< esc>
- 如果确实
- 使用
调用setpos('。',save_cursor)还原光标位置
- Get cursor position with
let save_cursor=getpos(".")
- Backward search for
\begin{quicktikz}
using:?\\begin{quicktikz}\|\\begin\*{quicktikz}
- Search for the
{
and move left using:normal 0f{h
- Check if the item under cursor equals
*
- if it does do
normal x
- if it doesn't do
normal a*<esc>
- if it does do
- Restore cursor position using
call setpos('.',save_cursor)
我知道如何做所有这些,除了步骤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:
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屋!