1

我正在尝试编辑我的环绕.vim 文件,以映射一个用于添加 erb 标签的快捷键。这是我指的stackoverflow帖子:

VIM:插入空的 ERB 标签

我正在使用这个 VIM 包https://github.com/spf13/spf13-vim,其中包括 Surround.vim

该stackoverflow帖子的答案说有一个文件位于“〜/ .vim/after/ftplugin/erb.vim”

由于我使用的是捆绑包,因此我的文件结构不同,因此我仔细研究并找到了这个“~/.vim/bundle/vim-surround/plugin/surround.vim”

该文件显示以下代码部分:(完整代码在此处的要点上https://gist.github.com/lando2319/5650746

  elseif exists("b:surround_".char2nr(newchar))
    let all    = s:process(b:surround_{char2nr(newchar)})
    let before = s:extractbefore(all)
    let after  =  s:extractafter(all)
  elseif exists("g:surround_".char2nr(newchar))
    let all    = s:process(g:surround_{char2nr(newchar)})
    let before = s:extractbefore(all)
    let after  =  s:extractafter(all)
  elseif newchar ==# "p"
    let before = "\n"
    let after  = "\n\n"
  elseif newchar ==# 's'
    let before = ' '
    let after  = ''
  elseif newchar ==# ':'
    let before = ':'
    let after = ''

如果我正确理解这一点,我需要将下面的代码添加到这个文件的某个地方到我的环绕.vim 文件中,我相信在第 151 行左右。

let b:surround_{char2nr('=')} = "<%= \r %>"
let b:surround_{char2nr('-')} = "<% \r %>"

到目前为止,我尝试将它添加到几个地方,然后在 VIM 中关闭并重新打开一个文件,然后 "cntl-s, =" 到目前为止没有运气。有什么建议么?

4

4 回答 4

3

创建文件~/.vim/after/ftplugin/erb.vim并添加以下行:

let b:surround_{char2nr('=')} = "<%= \r %>"
let b:surround_{char2nr('-')} = "<% \r %>"

另一种方法是使用autocmd's 来激活环绕映射。我更喜欢使用目录,而不是使用特定于文件类型的设置~/.vim/after/来混乱我。~/.vimrc

于 2013-05-25T23:38:17.000 回答
2

No you should not add the two lines into the plugin's (surround.vim) source. You should add them into your vimrc.

If you want your - and = does <% \r %> and <%= \r %> for all filetypes when you do surrounding, (e.g. yss- or yss=) .

You just add

let g:surround_{char2nr('=')} = "<%= \r %>"
let g:surround_{char2nr('-')} = "<% \r %>"

to your vimrc. please notice that they are global (with g: prefix) variables.

If you just want your customized - and = to work for certain filetype, you could create an autocmd and set buffer scope variables (with b:, same as the two lines in your question)

I am not familiar with ruby world. assume that the filetype you want to use the surrounding is erb, you could in your vimrc:

fun! AutoCmd_ERB()
        "do some other settings/mappings for your ERB, if you have
        "......
        "the customized surrounding :
        let b:surround_{char2nr('=')} = "<%= \r %>"
        let b:surround_{char2nr('-')} = "<% \r %>"
endf
autocmd FileType erb call AutoCmd_ERB()
于 2013-05-25T23:27:22.603 回答
0

如果它对任何人有帮助,对我来说,在我将 ~/.vim/after/ftplugin 中的名称从“erb.vim”更改为“eruby.vim”之后它就起作用了。

于 2014-05-29T21:45:59.403 回答
0

Welcome to Vim, I suppose!

Never edit plugin source files, it is always wrong.

Whenever you need to customize your setup, your vimrc file is where it happens. Type :h vimrc-intro to learn more.

In the case of surround.vim, the documentation states

The following adds a potential replacement on "-" (ASCII 45) in PHP files. (...) The carriage return will be replaced by the original text.

autocmd FileType php let b:surround_45 = "<?php \r ?>"

This means that you need to add the following entries to your vimrc file. For your purposes, this means that you need to add the lines

autocmd FileType erb let b:surround_{char2nr('=')} = "<%= \r %>"
autocmd FileType erb let b:surround_{char2nr('-')} = "<% \r %>"

to your vimrc file. Your vimrc file is located at ~/.vimrc (or $HOME\_vimrc if you're on Windows).

Restart Vim and you're ready to get to work.

于 2013-05-25T23:29:30.297 回答