Sublime Text 2: Trim trailing white space on demand - whitespace

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" }

Related

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

Sublime insert a symbol to the end of line to make column

I want to insert " to the end of each lines
I have the following text:
Some test\n
Also some text\n
The rest of text\n
And I want to get like this one:
Some test "\n
Also some text "\n
The rest of text "\n
Where \n - end of line
I just did a search for "sublime line padding" and found:
http://wbond.net/sublime_packages/alignment
Hope it helps.
From the website:
Features
Align multiple selections to the same column by inserting spaces (or tabs)
Align all lines in a multi-line selection to the same indent level
Align the first = on each line of a multi-line selection to the same column
See the screenshots below for examples of the plugin in action.
Installation
Download Package Control and use the Package Control: Install Package command from the command palette. Using Package Control ensures Alignment will stay up to date automatically.

TextMate: remove trailing spaces and save

I'm using a script to remove trailing spaces and then save the file.
The problem is that all my code foldings expand when I use it. How do I change the command so it will keep the code foldings?
You can use foldingStartMarker & foldingStopMarker to indicate the folds to TextMate.
To define a block that starts with { as the last non-space character on the line and stops with } as the first non-space character on the line, we can use the following patterns:
foldingStartMarker = '\{\s*$';
foldingStopMarker = '^\s*\}';
pressing the F1 key will fold any code folds present.
Reference: http://manual.macromates.com/en/navigation_overview#collapsing_text_blocks_foldings

TextMate find text and then delete entire line?

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

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt

Resources