TextMate find text and then delete entire line? - textmate

I'll show you what I want to do using a textmate command or bundle:
Lets say we have the following document:
foo
diddy
bah
foo
foobah
diddy
I want to find and delete all the lines matching bah, the desired ouput in this case would be:
foo
diddy
foo
diddy
Thanks!

With the document you want to filter open,
Cmd-F to bring up the Finder
window
Next, below the two text-entry boxes,
click Regular Expression
In the Find text box, type in
this regexp (without the spaces):
^ . * ? bah . * ? $
In the Replace text box, do not type
in anything--make sure it blank,
i.e., no whitespace characters
With the cursor at the beginning of
the document you want to filter,
click one of the buttons at the
bottom of the Find Window--e.g.,
Replace All to remove all of the matching lines in one step, or
Replace and Find to step through the lines one at a time
That's it.
Here's a more automated way to do the same thing:
from the Menu Bar, select Filter
Through Command from the Text
pull-down menu
enter this into the text box at the
top of the small window that appears:
sed ' / ^ . * bah . * $ / d '
select Document as Input and select
Replace Document as Output
Click Execute
[Note: i inserted spaces between the regexp tokens in both examples because for some reason the asterisks '' were not rendering in HTML page]

Building on top of #doug's answer
Since the user wants to delete the line after the match, the solution using Regex is
^ . * ? bah . * ? $\n
Matching the \n at the end will remove the line

Related

Reverse all line of text in Sublime Text

I have a file where I have multiple lines. Is there an option in Sublime Text 3 to reverse whole line ? Like
ABCDEFG
to
GFEDCBA
if someone need to do the following operation.
12345
67890
abcde
|
to
|
v
abcde
67890
12345
click Edit---->Permute lines--->Reverse and it will reverse all lines you selected in a file.
You best bet would definitely to take Leonid's advice and use a different tool, but if you are curious as to how one might do that in Sublime you have two options.
First go to Tools->New Plugin and paste the following code into the file:
import sublime, sublime_plugin
class ReverseCharactersCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
stringContents = self.view.substr(region)
self.view.replace(edit, region, stringContents[::-1])
Following that select the different sections of the document that you want reversed and run the follow command from the console
view.run_command("reverse_characters")
Here is an image of that workflow.
The import section of that code is the:
stringContents[::-1]
Which is an idiomatic way of reversing a string in Python.
Alternatively you could go checkout this follow git repository and which has the same code and a convenient command palette options specified for you :)
https://github.com/MattSeen/ST_ReverseCharacters
In vanilla sublime:
Select the text to reverse
Open the Find menu (Ctrl+F)
Ensure "In selection" and "Regular expression" options are enabled
Search for . and click "Find all" (⌥+Enter)
Now each character of the selection is highlighted with its own cursor.
Click Edit > Permute selections > Reverse
It's not elegant but it is straightforward and repeatable. If you already have the cursors, all you need is step 4.
Not inside Sublime Text, but in Linux/OSX the rev command-line utility does just that - rev file.txt reverses every line of the file.

Sublime - delete all lines containing specific value

I have a 900mb log file which I can open in SublimeText 3. This file is bloated with lines similar to the following.
10/08/2014 23:45:31:828,Information,,,,ExportManager: ,No records to send and/or not connected
How can I filter out all the lines which contain No records to send and/or not connected
You can do a regular expression search-and-replace:
Click Find > Replace.
Ensure that the Regular Expression button is pressed.
For the Find What field, put:
^.*No records to send and/or not connected.*\n
Leave the Replace With field empty.
Click Replace All
For people that don't want to write a regex - you can just select the search string, hit ctrl+cmd+g or pick "Quick Find All" from the menu, which will get you selections for each matching string; from there Home will move every selection cursor to the start of the line, shift+End will select every matching line, and del, del will delete all of them.
Multiple cursor editing is fun!
i could not get the regex to work so I used Alt-F3 approach from this answer:
https://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999
Select string of interest
Hit Alt+F3 to go into multi-cursor mode on all occurrences (Ctrl+CMD+G on Mac OS X)
Hit Ctrl+L [see comments] (Cmd+L on Mac)
Copy-paste selection to another buffer
Del
This is what i found for the windows users:
Select the string (every line containing this string is to be removed).
Press ALT+F3 .
Press Ctrl+L .
Press Delete .
Neither of the regex code suggested above worked in my case, but this did work:
.*(text in question).*
A simple way of doing it is:
1 Open Sublime Text
2 Find => Replace (Ctrl + H)
3 in Find write the desired text
4 click Find All
5 press ctrl + shift + K to remove all the lines where this search is present
This is a quick solution to remove some lines that contains some text
Above answers are the correct ways, but if you want to get rid of the rows with even a single string then do,
Find -> Replace -> put ^.*[a-zA-Z]+.*\n In the find section and keep replace with blank. Hit the replace all button this will delete all the rows with even a single string in it.
I like the manual edition solution, very good.
But.. have you tried to use cat and grep -v to filter out the lines and redirect to another file? Maybe better than learning regex.. (personally I always start with regex and end with editing the files myself).
In Windows you use findstr /v.
So you would do:
# in bash
cat my.log | grep -v "No records to send and/or not connected" > new.log
or
# in cmd
cat my.log | findstr /v "No records to send and/or not connected" > new.log
I ran into a similar problem editing a sitemap
This worked for me:
Copy the last word in the lines that you want to delete
Find all
Press delete to delete the entire line
Find -> Find all (this will mark the lines having the keyword)
Then go to Edit->Line->Delete line

SublimeText 2: Select lines that match search terms

I need to select some lines inside of a list of file names to bring them at the end of the list.
Suppose I have Files.txt with this list:
filename1.aaa
filename1.bbb
filename1.ccc
filename2.aaa
filename2.bbb
filename2.ccc
filename3.aaa
filename3.bbb
filename3.ccc
I want to select all the lines where the file extension is ccc and then be able to CTRL-X and CTRL-V to put them on the bottom of the list.
So the result have to be like this:
filename1.aaa
filename1.bbb
filename2.aaa
filename2.bbb
filename3.aaa
filename3.bbb
filename1.ccc
filename2.ccc
filename3.ccc
Is it possible?
In Windows press:
Ctrl+F for searching all ".ccc" terms in file;
Ctrl+L to expand selection to line;
Ctrl+X to cut all selected lines;
End to go at the end of your file;
Ctrl+V to past all selected line;
If you are using a Mac press Command instead of Ctrl.
This page could also help you: http://www.sublimetext.com/docs/selection
This is very easy in Sublime, select the extension .ccc on one of the lines and then press Alt+F3 to select all other matching occurrences of that selected text. Then press Ctrl+L to expand selection to the entire line, then cut and paste those lines elsewhere.

Sublime Text 2: Trim trailing white space on demand

I know that Sublime Text 2 can delete the trailing white space on files upon saving.
When working in a team and commiting a change to a file this tends to produce huge diffs which make peer code review more cumbersome. For that reason I prefer to only do the white space cleaning when I'm commiting huge changes to a file anyway and leave whitespace as it is for the minor changes.
I would like to know if there's any command for executing the trimming of the white space on demand on a file, other than "Activate trimming on save > Save file > Deactivate trimming".
Searching in the Documentation and on stackoverflow didn't show anything relevant, all the links seem to talk about the auto trimming on save.
I use these steps for a quick on-demand solution within Sublime Text:
Find > Replace...
Find What: [ \t]+\n
Replace With: \n
Replace All
You could also do this for a large set of files via
Find > Find in Files...
Find: [ \t]+\n
Where:
Replace: \n
Replace
Beware: using this plugin makes Sublime Text significantly slower
I use TrailingSpaces plugin for this.
Highlight trailing spaces and delete them in a flash.
ST2 provides a way to automatically delete trailing spaces upon file
save. Depending on your settings, it may be more handy to just
highlight them and/or delete them by hand. This plugin provides just
that!
Usage: click "Edit / Trailing Spaces / Delete".
To add a key binding, open "Preferences / Key Bindings - User" and add:
{ "keys": ["ctrl+alt+t"], "command": "delete_trailing_spaces" }
You can simply use a regex to remove trailing whitespaces:
Find > Replace...
Find what: [^\S\r\n]+$
Replace with: leave empty.
Click 'Replace All'
[^\S\r\n]+$ is Regex for "at least one whitespace character (so spaces and tabs but not newlines, using a double negation) followed by the end of the line"
Regular Expression must be enabled:
This method isn't perfect, but uses no plugins or settings and works in most situations.
Multi-Select and move cursor to the end of every line
Hold CTRL-Shift, Press Left, Right
The spaces and tabs at the end of the lines should now be selected. Press Delete or Backspace
Note - Special characters such as ( and + may also be selected at the end of the line at this point, not just spaces.
How to Multi-Select all lines:
One way is to use the middle mouse key to select vertically then hit the End Key if it's a small selection.
With hot-keys:
CTRL-A (select all)
CTRL-SHIFT-L (place cursor on all lines selected)
END (Go to end of lines)
You can also use the find function to find something that will be in every line, like the space character:
\s (using regex)
Click Find All
Press the "End" key to get multiple cursors at the end of each line
Sample Text:
text and number 44 more text and a space
text and number 44 more text and 2 tabs
text and number 44 more text and no space or tab
text and number 44 more text after a line feed
I found a soulution here:
http://www.sublimetext.com/forum/viewtopic.php?f=4&t=4958
You can modify the package
trim_trailing_white_space.py
located in the default packages directory, this way:
import sublime, sublime_plugin
def trim_trailing_white_space(view):
trailing_white_space = view.find_all("[\t ]+$")
trailing_white_space.reverse()
edit = view.begin_edit()
for r in trailing_white_space:
view.erase(edit, r)
view.end_edit(edit)
class TrimTrailingWhiteSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
trim_trailing_white_space(self.view)
class TrimTrailingWhiteSpace(sublime_plugin.EventListener):
def on_pre_save(self, view):
if view.settings().get("trim_trailing_white_space_on_save") == True:
trim_trailing_white_space(view)
class EnsureNewlineAtEof(sublime_plugin.EventListener):
def on_pre_save(self, view):
if view.settings().get("ensure_newline_at_eof_on_save") == True:
if view.size() > 0 and view.substr(view.size() - 1) != '\n':
edit = view.begin_edit()
view.insert(edit, view.size(), "\n")
view.end_edit(edit)
Now you can add the command to your keymap configuration:
{ "keys": ["your_shortcut"], "command": "trim_trailing_white_space" }

notepad++ remove line with blank space

i have multiple document which has lines which starts with blank space. those lines should be removed.
how can one remove a line which contains a blank space in the beginning and no other chars?
With Notepad++, you can use regular expression search and replace.
Go to Replace, select "Regular Expression" in the Search Mode box, and enter the following search expression:
\r\n \r\n
This means: newline, space, newline.
Replace by nothing. That should do the trick!
This is in Notepad++
Search
Replace
Find what: ^ +$
Replace with: (blank)
Search Mode: Regular expression
I my case regEx not solve the problem, I use plugin TextFX and I solve like this:
delete all spaces:
delete lines:
Find and Replace (Ctrl + h) than on Search Mode settings check Regular expression. After you sett the box to search with regex patterns enter this into the Find what field: ^([ ]+)$ and leave the replace field empty. Than click Replace All. That's it.
This will find all lines starting with a space character (may contain more that one space) but that have NO other character after the space/s.
Go to Edit-> Line Operations->Remove Empty Lines(Containing Blank Characters)

Resources