围绕选择创建虚拟编辑块 [英] Create virtualedit block around selection

查看:24
本文介绍了围绕选择创建虚拟编辑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,允许我选择几行并在其周围创建一个 virtualedit 块;最长行后 1 列.

I'm trying to create a script what permits me to select a few lines and create a virtualedit block around it; 1 column after the longest line.

这是我的代码

function! VirtualEdit()
  let EndLine = line("'>")

  set virtualedit = all

  let maxlength = max(map(range(line("'<"), line("'>")), "virtcol([v:val, '$'])"))-1
  call cursor(1,maxlength+1)  
  normal "^Q".EndLine."jc<space><ESC>"

  set virtualedit = ''
endfunction

它必须做的是
1) 控制我选择中的最长行(maxlength).
2) 将光标放在选区中最长行+1列的第一行.
3)比激活视觉块选择(在我的Windows pc上,命令是C-Q)
4) 将可视块扩展到选择中的最后一行(我电脑上用于向下移动的命令 = "j").
5)然后使用c"键插入空格"字符和ESC退出插入模式,以便用空格填充虚拟块列.

What it must do is
1) Control the longest line in my selection (maxlength).
2) Put the cursor on the first line in the selection at the column of the longest line +1 column.
3) Than Activate Visual Block selection (on my windows pc the command is C-Q)
4) Extend the visual block till the last line in the selection (the command on my pc for moving down = "j").
5) Than use the "c" key to insert a "space" character and ESC to exit the insert mode in order to fill the virtual block column with spaces.

但是我不知道如何在普通命令中使用变量(Endline).
我还注意到,在我上面的例子中,键和不起作用.
我做错了什么?

However I can't find out how to use a variable (Endline) in a normal command.
I noted also that keys as and don't work in my above example.
What did I wrong?

推荐答案

这里有很多错误:

  1. :set 不允许你在 =
  2. 周围有空格
  3. :set 不接受表达式,因此 set ve=''let &ve="''",而不是 let &ve='':set ve=.
  4. :normal 命令也不接受表达式,它接受刚刚执行的字符串.因此 :normal "^ 正在尝试使用寄存器 ^,失败(没有这样的寄存器)并停止处理该行的其余部分.使用 :execute 将表达式传递给 :normal.
  5. :normal 命令不接受 语法.viml 表达式也没有,它们有 "\" 代替(注意:只有双引号和反斜杠).^Q 语法不被任何人接受,在文本文件中包含原始控制代码(vim 显示为 ^Q)并不是最好的主意.
  6. 不要一成不变地使用:normal.大多数时候你不需要它(在这种情况下你需要用 \<C-v> 替换 ^Q ,因为它是一个映射).
  7. 不要对 virtualedit 值进行硬编码.而不是

  1. :set does not let you have spaces around =
  2. :set does not accept expressions, thus set ve='' is let &ve="''", not let &ve='' which is :set ve=.
  3. :normal command also does not accept expressions, it accepts strings that are just executed. Thus :normal "^ is trying to use register ^, fails (no such register) and stops processing the rest of the line. Use :execute to pass expressions to :normal.
  4. :normal command does not accept <Key> syntax. Neither do viml expressions, they have "\<Key>" instead (note: only double quotes and with backslash). ^Q syntax is not accepted by anybody and having raw control codes (displayed by vim as ^Q) inside a text file is not the best idea.
  5. Don’t use :normal without a bang. Most of time you don’t need it (you need to replace ^Q with \<C-v> in this case though because it is a mapping).
  6. Don’t hardcode virtualedit value. Instead of

set ve=all
<...>
set ve=

使用

let savedve=&ve
set ve=all
try
    <...>
finally
    let &ve=savedve
endtry

  • {N}j 表示向下 N 行",而不是转到第 N 行".转到第 N 行"是 {N}gg{N}G.
  • 您有 let maxlen=<...>-1 和唯一使用 maxlen 的行,您有 maxlen+1.很奇怪.
  • {N}j means "N lines down", not "go to N’th line". "Go to N’th line" is {N}gg or {N}G.
  • You have let maxlen=<...>-1 and the on only line where maxlen is used you have maxlen+1. It is strange.
  • 如果你解决了这个问题,你可以继续,但你根本不需要调整 virtualedit 和使用 :normal:

    If you fix this you can proceed, but you don’t need adjusting virtualedit and using :normal at all:

    function AddSpaces()
        let [lstart, lend]=[line("'<"), line("'>")]
        if lstart>lend
            let [lstart, lend]=[lend, lstart]
        endif
        let maxcol=max(map(range(lstart, lend), "virtcol([v:val, '$'])"))
        let newlines=map(range(lstart, lend), 'printf("%-'.maxcol.'s", getline(v:val))')
        call setline(lstart, newlines)
    endfunction
    

    这篇关于围绕选择创建虚拟编辑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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