Steven Lu
Steven Lu

Reputation: 43467

Sublime Text: Make cursor stay put upon pasting?

For example:

<div>somestuff</div>
<div>somemorestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>

Want to assign class="abc" to each one.

Now I realize there is the Ctrl+Alt+Arrows to "carve" out a vertical path of cursors (neat trick!) after which point I can paste in my class, but that won't work if my div's are separated by some lines.

So I'd like to paste:

<div class="abc">somestuff</div>
<div>somemorestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>
<div>somestuff</div>

But it puts my cursor HERE

<div class="abc"|>somestuff</div>

I'd like for it to stay here

<div| class="abc">somestuff</div>

so that I can just repeat Ctrl+V

Upvotes: 2

Views: 449

Answers (3)

anonymous coward
anonymous coward

Reputation: 12824

I like the plugin solution, and think that there's one more solution worth mentioning, especially since the question specifically calls for the simplicity of just hitting "down, ctrl+v" repeatedly...

Put your cursor where you want to enter the text, and use shift+ctrl+down to "multiply" your cursor points onto as many lines as necessary. Type what you want, and hit esc to return to a single point cursor.

Similar to @derek_duncan's answer, but with more granular control, assuming the text you're editing is lined up as in your example.

Upvotes: 0

Riccardo Marotti
Riccardo Marotti

Reputation: 20348

I think that the best way to achieve what you need is to use command split selection into lines, Ctrl+Shift+L (Cmd+Shift+L on Mac).

  1. Select all the lines that you need to edit
  2. Press Ctrl+Shift+L
  3. Move the cursors (with arrow keys) in the position where you want to paste your code
  4. Paste your code.

If you really want the the cursor to stay at the beginning of the pasted text, you can create a plug-in. Tools/New Plugin...:

import sublime_plugin


class PasteAndResetCursorPositionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        start_region = self.view.sel()[0]
        self.view.window().run_command("paste")
        self.view.sel().clear()
        self.view.sel().add(start_region)

Save this in your Packages/User directory.

Then add the key binding to your Key Bindings - User:

{ "keys": ["ctrl+alt+shift+v"], "command": "paste_and_reset_cursor_position" }

Of course you can use the shortcut that you prefer.

Upvotes: 3

derek_duncan
derek_duncan

Reputation: 1377

What I would do would be to select <div, then do CTRL+D to select each identical text. Then you can move your cursor on every one accordingly.

Upvotes: 3

Related Questions