alexy2k
alexy2k

Reputation: 560

Sublime text 2 - column select till the end of the line

Is there any way to select column with keyboard shortcut and expand selection till the end of each line?

Currently, when cursor reaches the end of the line it jumps to the beginning of the next one.

How can I avoid this behavior without using mouse?

Upvotes: 21

Views: 37090

Answers (7)

Fkiran
Fkiran

Reputation: 89

You can achieve it by removing the spaces at the end of each line. Workflow in Sublime is:

  1. option+command+F which is find and replace
  2. Activate Regular Expressions with option+command+R

Decide how many spaces from the end need to be deleted. Let's say at least 4 spaces,

  • then find * and replace it nothing.

Before before replace

After after replace

Upvotes: 0

Vivek Gani
Vivek Gani

Reputation: 1313

For sublime text 2 - consider the answers above such as Bart & Robert's solution

For sublime text 4 (and likely 3) - you can create custom keybindings that differentiate between moving and selecting without any extra functions. You just need to set the extends option appropriately. For example, here's my Default (linux).sublime-keymap file:

[
    {"keys": ["super+left"], "command": "move_to", "args": {"to": "bol", "extend": false}},
    {"keys": ["super+right"], "command": "move_to", "args": {"to": "eol", "extend": false}},
    {"keys": ["super+shift+left"], "command": "move_to", "args": {"to": "bol", "extend": true}},
    {"keys": ["super+shift+right"], "command": "move_to", "args": {"to": "eol", "extend": true}}
]

Note how the only difference above is in how I set extends: false for moving only, but for moving and selecting I set extends: true. If you want to support both moving and selecting, from my experience so far you need to have both custom lines in there.

Upvotes: 1

Armfoot
Armfoot

Reputation: 4921

I came to this answer because I was searching how to place the cursor in all the lines until EOF (end of file) without using ctrl+alt+/ (not pratical for more than a few dozens of lines), so I could trim or select a specific part of those lines.

So I eventually ended up in the sublime text documentation where I found:

  • ctrl+shift+L which will place cursors in all the lines selected and at the end of them (EOL):
    • select those lines with ctrl+L (or ctrl+shift+End to select until EOF);
    • press ctrl+shift+L to add cursors at EOLs;
    • now you can move all the cursors simultaneously by words with ctrl+/ or to the BOLs/EOLs with Home/End), if you also press shift you will select while moving them;
  • but the most useful feature is definitely the middle click of the mouse + drag which selects the lines and simultaneously places cursors at the end of those selections:
    • BONUS: If you just want to place the cursors at EOLs (without selecting) click on the background (after the EOLs) and drag! (if the lines are too long you can use the minimap to position your view-screen at the longest line);
    • now you can move all the cursors simultaneously by words with ctrl+/ or to the BOLs with Home), if you also press shift you will select while moving them.

Upvotes: 10

Bart
Bart

Reputation: 1008

robertcollier4's answer solved the question for me. For some reason the super+shift+right default OSX keybinding is overwritten in Sublime Text 3, and there is no way to properly unbind it in the user-key-bindings.

To add robert's code as a plugin go to Tools > New Plugin, paste the code, save it and add a reference to it in your keymapping:

[
    { "keys": ["super+shift+right"], "command": "SelectToEndoflineCommand" }
]

The only change I made to it was to change

caretPos = self.view.sel()[0].begin()

to

caretPos = self.view.sel()[0].end()

for the EOL function, otherwise it won't work correctly for multi-line selections.

Upvotes: 2

robertcollier4
robertcollier4

Reputation: 752

import sublime, sublime_plugin

class SelectToEndoflineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        caretPos = self.view.sel()[0].begin()
        self.view.sel().add(sublime.Region(caretPos, self.view.line(caretPos).end()))

class SelectToBegoflineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        caretPos = self.view.sel()[0].begin()
        self.view.sel().add(sublime.Region(caretPos, self.view.line(caretPos).begin()))

Upvotes: 2

Jason
Jason

Reputation: 847

You can also get the same result by the following steps:

  1. select lines by Shift + Up/Down
  2. split selection into lines (of selections): Cmd + Shift + L

Upvotes: 3

José Luis
José Luis

Reputation: 1847

If I've understood your question correctly, you can do that with the following keys (example with OS X keybindings):

  1. Ctrl + Shift + Up or Ctrl + Shift + Down to select a column in multiple lines.
  2. Cmd + Shift + Right (Shift + End on other OS's) to extend the selection up to the end of each line.

The related keybindings for all OS's: http://www.sublimetext.com/docs/2/column_selection.html

Upvotes: 37

Related Questions