Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 232
Mohammad Yusuf Avatar asked Nov 10 '16 04:11

Mohammad Yusuf


People also ask

How do I select all rows in Sublime Text?

You can use Ctrl and Alt with this too. Shift+Right mouse button is an alternative way to initial a column select. Dragging in the gutter (where the line numbers are), will select entire lines at once.

How do you select multiple lines in Sublime Text 3?

Use CTRL+D at each line and it will find the matching words and select them then you can use multiple cursors.

How do I get line numbers in Sublime Text?

Just hit Ctrl + G , and it does the same steps for you!


2 Answers

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)

Sublime Text screenshot

like image 114
Daniil Ryzhkov Avatar answered Oct 25 '22 22:10

Daniil Ryzhkov


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.

like image 38
OdatNurd Avatar answered Oct 25 '22 23:10

OdatNurd