Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
附加到vim中选定文本的顶部和底部_Vim - Fatal编程技术网

附加到vim中选定文本的顶部和底部

附加到vim中选定文本的顶部和底部,vim,Vim,因此,我有以下文本(==分隔符表示所选文本): 现在我想创建一个vim函数,这样在调用时,它会在顶部和底部附加一些默认文本。例如,我想以以下内容结束: This is some text I'm not interested in. This is indented text not important. THIS TEXT WAS APPENDED AFTER FUNCTION CALL This text portion is selected

因此,我有以下文本(==分隔符表示所选文本):

现在我想创建一个vim函数,这样在调用时,它会在顶部和底部附加一些默认文本。例如,我想以以下内容结束:

This is some text
I'm not interested in.
    This is indented text
    not important.
    THIS TEXT WAS APPENDED
    AFTER FUNCTION CALL
    This text portion is
    selected.
    THIS OTHER TEXT WAS
    APPENDED AFTER THE SAME
    FUNCTION CALL
    Also not important
    indented text
Other text i'm not
interested in.
请注意,应该保留缩进(感谢benjifisher)
我该怎么做呢?

这有点草率,但它可以:

function! Frame() range
  '<put!=\"THIS OTHER TEXT WAS\nAPPENDED AFTER\nFUNCTION CALL\"
  '>put=\"THIS OTHER TEXT WAS\nAPPENDED AFTER THE SAME\nFUNCTION CALL\"
endfun

这是可行的,但我忘了提到我需要保留缩进。我编辑了这个问题。如果您知道vim latex suite,我需要的与按F5(创建新环境)时得到的非常相似,但是在本例中,我不需要声明任何环境(我总是希望函数附加一些默认文本),请告诉我有关缩进的更多信息。您想要在正常模式下使用
=
的效果吗?是否要从选定的第一行复制缩进?所有选定的行缩进的方式是否相同?缩进中是否有硬制表符或空格?理想的缩进解决方案是:*在顶部)缩进应等于第一个选定行*在底部)缩进应等于最后一个选定行。我不理解=在正常模式下的行为。
function! Frame() range
  '<put!=\"THIS OTHER TEXT WAS\nAPPENDED AFTER\nFUNCTION CALL\"
  '>put=\"THIS OTHER TEXT WAS\nAPPENDED AFTER THE SAME\nFUNCTION CALL\"
endfun
:'<,'>call Frame()
" Add lines at the bottom before adding at the top, since the line numbers
" will change when you add lines.
function! Frame() range
  " Define a list of lines and prepend the desired indentation.
  let botlines = ['THIS OTHER TEXT WAS', 'APPENDED AFTER THE SAME', 'FUNCTION CALL']
  let botspaces = matchstr(getline(a:lastline), '\s*')
  let lines = []
  for line in botlines
    call add(lines, botspaces . line)
  endfor
  " Now add those lines at the bottom.
  call append(a:lastline, lines)
  " Now do the same thing at the top.
  let toplines = ['THIS OTHER TEXT WAS', 'APPENDED AFTER', 'FUNCTION CALL']
  let topspaces = matchstr(getline(a:firstline), '\s*')
  let lines = []
  for line in toplines
    call add(lines, topspaces . line)
  endfor
  call append(a:firstline - 1, lines)
endfun