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?
If I've understood your question correctly, you can do that with the following keys (example with OS X keybindings):
Ctrl + Shift + Up or Ctrl + Shift + Down to select a column in multiple lines.
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
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.
You can also get the same result by the following steps:
select lines by Shift + Up/Down
split selection into lines (of selections): Cmd + Shift + L
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()))
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.
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.
Related
I would like to select all the words at once between the backtick. In MACOSX use alt+ shift and the cursor put a multi cursor at the beginning of the words but I can't stop the selection to the "`" character where the word ends.
`apples` int(200) NOT NULL,
`bananas` int(100) NOT NULL,
`mangos` int(100) NOT NULL,
`kiwi` int(100) NOT NULL,
`raspberry` int(100) NOT NULL,
Is there a way I can select until some character is found in the line where the cursor is placed?
In this case, the expected selection is:
`apples`
`bananas`
`mangos`
`kiwi`
`raspberry`
Press command+F to open the find widget.
Type `.*?` in regex mode (.* icon).
Press command+shift+L to select all occurrences.
Press esc to close the find widget.
If there is a dedicate way to select until a specific character, I am not aware of it.
However, you have a very regular "input" here (none of the fruit words have spaces in them). If that's representative of your real scenario, then to select apples, bananas, mangos, kiwi, raspberry, then put the caret right at the beginning of "`apple`", then hold alt+shift and click the beginning of "raspberry", then press ctrl+shift+right, then shift+right (Windows and Ubuntu. I think for MacOS it's option+shift+right then shift+right, but I'm not 100% sure).
This doesn't work if the fruit words have spaces in them. In that case, if you happen to be doing this because you want to do find and replace, you can use the regular expression mode of the find and replace feature. Something like find ^ `([^`]+)` and then replace withsome usage of $1.
You can use the extension Select By
Create a keybinding:
{
"key": "shift+alt+]", // or any other combo
"command": "selectby.regex",
"when": "editorTextFocus",
"args": {
"forward": "('''|\"\"\"|'|\"|`)",
"forwardNext": "{{1}}",
"forwardInclude": false,
"forwardNextInclude": false
}
}
Place multiple cursors before the strings you want to select, and press the key binding.
If you do this selection frequently you can make a keybinding for it using this extension: Find and Transform (which I wrote). Sample keybinding - in your keybindings.json:
{
"key": "alt+d", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=`)(.*)(?=`)",
"restrictFind": "selections",
"isRegex": true
},
}
For this you just need to select the lines you want changed - it can be one selection, see demo. Or you could use this option:
"restrictFind": "line",
and just put a cursor on any lines you want changed - the cursor can go anywhere on the line.
By "line":
I want to save my current cursor position, move to some other line (in the same file), do some editing and jump back to the original cursor position to continue coding.
Lets say i am in line 50 and realise i need to add a header/library file at line 5. I want save my position here so that i can jump back after making my changes at line 5. How can i do this?
I have looked into cursor navigation shortcuts but they all move cursor by tracing every position cursor was in, and its time consuming and confusing, instead i want to jump back to saved position in one shot.
You could use the selection anchor. There is no default keybinding for "Go to Selection Anchor" so you'd have to add one yourself.
There is no built-in command for that. The Go Back / Go Forward navigation commands doesn't allows you to save specific locations.
Instead, you should rely on extensions, like my Bookmarks extensions.
There is a bunch of other similar extensions, as you can see on this search https://marketplace.visualstudio.com/search?term=bookmarks&target=VSCode&category=All%20categories&sortBy=Relevance. You should take a look at some, and try out the one that better fit our needs.
Hope this helps
Thanks #scottfrazer for the suggesting selection anchor.
Thanks #alefragnani for suggesting extensions.
Based on these answers, i wrote a keybinding using multi command extension,
After installing the multi command extension,
Add the following snippets in the keybindings.json,
[
{
"key": "ctrl+alt+`",
"command": "extension.multiCommand.execute",
"when": "!selectionAnchorSet",
"args": {
"sequence": [
"editor.action.setSelectionAnchor",
]
}
},
{
"key": "ctrl+alt+`",
"command": "extension.multiCommand.execute",
"when": "selectionAnchorSet",
"args": {
"sequence": [
"editor.action.goToSelectionAnchor",
"editor.action.cancelSelectionAnchor",
]
}
},
]
Above snippet will create a key binding for "ctrl+alt+`" (Ctrl + Alt + BackTick).
When pressed,
Creates a selection anchor at cursor position
If an anchor already exists, Move to that cursor position and deletes that anchor at that position.
Say for example I have a very long line of text in a single line in VS Code (let's pretend that the example given below is very long).
0xffffffffeeeeeeee02020202aaaaaaaa
At first I placed my cursor after the characters 0x.
(the cursor is denoted by the | character in the example below)
0x|ffffffffeeeeeeee02020202aaaaaaaa
Then I want to add more cursors after every N characters from the current cursor. In this case N is equal to 8 and I want to do this twice to add two more cursor like in the example below.
0x|ffffffff|eeeeeeee|02020202aaaaaaaa
So that after I press the following sequence of keys in the keyboard, in this case those sequence of keys are ,(space)0x I should be able to get these final result.
0x, 0x|ffffffff, 0x|eeeeeeee, 0x|02020202aaaaaaaa
After I deselect the cursors I should be getting this
0x, 0xffffffff, 0xeeeeeeee, 0x02020202aaaaaaaa
Is this possible to do in VS code?
There is a straightforward regex that can do what you want:
Find: ^0x|(.{8})(?!$)
But you have to enable the Find in Selection option and trigger the Select All Matches command yourself after entering it.
Or use a macro extension like multi-command and this keybinding to automate it:
{
"key": "alt+p",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.actions.findWithArgs",
"args": {
"findInSelection": true,
"isRegex": true,
"searchString": "^0x|.{8}",
}
},
"editor.action.selectAllMatches",
"cursorRight"
]
},
}
You must select up to where you want the last cursor and then trigger the macro.
Because of a flaky implementation, you must start with the Find in Selection option disabled in the Find Widget. I haven't found a way around that.
The setting Editor > Find: Seed Search String From Selection must be set to never. Otherwise your selected text will over-ride the searchString from the macro above.
Here is the pure regex method with no extensions:
Enter ^0x|(.{8})(?!$) in your Find Widget with the regex option enabled.
^0x the first part of the string you ultimately want a cursor after.
(.{8})(?!$) select each 8-character block, but not the last - that is why there is a negative lookahead for the end of the line (?!$) - so the last 8 characters are not matched. Don't worry, there will be a cursor in front of those last 8 characters as you want. (.{8}) doesn't actually need to be in a capture group, it is just clearer to see.
Select all the text to match: 0xffffffffeeeeeeee. Stop the selection there - wherever you want the last cursor.
Enable the Find in Selection option in the Find Widget by Alt+L.
Alt+Enter to select all the find matches respecting the Find in Selection option: editor.action.selectHighlights.
Step (4) will select your matches - you should have 4 for the above string. But you don't want the matches selected you just want a cursor at the beginning of each, so do step (5):
Right arrow: this cancels each selection with a cursor at the right end of each.
Type.
You can use the extension Select By
It has a command to add a new cursor by keyboard
{
"key": "ctrl+i ctrl+alt+right", // or any other key combo
"when": "editorTextFocus",
"command": "selectby.addNewSelection",
"args": {"offset": 8}
}
Now the offset is hard coded but I will add an option to ask the user the offset.
The possibility of the context switch is already working. I have to update the README.
I have turned on Word Wrap in VSCode, so that long lines are wrapped at certain window widths.
If I want to comment out some code or text, I usually just move the cursor to the beginning of the first line of the block of code, use ctrl+shift+down to add cursors, and type the // or # etc..
The problem with Word Wrap is that I not only get cursors in the beginning of each actual line, but in the beginning of every line as it is displayed at the moment in the editor.
For example, for the following document:
1 This is the first line.
2 Second line.
It might be wrapped like this:
1 This is the first
line.
2 Second line.
So if I use the method above and add % I would get:
1 %This is the first
%line.
2 %Second line.
But what I want is the following:
1 %This is the first
line.
2 %Second line.
Otherwise, I have a % in the middle of the line, just because I resized the editor window.
At the moment, I actually turn off Word Wrap to achieve this, but I hope there is a better way?
Follow the next steps:
Alt+Shift+i to put a cursor on every selected line
Cmd/Ctrl+← to move all cursors to the beginning of the [wrapped] lines
Cmd/Ctrl+← (again) to move all cursors to the actual beginning of the [wrapped] lines
Enjoy! 🙂
Update v1.62+ (October 2021)
This is now natively available in VSCode thanks to this PR on changing the default behavior of InsertCursorAbove/Below. However, it's not enabled by default.
You'll need to update your keybindings to pass in an args like this:
{
"key": "shift+alt+down",
"command": "editor.action.insertCursorBelow",
"when": "textInputFocus",
"args": { "logicalLine": true },
},
{
"key": "shift+alt+up",
"command": "editor.action.insertCursorAbove",
"when": "textInputFocus",
"args": { "logicalLine": true },
},
This appears to be a known issue. The CursorColumnSelectOnTrueLines extension by Martin Zimmermann provides commands that show the desired behavior, by default mapped to Ctrl+Alt+Shift+Up/Down.
I can't find any way to surround a selection with something in VS Code.
For example doing something like that : text => "text" just by selecting the word text and typing key "
Another example with the following text :
mon
tue
wed
thu
fri
sat
sun
By selecting all of theses words :
mon|
tue|
wed|
thu|
fri|
sat|
sun|
and typing " I would like to perform something like this :
"mon"
"tue"
"wed"
"thu"
"fri"
"sat"
"sun"
Selecting some text and pressing " already works in VSCode to surround a single item, and works for multi-line selections as well.
NOTE: this is language dependent. The language syntax must define opening and closing braces, e.g. quotes, braces, etc. So this will not work in a "plaintext" file, for example. Change your language mode with CTRL+SHIFT+P and type Change Language Mode ENTER and select something like JavaScript where this is supported.
What you are after though is not really that efficient like that. Your best bet is to use multi-cursors.
Place the cursor at the start of the first line, press CTRL+ALT+DOWN to add another cursor below on the next line. Keep doing that until you have a cursor in front of all your words.
Then just type " then END then " and all your lines are surrounded by quotes.
NB: To check if you have a key bound, and what it is, you can always press CTRL+SHIFT+P and type Add Cursor Below and if there's a keybinding it will show to the right of that text.
In VS Code hold Command + Shift + P
then write:
"> Preferences: Open Keyboard Shortcuts (JSON)"
In this area that you are allowed to modify, paste this inside the brackets:
{
"key": "ctrl+p",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
}
** note that in this example the key is set to Ctrl + p, you can change the key to whatever you prefer
Maybe you can try this extension, you can write your own custom wrappers:
https://marketplace.visualstudio.com/items?itemName=yatki.vscode-surround
A simple yet powerful extension to add wrapper templates around your code blocks.
Features
Supports multi selections
Fully customizable
Custom wrapper functions
You can assign shortcuts for each wrapper function separately
Nicely formated
Demo 1: Choosing wrapper function from quick pick menu
Demo 2: Wrapping multi selections
Using Yuri Aps' suggestion, I added the following JSON to keybindings.json. This provides the functionality Ronan Lamour requested for any file type, and without requiring an extension. It works for either single or multiple selections when using either single or double quotes. Coming from Sublime, this is helpful since it reproduces functionality Sublime provides natively.
{
"key": "'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "'${TM_SELECTED_TEXT}'"
}
},
{
"key": "shift+'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
},
I was coming from (neo)vim switching to VS Code, and was using Tim Pope's wonderful "vim-surround" plugin for vim before. I found a port of that plugin for VS Code. It's very useful, and incredibly efficient once you learn the shortcuts, in my opinion!
Links:
Original plugin by Tim Pope for vim
Port of plugin to VS Code
If you use vim or vim bindings in VS Code, please enjoy!
Edit: the VSCodeVim plugin includes the surround functionality automatically, so if you have that plugin installed, you don't really need the vscode-surround plugin.
Update 15-02-2022:
VS Code has introduced Surround with snippets for JS/TS natively.
It may not be totally related with the question but it may help someone who landed in this question with the intent of "surround with" in vs code.
This extension also exists if you want custom surround with text.
https://marketplace.visualstudio.com/items?itemName=sifue.surrounding.
I just installed it and got it working perfectly
Select the word you want to surround it with and enter Ctrl + Alt + T. Then just key in whatever key you want to surround it with.
A more generic solution: in keybindings.json:
{
"key": "alt+m",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$1${TM_SELECTED_TEXT}$1$0"
}
}
Whatever you type after triggering the keybinding will be added to both ends of all selections.
Just tab to the end of the word(s) when you are done and, if you had multiple cursors Esc to remove extra cursors leaving just one.
Since GitHub supports math in Markdown now, I need to wrap my formulas with dollar signs:
$E = mc^2$
When I select a formula and press dollar sign $ on my keyboard I get my formula wrapped automatically. Here is one way to achieve it:
Open Keyboard Shortcuts menu:
Press on Open Keyboards Shortcuts (JSON) button:
In shortucts.json file, which opens, paste this snippet:
{
"key": "shift+4",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$${TM_SELECTED_TEXT}$"
}
}