GithubHelp home page GithubHelp logo

rhysd / vim-operator-surround Goto Github PK

View Code? Open in Web Editor NEW
138.0 6.0 8.0 118 KB

Vim operator mapping to enclose text objects with surrounds like paren, quote and so on.

Ruby 1.73% Vim Script 98.27%
vim-plugin vim operator surround

vim-operator-surround's Introduction

Operator to Surround a Text Object

Build Status

This plugin provides Vim operator mappings to deal with surrounds like (), "" and so on. In addition, both end of the text object are the same character, vim-operator-surround recognizes them as a surround (this behavior is customizable with some variables).

It can

  • surround a text object with a specified block.
  • replace a surround in a text object with a specified block.
  • delete a surround in a text object.

This plugin would be more useful with vim-textobj-between or vim-textobj-anyblock. And you can customize and add surround definitions in global and filetype specific scope.

Policy of This Plugin (or The Reason Why I Don't Use vim-surround)

Requirements

This plugin uses vim-operator-user. Please install it in advance.

Customize

Set your favorite blocks to g:operator#surround#blocks. And control the behavior with some variables. Please read doc for more details.

Example of vimrc

" operator mappings
map <silent>sa <Plug>(operator-surround-append)
map <silent>sd <Plug>(operator-surround-delete)
map <silent>sr <Plug>(operator-surround-replace)


" delete or replace most inner surround

" if you use vim-textobj-multiblock
nmap <silent>sdd <Plug>(operator-surround-delete)<Plug>(textobj-multiblock-a)
nmap <silent>srr <Plug>(operator-surround-replace)<Plug>(textobj-multiblock-a)

" if you use vim-textobj-anyblock
nmap <silent>sdd <Plug>(operator-surround-delete)<Plug>(textobj-anyblock-a)
nmap <silent>srr <Plug>(operator-surround-replace)<Plug>(textobj-anyblock-a)

" if you use vim-textobj-between
nmap <silent>sdb <Plug>(operator-surround-delete)<Plug>(textobj-between-a)
nmap <silent>srb <Plug>(operator-surround-replace)<Plug>(textobj-between-a)

License

vim-operator-surround is distributed under MIT license.

Copyright (c) 2013-2017 rhysd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

vim-operator-surround's People

Contributors

aiya000 avatar hokorobi avatar machakann avatar rhysd avatar syngan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

vim-operator-surround's Issues

Backslash blocks don't work well

I would like to have a \langle ... \rangle surround for use in latex.
Hence I have defined:

let g:operator#surround#blocks = {
\ '-' : [
\   {'block': ['\langle', '\rangle'], 'motionwise': ['char', 'line', 'block'], 'keys': ['<', '>']},
\ ] }

And the following block with vim-textobj-user:

call textobj#user#plugin('latex', {
\   'code': {'pattern': ['\\langle\>', '\\rangle\>'], 'select-a': 'a<', 'select-i': 'i<' },
\ })

This allows me to change () to \langle\rangle using sra(<.
However I cannot change back. That is sra<( beeps and does nothing.
If I change to 'block': ['\\langle', '\\rangle'] I can do sra<(,
but then the original command inserts the wrong thing \\langle\\rangle

I seems to me this could be fixed by making sure we always search literaly, rather than using regex, when changing or deleting a surround.

Tag ~operator~

I couldn't find in the docs/issues (or in the related plugins docs/issues [some are in japanese]) if this plugin support tags like vim-surround:

Now press cs' to change it to

<q>Hello world!</q>

Thanks.

(vim-surround is for sure over complicated, thanks for this plugin)

g:operator#surround#blocks に正規表現を設定したい

いますぐほしい!というわけではないのですが忘れないうちに Issues に投げておきます。
以下のように g:operator#surround#blocks に正規表現を設定できれば便利そうだと思いました。

let g:operator#surround#blocks = {
\   "-" : [
\       { 'block' : ['\w*(', ')'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['(', ')'] },
\   ]
\}

<Plug>(operator-surround-delete)func(hoge)func() のみ削除するようなイメージです。
textobj-xbrackets.vim と組み合わせて使用することを考えています。
<Plug>(operator-surround-append)<Plug>(operator-surround-replace) で利用する場合は func(hoge( みたいに \w*( にマッチするワードを入力したら func({textobj})hoge({textobj}) みたいな感じになるとかですかねぇ。

.vimrcを再読み込みしたときにg:operator#surround#default_blocksがマージされない?

let g:operator#surround#blocksを設定している状態で.vimrcを再読み込み(:source $MYVIMRC)すると、g:operator#surround#blocksg:operator#surround#default_blocksがマージされないのか、デフォルトの設定が効かなくなってしまうようです。たとえば以下のように設定しているとき……

let g:operator#surround#blocks = {}
let g:operator#surround#blocks['-'] = [
    \   { 'block' : ['(', ')'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['P'] },
    \   { 'block' : ['「', '」'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['B'] },
    \   { 'block' : ['『', '』'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['D'] },
    \ ]

.vimrcを再読み込みしたあとに<Plug>(operator-surround-append)wPならば(word)としてくれるのですが、 <Plug>(operator-surround-append)w(すると、単語が(word(と囲まれてしまう感じです。

バグというよりは、私の設定のしかたがまずいのだろうと思ってはいるのですが、回避する方法などあればご教示いただけないでしょうか……!

Suggestion for documentation

I just think it might be worth mentioning that it's possible (and in my case very handy) to use this in visual mode and even visual block mode.

Example .vimrc:

vmap <silent>( <Plug>(operator-surround-append)( vmap <silent>[ <Plug>(operator-surround-append)[ vmap <silent>{ <Plug>(operator-surround-append){ vmap <silent>" <Plug>(operator-surround-append)" vmap <silent>' <Plug>(operator-surround-append)'

This works also in visual block mode in the sense that selecting a block and hitting ( for example, will put a ( and ) around the selection on each line.

escaping in config

For keys the (=<space>) character has to be escaped, for example

\ { 'block' : [" ", " "], 'motionwise' : ['char', 'line', 'block'], 'keys' : ["\ "] },

For block the \ (=) charcter has to be escpaed, for example

\ { 'block' : ["\\(", "\\)"], 'motionwise' : ['char','line','block'], 'keys' : ['eb'] },

How about a note in the documentation?

<Plug>(operator-surround-append) 時に textobj を入力する前に括弧を指定したい

lingr で言っていたやつですが、こちらにも要望を出しておきます。
現在、<Plug>(operator-surround-append)

  • operator → textobj →任意の括弧の入力

という順番で処理されるのですが、これを

  • operator →任意の括弧の入力→textobj

という風に textobj の前に括弧の入力を行いたいです。
これは例えば次のように利用することを想定しています。

" <Plug>(operator-surround-append-before) は先に括弧の入力を受け付けるマッピング
" <Plug>(operator-surround-append){textobj}( と同等
" e.g. s(iw は単語の範囲を () で囲む
map s( <Plug>(operator-surround-append-before)(

operator-surround-replace kills indent in visual mode

Let .vimrc contain

nnoremap <script> <SID>(hvgu) ^vg_
nmap <silent> css <SID>(hvgu)<Plug>(operator-surround-replace)

and let test.vim consist of the single line

  (nmap csg)

Expected: hitting cssB gives

  {nmap csg}

Result:

{nmap csg}

囲み処理を行ったあとに、処理を行いたい

VAC2014 紹介で存在知りました。
素晴らしいプラグインありがとうございます。

tpope/vim-surround からの移行中なのですが、
要望があります。

let g:operator#surround#blocks =
      \ {
      \   '-' : [
      \       { 'block' : ["{\<CR>", "\<CR>}"], 'motionwise' : ['line'], 'keys' : ['{', '}'] },
      \   ]
      \ }

この設定をしています。
C++コード編集中に波括弧で囲いたいときはこの定義がよく効くのですが、
その後インデントを合わせたいので
=%
を入力しています。
これをなんとか自動出来ないものかなと思いました。

よろしくお願いします。

set selection=exclusive の場合<Plug>(operator-surround-delete) が正しく動作しない

set selection=exclusive が設定されている場合、<Plug>(operator-surround-delete) で括弧を削除しようとすると

function operator#surround#delete..<SNR>448_delete_surround の処理中にエラーが検出されました:
行   37:
no block matches to the region

とエラーになります。
selection の値が old または inclusive の場合は正しく動作します。

再現手順

" 設定
set selection=exclusive
map <silent>sd <Plug>(operator-surround-delete)
o の上にカーソルがある状態で sdiW
(homu)

あと本件とは直接関係ないのですが上記のようにプラグインからメッセージを出力する場合、『~の処理中にエラーが検出されました:行 37:』というメッセージはない方がよいと思います。
(コマンドラインの高さによっては『続けるには ENTERを押すかコマンドラインを入力して下さい』とメッセージが表示されるため。

Is this still maintained?

I've found vim-surround really buggy, and I've tired this. But I'm curious about the status of the plugin, can anyone tell me? Thanks a lot!

surround append with <backslash> procduces regex error

Let the buffer be

word

Bug:

<operator-surround-append>iw\ gives

Error detected while processing function operator#surround#append..<SNR>345_get_block_from_input..<SNR>345_get_block_or_prefix_match..<SNR>345_get_block_or_prefix_match_in_filetype:
line    6:
E114: Missing quote: "^\\V\"

Expected:

\word\

Decrements counter?

<?php
    $sponsor_info  = array(
        'na|me'    => $promoter_info[0]->post_title,
        'content' => $promoter_info[0]->post_content,
    );

cursor at the |, surround replace around ' with " (switch to double quote) causes the [0] to decrement (like C-x) (using recommended mapping and keys: sra'"

Space is trimmed when replacing delimiter of string literal contains trailing space

When cursor is on 'f',

puts 'foo '

<Plug>(operator-surround-replace)a'" will change above code to:

puts "foo" 

Note that the line has one trailing space at the end of line. So 'foo ' is modified to "foo" .

This occurs because replacing is implemented as a combination of deleting and appending a surround. It deletes the surrounding ' at first(foo ), and then appends surrounding " next("foo" ). When appending the surround, the space is not counted as surrounding and only 'foo' is considered as target.

textobjectがスペースに隣接している場合<Plug>(operator-surround-replace)を使うとsurroundにスペースが含まれてしまう

例えば次のような場合を考えます。

function('foo', 'bar')

'bar' を "bar" に置き換えるために (operator-surround-replace)a'" と入力すると

function('foo', "bar")

となることを期待するのですが、

function('foo'," bar")

となります。すなわち bar の直前にスペースが含められています。回避する方法はないでしょうか?

ヤンクやペーストする機能がほしい

これも『いますぐほしい!』というわけではないですが、忘れないうちに Issues に投げておきます。
#5 と少し関連していますが、func(foo)func() をヤンクしたり func() がヤンクレジスタに保持されている場合、func({textobj}) の用に textobj をいい感じに囲むようにペースとされる機能がほしいです。

escaping for $ not working

For example, when using targets.vim (or user plug-ins providing a i/a$ text-object) with

let g:targets_quotes = '$d'

and using

let g:operator#surround#blocks =\ {
\ '-' : [
\ { 'block' : ['(', ')'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['(', ')', 'b'] },
\ { 'block' : ['{', '}'], 'motionwise' : ['char', 'line', 'block'], 'keys' : ['{', '}', 'B'] },
\ { 'block' : ["$", "$"], 'motionwise' : ['char','line','block'], 'keys' : ['d'] },
\ ]\ }

then hitting <Plug>(operator-surround-delete)aB when the cursor is between \alpha and \beta on

Yes ${\alpha_\beta{Un^{-1}}^{m}}$ No

produces

Error detected while processing function operator#surround#replace..operator#surround#delete..<SNR>294_delete_surround:
line   31:
E54: Unmatched \(

Option to retain cursor position

How about plugin option to enclose mapped actions inside

old = getcurpos()
...
setpos('.', old+(append ? len(newleftblock) : delete ? -len(oldleftblock) : replace ? len(new)-len(old) : 0))

I understand, this feature will be greatly broken for some more-then-one letter blocks and even for some usecases of one-letter (like multiline append). But nevertheless it would be nice to keep position in much more often usecases for ({["'<.

ファイルタイプ毎の設定にデフォルト設定をマージしたい

デフォルトの設定に少しだけ追加したい設定がある場合に、- の場合には、g:no_default_blocks によりマージされると思うのですが、

  • 特定のファイルタイプにデフォルトの設定 + αしたい場合
  • もしくは、- の設定に+αしたい場合

に、最小限の設定(αのみの追加)でできるようにならないでしょうか?

g:operator#surround#blocks['-'] 未定義時の動作

以下のエラーになりました。

function operator#surround#delete..<SNR>262_delete_surround..<SNR>262_get_surround_in..<SNR>262_get_surround_in_with_filetype の処理中にエラーが検出されました:  
行    1:
E716: 辞書型にキーが存在しません: -

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.