Sublime 3: Selecting text without line numbers (from find all results) - html

I did a find all in the project directory and I got the following result:
/home/yusuf/Downloads/concept/concept/css/style.css:
234 position: relative;
235 }
236: .scrolled .fh5co-nav {
237 background: #fff;
238 padding: 10px 0;
...
241 box-shadow: 0px 5px 7px -7px rgba(0, 0, 0, 0.15);
242 }
243: .scrolled .fh5co-nav a {
244 color: #000;
245 }
How do I select text without the line numbers? Is there a way to hide line numbers in search results? I want to select the code and paste it somewhere else.

The simplest solution is to open "Find&Replace" (⌘⌥F on OS X, CtrlH on Windows/Linux) and enter following options (RegEx mode, find ^\s+[0-9]+, replace empty)

There is no (documented) option to hide the line numbers in find in files results. In order to not copy the line numbers you would need to either carefully use multiple selection to copy all lines and skip the numbers, or use find and replace as Danill mentioned in his answer.
However with a bit of plugin code you can get the best of both worlds by having sublime do the heavy lifting for you.
For example, select Tools > Developer > New Plugin... from the menu and replace the contents of the buffer with the following python code, then save it as e.g. find_results_copy.py. This needs to be in your User package (name doesn't matter, only the extension does), but Sublime should take care of this automatically if you use the menu entry to create the stub plugin.
[edit]
Plugin code modified to use a single regex operation, which (due to a late night brain fart) I originally implemented via two operations instead.
[/edit]
import sublime
import sublime_plugin
import re
class FindResultsCopyCommand(sublime_plugin.ApplicationCommand):
def run(self):
sublime.active_window ().run_command ("copy")
sublime.set_clipboard (re.sub (r"^\s*[0-9]+.", "",
sublime.get_clipboard (), flags=re.MULTILINE))
This implements a new command named find_results_copy that first runs the default copy command, and then modifies the contents of the clipboard with a regular expression replacement to throw away the line numbers.
Now you can implement a custom key binding to invoke this command. Since we only want this command to trigger in find results, you can re-use the standard copy keyboard shortcut, modified to use our new command and with a context added that causes it to take effect in find results only.
This example uses the keyboard command for Windows/Linux; if you're on a Mac use super+c instead to map to the standard key for that platform.
{"keys": ["ctrl+c"], "command": "find_results_copy", "context":
[
{ "key": "selector",
"operator": "equal",
"operand": "text.find-in-files",
"match_all": true
},
]
},
Since this uses the default copy command, if you have copy_with_empty_selection turned on, this will copy the current line without a line number without your having to select anything, if you're used to working that way.
If desired, you can also duplicate this command (you can store it in the same file) and rename the class to FindResultsCutCommand and the command executed to cut (with an appropriate key binding) to also gain the ability to cut text and remove the line numbers, if you need that sort of thing as well.

In the search results double click either the resulting file path or any txt string to open the original file to copy from.

Related

How to change all numbers by a % in Visual Studio Code

Sorry I am still a beginner but slowly getting there. I want to change all the "base-purchase-prices" by a % all at once? I am tearing my hair out trying to work out how to do it. There are 7000 line items so simply saying "get a calculator" is not going to work
{
"tradeable-code": "Scissors_01",
"base-purchase-price": "110",
"base-sell-price": "12",
"delta-price": "-1.0",
"can-be-purchased":"default"
},
{
"tradeable-code": "Scissors_Plastic",
"base-purchase-price": "88",
"base-sell-price": "9",
"delta-price": "-1.0",
"can-be-purchased":"default"
},
You can use the extension Regex Text Generator
select all the base-purchase-price prices
perform a regex find with: (?<="base-purchase-price": ")([^"]*)
press Alt+Enter while focus in Find dialog
Execute command: Generate text based on Regular Expression (regex)
for Match Original Text Regular Expression enter: (.*)
for Generator Regular Expression enter: {{=N[1]*1.05:fixed(2):simplify}}
this increases the prices with 5% (1+0.05) and rounds it to 2 decimal digits
if you like the preview type Enter otherwise Esc
press Esc to leave Multi Cursor Mode
Using this extension: Find and Transform, full disclosure - written by me, it is easy to do math on any values in your file.
For example, make this keybinding, in your keybindings.json file (it could also be made into a setting that will become a command in the Command Palette if you want:
{
"key": "alt+c", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=base-purchase-price\":\\s*\")(\\d+)",
"isRegex": true,
"replace": [
"$${",
"return ($1 * 1.05).toFixed(2);",
"}$$"
]
}
The find will capture only the digits you want to change. The replace will run any math (or string) operation you want. Here I simply multiplied the capture group $1 by 1.05 and set it to 2 decimal places.

Is there a way to copy a single line of code but have it incrementally increase a number in said line of code for each paste?

I have a line of code I want to mass-paste into a file.
The line of code is "enchantment.level.x": "string". I want to paste this multiple times into my program, but each time I paste the line, I want x to increase by one.
So if line 2000 is "enchantment.level.1": "I", I would want line 2001 to be "enchantment.level.2": "I" (but I would change "I" to "II" manually).
Is there a way I can achieve this?
Thank you
I don't think there is any way to do what you want on each paste. But if you make all your pastes first then you can #rioV8's or my extension to accomplish the same goal.
The extension is Find and Transform that I wrote. It supports replacing with the match number (or index). Put this keybinding into your keybindings.json:
{
"key": "alt+m",
"command": "findInCurrentFile",
"args": {
"find": "(?<=enchantment\\.level\\.)x", // match the "x"
"replace": "${matchNumber}", // replace with the matchNumber
"isRegex": true
}
}
You could make that into a named setting too if you don't want to run it from the Command Palette.
${matchNumber} starts at 1. ${matchIndex} starts at 0.
You can use the extension Regex Text Generator to generate the new numbers.
Paste the line as often as you like
Select the number on all the lines with Multiple Cursors
use the extension and use (.*) for match regex and {{=i+1}} for generator expression

Build system for typescript using different flags

I want to use different flags (sourcemap, out, target) that the typescript compiler provides. I am trying to define a build system in sublime 2 but unable to do so.
Have already read this question.
basically i want to do something like the following
tsc src/main/ts/myModule.ts --out src/main/js/myModule.js --sourcemap --target ES5
Just add them to the cmd array
{
"cmd": ["tsc","$file", "--out", "src/main/js/myModule.js"],
"file_regex": "(.*\\.ts?)\\s\\(([0-9]+)\\,([0-9]+)\\)\\:\\s(...*?)$",
"selector": "source.ts",
"osx": {
"path": "/usr/local/bin:/opt/local/bin"
}
}
First of all let me say that I'm using Sublime Text 3 on Windows and Typescript 1.0.
I don't think that SublimeText2 is so much different, though...
If you're on similar conditions, take a look at my current configuration file:
{
"cmd": ["tsc", "$file"],
"file_regex": "(.*\\.ts?)\\s*\\(([0-9]+)\\,([0-9]+)\\)\\:\\s(.+?)$",
"selector": "source.ts",
"windows": {
"cmd": ["tsc.cmd", "$file", "--target", "ES5"]
}
}
Please notice that I tweaked the regex so that it matches the TSC error format (and brings you to the line containing the error when you double click it from the error log...)
Besides of that, I think that the real command-line which gets run is the lower one: as a matter of fact I had it working only placing the options down there... (in this specific case I'm asking an ES5 compilation type, your parameters will differ).
This suppose you have a tsc.cmd avaliable on path; if not, put the full path of tsc.cmd or tsc.exe instead of "tsc.cmd" and be sure to escape backslashes \ as \\...
This works in my situation, maybe in other contexts they should also be placed on the first line...
Hope this helps :)

Save panes layout generated by Origami in Sublime Text 2

Thanks to this great plugin : Origami
I am able to obtain the following layout :
How could I save this view to call it from the view/layout menu ?
You can get the layout data from the Auto Save Session.sublime-session file, under the "layout": key. This file is in standard JSON format, and can be opened in ST2 just fine (select View->Syntax->JavaScript->JSON for syntax highlighting if you want). For OSX, this file is (probably, I'm not in front of my Mac at the moment to verify) located in ~/Library/Application Support/Sublime Text 2/Settings - it should be in the same directory as the Packages/ folder where plugins etc. are stored.
So, to make a keyboard shortcut, set up your Origami layout, then perhaps move some files around, search for some text, anything to update the Auto Save Session.sublime-session file. It may already have updated after changing your layout, so check the time stamp to make sure. Then, open the file and search for layout. Copy the contents of the key - the "cells":, "cols": and "rows": keys inside the curly braces, as well as the curly braces themselves. Then, open Sublime Text 2->Preferences->Key Bindings-User and add the following to it (include the square brackets if you don't have anything in this file yet, omit them if you already do. If you already do, make sure to add a comma , after the last curly brace of the item before):
[
{
"keys": ["alt+shift+o"],
"command": "set_layout",
"args":
|
}
]
Set your cursor where I put the | character after "args": (make sure you delete the |) and paste in the contents of the "layout": key from Auto Save Session.sublime-session you copied earlier. Save the file, and you should now have a keyboard shortcut AltShiftO (O for Origami) that will restore your layout for you. If you have more than one layout you'd like to save, repeat the steps above, and just change the "keys": value to another key combination. If you have a lot of plugins, I highly recommend #skuroda's FindKeyConflicts plugin, which is available through Package Control under the same name. With it you can get a full list of all current key mappings, so if you're planning on assigning a new one you can check to see if it's already taken. The plugin does more, as well, so if you're a plugin dev, or just a customization/macro geek like me, it's really quite useful.
As a caveat, with the complexity of the layout you displayed above, the "layout": key is going to be quite large and complex, and is made larger by the fact that each value in the "cells":, "cols": and "rows": keys is on its own line. I don't know enough regex to clean everything up automatically, but I'm sure it can be done.

Hotkey to end the line with a semicolon and jump to a new line in Sublime Text 2

I'm trying to figure out a hotkey at work. I just got this job and I am using a Mac for more or less the first time in my life.
Back home on my Laptop, when using Eclipse, I seem to remember there being a single hotkey which would both:
Add a ; to the end of my current line (no matter where the caret was within said line)
Place my cursor at the beginning of a new line, with the same indentation level as the line I had just added a semicolon to
Does anybody know if this was an Eclipse-specific hotkey, or know of a way to replicate said hotkey in Sublime Text 2?
Best solution for this is recording a macro on Sublime Text and then assigning it to a keyboard shortcut. Follow these steps:
Create a line such as alert('hello') and leave the cursor right
after letter 'o'.
Then go to Tools > Record a Macro to start recording.
Press Command+→ to go to the end of line.
Press ; and hit Enter
Stop recording the macro by going to Tools > Stop Recording Macro
You can now test your macro by Tools > Playback Macro (optional)
Save your macro by going to Tools > Save Macro (ex: EndOfLine.sublime-macro)
Create a shortcut by adding this between the square brackets in your
in your Preferences > Key Bindings - User file:
{
"keys": ["super+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EndOfLine.sublime-macro"}
}
Now, every time you hit Command+;, it will
magically place the semicolon at the end of current line and move the cursor to the next line.
Happy coding!
After reading your question about three times, I finally realized that you were looking for one hotkey to perform both operations. Whoops.
Your request sounds like the Ctrl+Shift+; hotkey of the Smart Semicolon Eclipse plugin. While adding semicolons of a genius-level IQ would probably require an entirely new Sublime Text 2 plugin, you can easily create a smart semicolon–esque key binding with Sublime Text's Macros. I actually didn't know about them until now!
In this case, recording the macro yourself is actually the fastest way to create it, rather than copying and pasting a new file (and now you'll have the experience for making more). First, open a new file and type in your favorite garbage line:
Lord Vetinari's cat|
Then move the caret to anywhere within the line:
Lord Veti|nari's cat
Now, press Ctrl+Q, the hotkey for Tools -> Record Macro. If the status bar is enabled, it will notify you that it is "Starting to record [a] macro". Press End (if you don't have an End key, skip to below), then ;, then Enter. Finally, press Ctrl+Q again to stop recording. When you do, the status bar will display "Stopped recording macro". Check that your macro is working by hitting Ctrl+Shift+Q on a code segment of your choosing.
Just pressing Enter will adjust indentation on the next line accordingly as long as the "auto_indent" setting is set to true. See Preferences -> Settings – Default, line 59.
When you're satisfied, save your new macro with Tools -> Save Macro.... I saved mine as Packages/User/smart-semicolon.sublime-macro. My file looked something like this; feel free to copy it if you can't or won't make the macro manually:
[
{
"args":
{
"extend": false,
"to": "eol"
},
"command": "move_to"
},
{
"args":
{
"characters": ";"
},
"command": "insert"
},
{
"args":
{
"characters": "\n"
},
"command": "insert"
}
]
"extend": false, just means that the macro won't add any text to the working selection. Read more about the options for commands at the Unofficial Docs Commands Page.
Now that you have your macro, we can give it a custom key binding. Add the following lines to your Preferences -> Key Bindings – User file:
{ "keys": ["ctrl+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/smart-semicolon.sublime-macro"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.java" }
]
},
Replace Ctrl+Shift+; with whatever key binding you prefer, save your file, and give it a shot. The "context" array restricts the key binding to Java files (see the Unofficial Docs Key Bindings page for more information on contexts); if you want the key binding to be active everywhere, use this line instead:
{ "keys": ["ctrl+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/smart-semicolon.sublime-macro"} },
This NetTuts+ article has much more information on macros and binding them to keys; I referenced it often. This UserEcho post looks like it has more information on making the insertion more extensible.
You can also use a ready-made sublime package built for this purpose: AppendSemiColon
You can use package control to install it, just search for "AppendSemiColon".
Place a semicolon at the end of the cursor's current line(s) by pressing:
Command+; on OS X
Ctrl+; on Windows and Linux
and
You can also automatically go to the next line at the same time like this:
Command+Shift+; on OS X
Ctrl+Shift+; on Windows and Linux
I've been using this package for a while now, and it works great.
UPDATE:
As the author mentioned in a comment, you can now also change the default hotkeys if you like (personally, I love the defaults). As an example, just change:
[
{ "keys": ["ctrl+;"], "command": "append_semi_colon" },
{ "keys": ["ctrl+shift+;"], "command": "append_semi_colon", "args": {"enter_new_line": "true"} }
]
in your sublime-keymap to use whatever keys you want.
I tested the latest version, and this feature too works fine. There was a bug where extra semicolons were being appended incorrectly upon extra hotkey presses - this minor annoyance has been fixed too. Thanks, MauriceZ/mzee99!
Ctrl+Enter will create a new line with the same level of indentation.
I could not find anything like the Ctrl+A you mentioned
I'm on Sublime Text 3 ... inspired by your post, I've added the following to my .sublime-keymap:
{ "keys": ["ctrl+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/endOfLine.sublime-macro"} },
I'm amazed at how much time this saves.
#Felipe covered the second point. You can mimic the behavior of the first with a simple macro or plugin. I say or because you didn't describe the cursor position after you hit the key combination. Does it move the cursor to the next line? Does it insert a new line? I think you get the idea.
I'm pretty sure eclipse works on OS X as well as windows, so if you are more comfortable with that, why not use it (unless your job requires ST of course)? Most of the key bindings are probably the same/similar (well it's probably command rather than control for shortcuts).
http://www.eclipse.org/downloads/?osType=macosx
If you continue using ST, it's probably worthwhile to learn the basics of plugins. A lot can be built so you have the same behavior as other editors, it just takes some extra effort (through creating the plugin or finding a plugin that does it) up front.