Code Monkey home page Code Monkey logo

vim-exchange's Introduction

exchange.vim

Easy text exchange operator for Vim.

Mappings

cx

On the first use, define the first {motion} to exchange. On the second use, define the second {motion} and perform the exchange.

cxx

Like cx, but use the current line.

X

Like cx, but for Visual mode.

cxc

Clear any {motion} pending for exchange.

Some notes

  • If you're using the same motion again (e.g. exchanging two words using cxiw), you can use . the second time.
  • If one region is fully contained within the other, it will replace the containing region.

Example

To exchange two words, place your cursor on the first word and type cxiw. Then move to the second word and type cxiw again. Note: the {motion} used in the first and second use of cx don't have to be the same.

More

Check out these other resources for more information:

Other text editors

Exchange.vim has been ported by third parties to work with Vim emulators in other text editors.

Troubleshooting

More details and troubleshooting can be found in the Wiki.

Installation

If you don't have a preferred installation method, I recommend installing pathogen.vim, and then simply copy and paste:

cd ~/.vim/bundle
git clone git://github.com/tommcdo/vim-exchange.git

Once help tags have been generated, you can view the manual with :help exchange.

vim-exchange's People

Contributors

alexpl292 avatar bps avatar cohama avatar colinkennedy avatar habamax avatar inkarkat avatar jchros avatar jebaum avatar michamos avatar okapia avatar raimondi avatar tommcdo avatar wellle avatar wilywampa 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  avatar  avatar  avatar  avatar  avatar  avatar

vim-exchange's Issues

Request: Swapping text across different buffers

Right now, It appears as though the last area marked for swapping is local to the window, or possibly the buffer. Seems more useful to me to just have one global concept of swapping text.

'.' Doesn't Repeat Custom Text Objects Before Version 7.4

Hello! First off, great plugin. I don't often need to use it but when I do it's really nice to have. Get's me another step closer to having my editor do exactly what I'm thinking. So thanks!

The Issue

I think the title says it all. On vim before version 7.4, running the exchange mapping on a custom text object doesn't repeat correctly. The problem is that prior to version 7.4, using an operator with a custom text object was not automatically repeatable. To make a text-object repeatable, you needed to do some extra work utilizing repeat.vim. See https://github.com/wellle/targets.vim/blob/master/autoload/targets.vim#L498-L507 for an example. Since your code calls repeat#invalidate() it prevents this from working. I know the repeat#invalidate() code was put in for a reason in PR #32 but I think we can add support for versions prior to 7.4 and keep the existing functionality. I have two possible solutions to propose. If you or @wellle or @wilywampa could take a look (since you were all involved in that PR) that would be much appreciated.

Solution 1

https://github.com/tommcdo/vim-exchange/compare/tommcdo:master...lag13:improve-repeat-compatibility1?expand=1

Solution 2

https://github.com/tommcdo/vim-exchange/compare/tommcdo:master...lag13:improve-repeat-compatibility2?expand=1

How/Why This works

To make custom text objects repeatable, vim-repeat registers a one-time CursorMoved handler which sets g:repeat_tick equal to b:changedtick. Apparently this works because the CursorMoved event will only trigger after the operator + operator-pending mappings have completed. The reason your operator won't repeat correctly with a custom text object is because calling repeat#invalidate() will clear the CursorMoved autocommand before it gets a chance to do it's thing. So my solutions work because they make b:changedtick and g:repeat_tick differ (which wipes out a previously used repeat#set() call) but they leave the CursorMoved autocommand (which will make the text object repeatble). Kudos to guns for finding out how to make custom text objects repeatable in the first place tpope/vim-repeat#8.

Odd behavior with cxiw

Hi, I recently cloned your exchange plugin, but I am seeing some odd behavior.

I have the sentence:

Pass the salt and pepper.

I do cxiw on the word "salt" and then go to "pepper" and repeat cxiw (or . ) and I get:

Pass the peppert and saltr.

It does it with every word exchange and always has funny ending. I checked my mappings and their doesn't seem to be any conflicts. Any idea?

Alain

On the
capture1
capture2

Clipboard error happens when used in NeoVim in WIN10

I used this plugin in NeoVim, when I did the exchange via cxiw, I got the error information shown below:

clipboard: error invoking win32yank.exe: thread 'main' panicked at 'called Result::unwrap() on an Err value: WinAPI Error(1418)', src\libcore\result.rs:859 note: Run with RUST_BACKTRACE=1 for a backtrace.

I tried to add one line in the source code, or comment one call s:restore_reg(), then, the error is gone.

    51 call s:restore_reg('*', reg_star)
    52 sleep 100m
    53 call s:restore_reg('+', reg_plus)

In another hand, I try the same action in VIM, there is no error happened.

cxx highlights two lines (ideavim)

"cxx" (exchange line) works functionally, but as you can see it in the gif below, it for some reason highlights two lines (although it only changes the above most). This is surely a bug.

Take a look: gif

Work around Vim bug fixed in patch 7.3.411

I was curious as to why exchange silently failed under the system version of Vim on OS X 10.9.1, when it worked fine under MacVim snapshot 72. I dug into it and found that the default register (") was being overwritten upon paste with the visually-selected text — which meant that the exchange was effectively a noop.

OS X 10.9.1 ships with Vim 7.3 (and no patches), whereas MacVim snapshot 72 is Vim 7.4 and patches 1–52. I searched the changelog and found patch 7.3.411:

Pasting in Visual mode using the "" register does not work.

Pasting from register 0 works around the bug:

diff --git a/plugin/exchange.vim b/plugin/exchange.vim
index 0f2902d..7effdb1 100644
--- a/plugin/exchange.vim
+++ b/plugin/exchange.vim
@@ -7,12 +7,12 @@ function! s:exchange(x, y, reverse)
        call setpos("'a", a:y[2])
        call setpos("'b", a:y[3])
        call setreg('"', a:x[0], a:x[1])
-       silent exe "normal! `a" . a:y[1] . "`b\"\"p"
+       silent exe "normal! `a" . a:y[1] . "`b\"0p"

        call setpos("'a", a:x[2])
        call setpos("'b", a:x[3])
        call setreg('"', a:y[0], a:y[1])
-       silent exe "normal! `a" . a:x[1] . "`b\"\"p"
+       silent exe "normal! `a" . a:x[1] . "`b\"0p"

        if a:reverse
                call cursor(a:x[2][1], a:x[2][2])

It would also work to use a register other than the default, saving it up top and restoring it before returning. I can submit a pull request if you'd like — if so let me know which way you prefer.

Idea: Extend exchange.vim by bubbling commands

I like the bubbling concept of a single line or serval lines see vimcast #26. Plugins offering such feature are unimpaired.vim (line/lines), matze/vim-move (line/lines), frace/vim-bubbles and sideways.vim (function arguments & other). I could imagine that exchange.vim could also have a bubbling feature. I think of following workflow:

Step 1: Invoke exchange.vim normally

cx{MOTION}

Step 2: Invoke bubbling feature, e.g. with

[e or ]e

to bubble/exchange the marked object with the equivalent previous or next object. The equivalent object must be defined for the used motion. This does not have to exist for all motions/objects. For instance, for the visual mode mapping X it can't be known what an equivalent next/prevous object would be.

In a first step I would like to see it working for cxiw, cxiW, cxis, cxip (i.e. words/WORDS, sentences, and paragraphs).

Step 3:
After that it would also be useful to proceed with the dot command.

Mappings are stolen from unimpaired.vim:

[e - bubble over previous line
]e - bubble over next line

The mappings have a good mnemonic: e for exchange.

This clash of mappings could be resolved: If cx is not active, invoke previous mapped commands.

An implementation could look like this:

  1. Create a dictionary assigning to objects a sensible motion to the previous and next equivalent object, e.g. dict['ip'] = {'{', '}'}. This can be extended if needed and is possible/makes sense.
  2. query existing mappings of [e, and ]e and store them
  3. re-map [e, ]e to call funcion bubble_exchange() and make them repeatable
  4. The definition of the function bubble_exchange could look somehow like this:
     function bubble_exchange()
      if "motion in dictionary and cx active":
          if "[e"
            "do motion in backward direction and invoke dot command"
           if "]e"
            "do motion in forward direction and invoke dot command"
      else 
               "invoke saved mappings accordingly (]e or [e)"
     endfunction

Transpose the last 2 words

What would be a reliable mapping to transpose the last 2 words, something that works consistently even if words are only one character long?

Sluggish 'c' command Visual mode

When performing and command such as viwc there is almost a full second delay.

:vmap c

outputs:

v  cx                     <Plug>Exchange

I'm not sure what else to do to troubleshoot this sluggishness.
Perhaps you know more?

How to Reset

How do I reset? I tried uninstalling, deleting all the supporting files, and reinstalling, and no luck.
I mess up the setting, and now I can't change any setting.

TextObjectify interferes with repeating cxi/a commands

Hi,

Great plugin, I just found it thanks to Drew's vimcast(http://vimcasts.org/episodes/swapping-two-regions-of-text-with-exchange-vim/).

I have also the TextObjectify(https://github.com/paradigm/TextObjectify) plugin installed which makes some word objects more logical too work with as well as allows you to define some yourself.

The sad thing is when i try to repeat something like cxi" with . command it doesn't happen. I just see this in the status line and nothing happens:
exchange

Any possibility of fixing that?

allow count for cxx

How about allowing counts for cxx ? For example, the following workaround over here seems to achieve just that:

onoremap <SID>(underline) _
nmap <expr> cxx '<Plug>(Exchange)' . v:count1 . '<SID>(underline)'

support repeating swap operations

I often use this plugin to swap function arguments. When doing this I often have to repeat the same operation in various call sites. Unfortunately this plugin doesn't play nicely with vim's repeat operatiorn '.'.

It would be nice to add support for repeat, perhaps by leveraging https://github.com/tpope/vim-repeat

Option for disabling highlighting

Occasionally, I don't like to see the highlighting which in general is very helpful.
For instance, if you are working with someone together and don't want to distract
the other person by visual highlightings in your editor.

Using 'cx' {motion} followed by 'cxx' has strange behavior

This seems like an edge case, but using cx {motion} , followed by cxx doesn't seem to work as expected.

Example:

1 word
2 this is a sentence
3
4 

Use cxw on line 1, and then use cxx on line 2, will produce:

1 
2 this is a sentence
3 
4 word

'cxx' / 'cX' incorrectly mapped to Visual mode and not Visual-Line mode?

Checking my :vmap I see that cxx and cX is mapped to Visual mode.
I also see that it calls the <Plug>ExchangeLine function

I feel as though this type of Exchange would be more appropriate for 'Visual-Line' (V-Line) Mode since it deals with exchanging with lines of code.

If I recall correctly (I could be wrong) but this Visual-Line mode mapping uses lmap

As a side note. I had to remap cxx and cX in order to avoid the sluggish 'c' issue shown here - #11

These were the mappings I used.

" Re-Map the visual exchange capability in order to avoid a 'slugish'
" 'c' command in visual mode
" https://github.com/tommcdo/vim-exchange/issues/11
vmap <leader>cx <Plug>Exchange
vmap <leader>cxx <Plug>ExchangeLine
vmap <leader>cX <Plug>ExchangeLine

I never knew about the cxx / cX capability prior to this so unfortunately I'm not sure how well these commands perform since I've remapped them.

Please let me know what your thoughts on this are.

Detect EOL visual block selections

When a visual block is created using $, it may result in a selection where the rightmost corners are not the rightmost edge of the visual block. Example:

hello
goodbye
hi

Since blocks are reproduced using '< and '>, the longer text in the middle lines may not be covered.

Broken if exchanging in the wrong order

If pasting the first set text over the second set text causes the first text's position in the buffer to change (line or column), things will be messed up.

Potential solution: Choose the text that appears later in the file as the second set text.

Space isn't preserved when swapping words

Hi, Thanks for the great plugin!

I have a little issue getting it to work properly. Here is what I did in a clean document:

|test1 test2

The cursor is represented by "|". I typed cxw, pressed w to move to the second word and pressed cxw again. This is what I got as a result:

test|2test1_

I would have expected something like this:

test2 |test1

The example in the help doc isn't working

I love the idea for the plugin, but I'm having a hard time getting it to work. I'm attempting to do this example found in :help exchange:

In the text below, we will exchange everything inside the parentheses with
everything inside the double quotes.

    Tom "a Vim plugin developer" McDonald (The Dev)

First, place your cursor somewhere inside the quoted string and type `cxi"`.
Then move your cursor to somewhere inside the parentheses and type `cxi)`. The
text will the be changed to:

    Tom "The Dev" McDonald (a Vim plugin developer)

It should be noted that I don't actually go by that nickname.

But it isn't working for me. Here are my exact keystrokes, starting from the command line:

vim<CR>:help exchange<CR>/plugin dev<CR>cxi"fDcxi)

Result: Exchange aborted: overlapping text

Add option to set custom colors

Is it possible to expose some kind of variable for setting custom color or is there any other way to do that?

I am not familiar with vimL, so sorry if this comes as a stupid question.

Thanks

Doesn't work with unnamed or unnamedplus clipboard

If you have set clipboard=unnamedplus or set clipboard=unnamed in your vimrc, the plugin doesn't work properly.

trying to swap the arguments in something like this:
(int thing1, int thing2)
with cxt, and cxt) results in

(int thing2, int thing2)

support to keep cursor as it was after calling <plug>(Exchange)

context:

  • have a map: vmap X <Plug>(Exchange)
  • key sequence in vim: viwX

the problem is:

  • after viw the cursor stays at word end
  • after X the cursor jump to the word start

So I changed map to vmap X <Plug>(Exchange)E (appended a E), but sometimes the selection is **==not a word.

The cursor jumping is not really matter too much, but when I noticed it, It becoming annoying more and more ...

Don't require +xterm_clipboard option

Hey,

I have the following error with vim-exchange with Vim on a server (not compiled with +xterm_clipboard):

Error detected while processing function <SNR>96_exchange_set..<SNR>96_exchange_get:
line   34:
E354: Invalid register name: '*'

I quickly fixed it by removing all references to * and + registers, but there should maybe be a condition before using these registers?

Highlight the first defined exchange region

On the first call to exchange, highlight the text that has been set. Use matchadd() and the \%23l / \%23c zero-width anchors to highlight parts of specific lines. Multiple calls to matchadd() will be needed for multi-line selections.

Highlighting is incorrect for the wrapped lines if linebreak is set

Given an example with a long line:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim

When I have set linebreak and do /inci<cr>cxiw, I see this (exchanging itself acts correctly, though):

scr
I'm on neovim 0.1.6

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.