Delete extra spaces in list items - Sublime Text 2 - list

Sometimes I need to put tags at the end of a list item using the shortcut: [Ctr] + [Shift] + [L] + [End] to edit multiple list items using multiple cursors. This doesn't always work so well when there are extra spaces at the end of a list item, making tha actual end of the item a few spaces longer than the text of the item.
Is there a shortcut I can use along with [Ctr] + [Shift] + [L] to delete those extra spaces and essentially make the "end" where the text ends?
Please ask questions if you need clarification on my question and I will try to best make it most clear.

I haven't found shortcuts for this. So I wrote one. It will trim the trailing white spaces of the current line. For example, after you enter ctrl+shift+l, you find that there are extra white spaces, enter ctrl+f and then ctrl+t to delete them. White spaces in other line won't be affected.
Key mapping:
{ "keys": ["ctrl+f", "ctrl+t"], "command": "delete_trailing_white_space" }
delete_trailing_white_space.py (put it Sublime Text 2\Packages\User)
import sublime
import sublime_plugin
class DeleteTrailingWhiteSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
line = self.view.line(region)
line_content = self.view.substr(line)
trimed_line = line_content.rstrip()
self.view.replace(edit, line, trimed_line)

If you don't want to delete the spaces, you can rebind the end key to a plugin that modifies the behavior. I wrote this when I was playing around with some virtual white space stuff, so it's only minimally tested (link). Save it to Packages/User. The name of the file doesn't matter, just make sure it has the .py extension. Then, add the following as a custom key binding.
{
"keys": ["end"], "command": "custom_move_to_end_line"
}

Related

Sublime Workflow for replacing quotes

I use text editor Sublime Text 3 to edit code, and very often I'll have a string literal wrapped in double quotes, that I want to change to single quotes, or vise versa. Right now I scroll to each quotation mark, and replace it with the one I want. Is there a faster workflow for this? Say, highlighting the word or a hotkey or something? I would find it super useful.
If you have a large number of such strings in a file and you want to convert all of them at once, you could use a regex find/replace operation to find and replace them all. You would use Find > Replace... or Find > Find in files... to search for a matching regex that captures the text in the quotes.
For example you could use \"([^"\n]*)\" as a search term and '\1' as the replacement text to swap all double quoted strings for single quotes.
You can't bind something like that to a key directly because Find/Replace can't be used in a Macro, but you could use the RegReplace package to do this if you want to go that route.
You can potentially speed up the workflow that you're currently using by taking advantage of multiple cursors, if you're not already doing that.
You could for example select the first quote, then press Ctrl+D or Option+D to select the other one. Now that you have two cursors, press Backspace to delete both quotes and press the new quote character to insert the new ones.
This can't be macro-ized and bound to a key because the find_under_expand command can't be used in a macro, though.
For a full key press solution, as far as I'm aware you would need a plugin of some sort to do this for you. One such example appears to be ChangeQuotes, although I've never personally used it.
It's also possible to write your own small plugin such as the following:
import sublime
import sublime_plugin
class SwapQuotesCommand(sublime_plugin.TextCommand):
pairs = ["'", '"']
def run(self, edit):
self.view.run_command("expand_selection", {"to": "scope"})
for sel in self.view.sel():
self.toggle(edit, sel)
def toggle(self, edit, region):
begin = self.view.substr(region.begin())
end = self.view.substr(region.end() - 1)
if begin == end and begin in self.pairs:
index = self.pairs.index(begin) + 1
new = self.pairs[index % len(self.pairs)]
for point in (region.begin(), region.end() - 1):
self.view.replace(edit, sublime.Region(point, point+1), new)
This expands the selection in all of the cursors out by the current scope, and then if both ends of the selection are a matching quote, the quote in use is swapped.
In use, you would use a key binding such as the following, which includes a context to make the key only trigger while the cursor is inside of a string so that it doesn't mess up your selection in cases where it definitely won't work.
{
"keys": ["ctrl+shift+'"], "command": "swap_quotes",
"context": [
{ "key": "selector", "operator": "equal", "operand": "string.quoted", "match_all": true }
]
},

Remove Multiple Periods Up To Bracket From String

Would like to know how to create an Emacs macro that will
Find the first instance of multiple periods in string
Set mark
Move to the first closed bracket in string
Remove all chars between mark and closed bracket
Here is an example string. I'd like to go from this:
* [This is Chapter 1.......................................................... 1-83](chapter1.md)
To this:
* [This is Chapter 1](chapter1.md)
Can anyone assist?
Thanks
Heres the hacky way I accomplished. I'm sure there is a cleaner way.
Start with cursor at the beg of line
M-x start-kbd-macro
C-s RET .. to search for first instance of ".." in the string
C-SPACE to set mark
C-s ] to search for first instance of "]" in the string
DEL to remove everything marked
BKSP BKSP to remove the final two ".."
DWN ARROW to get to next line
C-a to get to beg of line
M-x end-kbd-macro
I know its lame, but it worked!! I have ~100 pages of docs to do this to! Need to figure out how to reliably perform this on the entire doc next.

notepad++ Stop replacing at a specific line

I've been trying to figure something out for a while now and I can't seem to understand. I've looked everywhere and I still can't find it.
I'm trying to make a dictionary for an auto corrector with AutoHotKey and I need to replace the beginning of each line with "::" and somewhere in between the line with another "::"
like so:
::togehter::together
Now I have around 20,000 of these to add with no "::" yet and what I'm doing is this in the replace textbox:
Replace: ^
With: ::
Now it works fine for the first line BUT if I press replace all cause no way am I going to click 20,000 times on replace, it replaces not only from where I am to the bottom but also the beginning too. So every line now has a new "::" added.
So what I need is to be able to tell the replace at what line to stop instead of doing every single line.
Also if you could help me add the "::(word)" after the first ::(misspelled word) that would be a great help.
Image for reference
I have found that the regular expression replace-all of ^ with some text, i.e. to add some text at the start of every line, does not work in some versions of Notepad++. My workaround for this was to use the ^(.) as the search string and include \1 in the replacement. For your case the replacement would be ::\1. The effect here is to replace the first character of each line with :: plus the first character. In a quick test with Notepad++ v7.1, replacing ^ with :: worked as I would want.
Two things should be checked in the Replace dialogue before doing the replace-all: (1) that "Regular expression" is selected and (2) "In selection" is not selected.
The question is not clear how the two words in the input are separated, so assuming that one or more spaces or tabs is used the search string to use is ^(\w+)\h+ and the replace string is ::\1::.
This AutoHotkey script might do what you require.
It leaves unchanged lines that start with '::',
and prepends/replaces text in the others. You copy the original text to the clipboard, run this script, and then the desired text is put on the clipboard. (To create and run the script: copy and paste it into a text editor and save it as myscriptname.ahk, or myscriptname.txt and then drag and drop the file into the AutoHotkey exe file. Or alternatively, if you save it as an ahk file, and install AutoHotkey, you can double-click to run.) AutoHotkey
vText := Clipboard
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
StringReplace, vText, vText, `r`n, `n, All
Loop, Parse, vText, `n
{
vTemp := A_LoopField
if (vTemp = "")
if (1, vOutput .= "`r`n")
continue
if (SubStr(vTemp, 1, 2) = "::")
if (1, vOutput .= vTemp "`r`n")
continue
StringReplace, vTemp, vTemp, %A_Space%, ::, All
vOutput .= "::" vTemp "`r`n"
}
Clipboard := vOutput
MsgBox done
Return

how to bulk remove the name in my list with regex

I have many bbm pin list
and I want to delete all the names with the red line on the list in bulk using regex?
Sample:
5e357935 JANCOK
51de9aa7 ASU
5AB88ECD
570E997E
570E997E COK
5D026965
5787B95A
5B0502DB
5bbb3c33 NGNTOD
7C60CE59
5815E544
5EA4EA57 KIRIK
5EBA3A6A
5ec0c768 PELER
5F6E8BEC
5F553830
5EA4EA57
5a476122 DOMBA
The picture in the question looks suspiciously a screenshot from Notepad++
In that case one can do it directly in that editor via a Replace All.
I would do this:
:g/ /d
That's a vim (ex) command, which says "delete every line that contains a space".
Since it looks like you're on Notepad++, I'd use the following search/replace :
search " .*$" (a space followed by any characters up to the end of the line)
replace by "" (the empty string)

Strip blank lines from first n lines using sed

I need to strip blank lines from only the first 6 lines of a text file. I've attempted to cobble together a solution using this StackOverflow question and this file but to no avail.
Here's the sed script I'm using (aliased as faprep='~/misc-scripts/fa-prep.sed), the last command is the one that's failing:
#!/opt/local/bin/sed -f
# Title Treatments
s|<\(/\?\)h1[^>]*\?>|[\1b]|g # Replace <h1></h1> with [b][/b] for saga titles
s|<\(/\?\)h2[^>]*\?>|[\1i]|g # Replace <h2></h2> with [i][/i] for arc titles
s|</\?h3[^>]*\?>||g # Strip <h3 id=""></h3> out without removing chapter title text
# HTML tag strips & substitutions
s|</\?p>||g # Strip all <p></p> tags
s|<\(/\?\)em>|[\1i]|g # Change <em></em> to [i][/i]
s|<\(/\?\)strong>|[\1b]|g # Change <strong></strong> to [b][/b]
# Character code substitutions
s/&\#822[01];/\"/g # Replace “ and ” with straight double quote (")
s/&\#8217;/\'/g # Replace ’ with straight single quote (')
s/&\#8230;/.../g # Replace … with a 3-period ellipsis (...)
s/&\#821[12];/--/g # Replace — with a 2-hyphen em dash (--)
# Final prep; stripping out unnecessary cruft
/<body>/,/<\/body>/!d # Delete everything OUTSIDE the <body></body> tags
/<\/\?body>/d # Then, delete the body tags :3
# Pay attention to meeeeeeee!!!!
1,6{/./!d} # Remove blank lines from around titles??
Here's the command I'm running from terminal, which shows the last line failing to strip whitespace from the first 6 lines of the file (after all of the other modifications have been made, of course):
calyodelphi#dragonpad:~/pokemon-story/compilations $ ch='ch6'; faprep $ch-mmd.html > $ch-fa.txt; head -6 $ch-fa.txt
[b]Hoenn Saga (S1)[/b]
[i]Next City Arc (A2)[/i]
Chapter 6: A Peaceful City Stroll... Or Not
calyodelphi#dragonpad:~/pokemon-story/compilations $
The rest of the file is composed of a blank line after the third title and then paragraphs all separated by blank lines. I want to keep those blank lines, so that only the blank lines between the titles at the very top are stripped.
Just to clarify a few points: this file has Unix line endings, and the lines are supposed to not have spaces. Even viewing in a text editor that shows whitespace, each blank line contains only a newline character.
Since the discussion in the comments made it clear that you want to ignore empty lines in the first six lines of the body tag -- in other words, the first six times that part of the script is reached -- rather than the first six lines of the overall input data, you cannot use the global line counters. Since you're not using the hold buffer, we can use it to build our own counter, though.
So, replace
1,6 { /./! d }
with
x # swap in hold buffer
/.\{6\}/! { # if the counter in it hasn't reached 6
s/^/./ # increment by one (i.e., append a character)
x # swap the input back in
/./!d # if it is empty, discard it
x # otherwise swap back
}
x # and swap back one more time. This dance ensures that the
# line from the input is in the pattern space when we drop
# out at the bottom to the printing, regardless of which
# branches were entered.
Or, if this seems too complicated, use #glennjackman's suggestion and pipe the output of the first sed script through sed '1,6 { /./! d; }', since the second process will have its own line counters working on the preprocessed data. There's no fun in it, but it'll work.
This answer courtesy of #Wintermute's comments on my question pointing me in the right direction! I was mistakenly thinking that sed was working on the modified stream when I put that delete statement in at the very end. When I tried a different address (lines 9,14) it worked perfectly, but was too hackish for me to settle on. But this confirmed I needed to think of the stream as still including lines that I thought were already gone.
So I moved the delete statement up above the statement that clears out the <body> tags and everything outside them, and used a regex and the addr1,+N trick here to produce this final result:
The script:
#!/opt/local/bin/sed -f
# Title Treatments
s|<\(/\?\)h1[^>]*\?>|[\1b]|g # Replace <h1></h1> with [b][/b] for saga titles
s|<\(/\?\)h2[^>]*\?>|[\1i]|g # Replace <h2></h2> with [i][/i] for arc titles
s|</\?h3[^>]*\?>||g # Strip <h3 id=""></h3> out without removing chapter title text
# HTML tag strips & substitutions
s|</\?p>||g # Strip all <p></p> tags
s|<\(/\?\)em>|[\1i]|g # Change <em></em> to [i][/i]
s|<\(/\?\)strong>|[\1b]|g # Change <strong></strong> to [b][/b]
# Character code substitutions
s/&\#822[01];/\"/g # Replace “ and ” with straight double quote (")
s/&\#8217;/\'/g # Replace ’ with straight single quote (')
s/&\#8230;/.../g # Replace … with a 3-period ellipsis (...)
s/&\#821[12];/--/g # Replace — with a 2-hyphen em dash (--)
# Final prep; stripping out unnecessary cruft
/<body>/,+6{/^$/d} # Remove blank lines from around titles
/<body>/,/<\/body>/!d # Delete everything OUTSIDE the <body></body> tags
/<\/\?body>/d # Then, delete the body tags :3
And the resulting output:
calyodelphi#dragonpad:~/pokemon-story/compilations $ ch='ch6'; faprep $ch-mmd.html > $ch-fa.txt; head -6 $ch-fa.txt
[b]Hoenn Saga (S1)[/b]
[i]Next City Arc (A2)[/i]
Chapter 6: A Peaceful City Stroll... Or Not
The next two weeks of training passed by too quickly and too slowly at the same time. [rest of paragraph omitted for space]
calyodelphi#dragonpad:~/pokemon-story/compilations $
Thanks #Wintermute! :D