I'm trying to add a macro/command to Textmate for some file cleanup and the last tidbit I haven't figured out is simply to remove blank lines from the beginning and end of a file -- does anyone know how to do this? I know some Textmate or regex trick must exist, just not sure what...
For the tasks you mentioned, you can use built-in commands from the "Text" Bundle.
Even if you just want to script your own, i would suggest using these as templates for your own efforts.
To access these:
Removing Trailing Spaces:
ctrl-cmd-t to bring up the context-sensitive Bundle menu
begin typing "remove trailing",
before you finish "remove", you
should see the "Remove Trailing
Spaces" Command move to the top of
the menu (this Command is in the
"Text" Bundle, one of the Bundles
included with the TextMate )
"enter" will execute the command
(assume the cursor is positioned
correctly, etc.)
Alternatively, you can access this Command using a key equivalent, but since one is not assigned in the default TextMate installation, you'll need to assign it yourself, which is simple to do:
ctrl-alt-cmd-b to bring up the
Bundle Editor
find the Text Bundle then click on
the "Remove Trailing Spaces" Command
In the upper right-hand-side of the
Editor, toggle "Settings" and enter
either a key equivalent or a "tab
trigger" (which is activated by
entering some key combination you
assigned followed by the tab key.
Remove Leading Spaces:
This one's a macro rather than a command. It's probably easiest to access it via it's pre-configured key-equivalent, which is cmd-del
This regex will remove the whitespace at the beginning of the file
Find ^[\r\n\t ]+ and replace with (nothing).
And this one will remove the whitespace at the end
Find [\r\n\t ]+$ and replace with (nothing).
I've never used Textmate regular expressions. There can be a \s (whitespace) class you can use instead of [\r\n\t ]. Also, you might need to turn multi-line mode on, if there is one.
to match use the following regEx
^[ ]+|[ ]+$
and replace it by empty sting("")
Related
I am using visual studio code for several things. Everything is working fine, but I cannot get one specific thing to work.
I need the ability to remove line breaks from the text.
Example:
first line
second line
Should become:
first linesecondline
Since a recent update it is possible to search for line breaks with using ^$.
It is described here: https://github.com/Microsoft/vscode/pull/314
The problem I have is that when I use this for replacing, it does actually "add" to the line break and does not "replace" it.
The latest version of VS Code has a shortcut to join lines (some may say remove breaks) from selection: CTRL + J.
I found that (at least on Windows) the solution was to use search and replace with a regular expression. Search for $\n and replace with nothing to get rid of the newlines. Note that the newline character that we want to replace is placed after the end of line matcher ($).
#tripleonard hint did not work for me (no shortcut key assigned), so what I did was first ctrl+shift+p to list all commands and then just type Join lines
I'm able to manage this with the search and replace tool and "Use Regular Expression" enabled. Search for the pattern \n$ and replace with $
In my case shorcut in VS Code was not set. It took me a while to find out what command in VS Code am I looking for. For other with same problem it is: "Join lines".
Turn on regex mode and find and replace.
Search for \n and replace with nothing.
Select the new line, and press ctrl+D (and hold it).
Then press ctrl+h, you will be able to replace it with whatever you need.
On Mac, use cmd+a to select all lines. Then, use cmd+shift+p to open commands and type Join Line and click on it.
You can use \n to search for new lines
but while finding/searching,
the Use Regular Expression option should be enabled
Press ctrl+f or ctrl + h
Copy and past this ^(\s)*$\n expression into top input field
after click on the * icon, then you can see all white lines break.
Past bottom input field = \n //one line break
That means what you want to replate in white line break
After click on the Replace or Replace All Icon button
https://bitcoden.com/answers/visual-studio-code-delete-all-blank-lines-regex
I'm new to Regex and trying to figure out how to remove all text from file open in Notepad++ that does not match #LCxxxx or #LAxxxx. Example below (text wanting to keep in bold):
1.In rare cases, reinstalling this MSP file can cause the Citrix Display Driver.....
[From ICAWS760WX86][#0528688]
30.This release includes an enhancement...
[From ICAWS760WX86022][#LA3014]
New Fixes in This Release
1.Windows Server 2008 R2 and Windows Server 2012 R2,...
[From ICAWS760WX86026][#LC2179]
Fixes from Replaced Hotfixes
1.If the Windows Remote Desktop Session Host....
[From ICAWS760WX86004][#LC1180]
I think this is what you're looking for:
(?:[\S\s]*?)(\#L[AC]\d{4})(?:.*)
Replace with:
$1\n
You could do a regular expression search and replace, searching for
(#L[AC]....)
where "dot matches newline" is NOT selected. Replace with
\r\n\1\r\n
That will put all the wanted pieces of text on a line on their own.
Next use the "Mark" tab in the find window. Select "Bookmark line", use the same search string as above (the capture brackets are not needed this time, but they are harmless and so can be left), and them click "Mark all". Now all the wanted lines are bookmarked. Use menu => Search => Bookmark => Remove unmarked lines.
There may be a way of doing it all in one go, but that would be a complex regular expression. The method above uses two simple steps.
remove all text from file open in Notepad++ that does not match #LCxxxx or #LAxxxx
^.*(\[#L[CA]\d+\])$|^.*$
DESCRIPTION
DEMO
https://regex101.com/r/hO1aL8/2
Notepad++
Do a search and replace like describe in the screenshot below:
Alternatively, if you want to get rid off the empty lines during the replace operation, use the regular expression below:
^[\S\s]+?(\[#L[CA]\d+\])$
\s : Whitespaces (\t,\r,\n ...)
\S : Any character except whitespaces.
Tested on Notepad 6.6.9
I noticed I could use regex functions with search in vim, and I could see hilights while I typed by setting incsearch. But that didn't work for search and replace queries like this one:
:%s/std::regex\s\([_a-zA-Z]*\)(/regex_t \1 = dregc(/gc
That one surprised me when it actually worked.
Are there settings or plugins for vim that, like incsearch but better, will highlight your replace query as you type? Just highlighting the matches would be pretty neat, but putting the old and new strings next to eachother in different highlighting colors would be a godsend, because I might not be sure about the backreference.
Not a direct answer to your question, but traditionally in Vim you craft your search regex first, as in:
/regex
Then you hit enter to execute it. The settings :set hlsearch and :set incsearch make this easy to see visually. Then you can just do:
:%s//replace
With no search specified, :%s (substitute acting on %, a shorctut meaning all lines in the file) will use the last search term you specified.
Going one step further, you could then do
:%s/~/replace2
Which replaces your last substitution (in this case, replace1) with replace2.
Unrelated, it may be useful for you to put this in your .vimrc:
set gdefault
Which will make all replaces global by default, so you don't need the /g flag after every :%s command.
You might be looking for vim-over?
This is a plugin that: (to clarify, let's say we're doing :%s/foo/bar/g.) i) highlights matches for substitutions in the buffer (foo) and optionally ii) previews what's after replacement (bar).
I know the regex for doing a global replace,
%s/old/new/g
How do you go about doing an interactive search-replace in Vim?
Add the flag c (in the vim command prompt):
:%s/old/new/gc
will give you a yes/no prompt at each occurrence of 'old'.
Vim's built-in help offers useful info on the options available once substitution with confirmation has been selected. Use:
:h :s
Then scroll to section on confirm options. Screenshot below:
For instance, to substitute this and all remaining matches, use a.
Mark Biek pointed out using:
%s/old/new/gc
for a global search replace with confirmation for each substitution. But, I also enjoy interactively verifying that the old text will match correctly. I first do a search with a regex, then I reuse that pattern:
/old.pattern.to.match
%s//replacement/gc
The s// will use the last search pattern.
I think you're looking for c, eg s/abc/123/gc, this will cause VIM to confirm the replacements. See :help :substitute for more information.
I usually use the find/substitute/next/repeat command :-)
/old<CR>3snew<ESC>n.n.n.n.n.n.n.
That's find "old", substitute 3 characters for "new", find next, repeat substitute, and so on.
It's a pain for massive substitutions but it lets you selectively ignore some occurrences of old (by just pressing n again to find the next one instead of . to repeat a substitution).
If you just want to count the number of occurrences of 'abc' then you can do %s/abc//gn. This doesn't replace anything but just reports the number of occurrences of 'abc'.
If your replacement text needs to change for each matched occurrence (i.e. not simply choosing Yes/No to apply a singular replacement) you can use a Vim plugin I made called interactive-replace.
Neovim now has a feature inccommand which allows you to preview the substitution:
inccommand has two options:
set inccommand=split previews substitutions in a split pane
set inccommand=nosplit previews substitution in the active buffer
Image taken from: https://medium.com/#eric.burel/stop-using-open-source-5cb19baca44d
Documentation of the feature: https://neovim.io/doc/user/options.html#'inccommand'
How can I find/replace all CR/LF characters in Notepad++?
I am looking for something equivalent to the ^p special character in Microsoft Word.
[\r\n]+ should work too
Update March, 26th 2012, release date of Notepad++ 6.0:
OMG, it actually does work now!!!
Original answer 2008 (Notepad++ 4.x) - 2009-2010-2011 (Notepad++ 5.x)
Actually no, it does not seem to work with regexp...
But if you have Notepad++ 5.x, you can use the 'extended' search mode and look for \r\n. That does find all your CRLF.
(I realize this is the same answer than the others, but again, 'extended mode' is only available with Notepad++ 4.9, 5.x and more)
Since April 2009, you have a wiki article on the Notepad++ site on this topic:
"How To Replace Line Ends, thus changing the line layout".
(mentioned by georgiecasey in his/her answer below)
Some relevant extracts includes the following search processes:
Simple search (Ctrl+F), Search Mode = Normal
You can select an EOL in the editing window.
Just move the cursor to the end of the line, and type Shift+Right Arrow.
or, to select EOL with the mouse, start just at the line end and drag to the start of the next line; dragging to the right of the EOL won't work.
You can manually copy the EOL and paste it into the field for Unix files (LF-only).
Simple search (Ctrl+F), Search Mode = Extended
The "Extended" option shows \n and \r as characters that could be matched.
As with the Normal search mode, Notepad++ is looking for the exact character.
Searching for \r in a UNIX-format file will not find anything, but searching for \n will. Similarly, a Macintosh-format file will contain \r but not \n.
Simple search (Ctrl+F), Search Mode = Regular expression
Regular expressions use the characters ^ and $ to anchor the match string to the beginning or end of the line. For instance, searching for return;$ will find occurrences of "return;" that occur with no subsequent text on that same line. The anchor characters work identically in all file formats.
The '.' dot metacharacter does not match line endings.
[Tested in Notepad++ 5.8.5]: a regular expression search with an explicit \r or \n does not work (contrary to the Scintilla documentation).
Neither does a search on an explicit (pasted) LF, or on the (invisible) EOL characters placed in the field when an EOL is selected.
Advanced search (Ctrl+R) without regexp
Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.
I recommend this method as the most reliable, unless you really need to use regex.
As an example, to remove every second newline in a double spaced file, enter Ctrl+M twice in the search string box, and once in the replace string box.
Advanced search (Ctrl+R) with Regexp.
Neither Ctrl+M, $ nor \r\n are matched.
The same wiki also mentions the Hex editor alternative:
Type the new string at the beginning of the document.
Then select to view the document in Hex mode.
Select one of the new lines and hit Ctrl+H.
While you have the Replace dialog box up, select on the background the new replacement string and Ctrl+C copy it to paste it in the Replace with text input.
Then Replace or Replace All as you wish.
Note: the character selected for new line usually appears as 0a.
It may have a different value if the file is in Windows Format. In that case you can always go to Edit -> EOL Conversion -> Convert to Unix Format, and after the replacement switch it back and Edit -> EOL Conversion -> Convert to Windows Format.
It appears that this is a FAQ, and the resolution offered is:
Simple search (Ctrl+H) without regexp
You can turn on View/Show End of Line
or view/Show All, and select the now
visible newline characters. Then when
you start the command some characters
matching the newline character will be
pasted into the search field. Matches
will be replaced by the replace
string, unlike in regex mode.
Note 1: If you select them with the
mouse, start just before them and drag
to the start of the next line.
Dragging to the end of the line won't
work.
Note 2: You can't copy and paste
them into the field yourself.
Advanced search (Ctrl+R) without regexp
Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.
On the Replace dialog, you want to set the search mode to "Extended". Normal or Regular Expression modes wont work.
Then just find "\r\n" (or just \n for unix files or just \r for mac format files), and set the replace to whatever you want.
I've not had much luck with \r\n regular expressions from the find/replace window.
However, this works in Notepad++ v4.1.2:
Use the "View | Show end of line" menu to enable display of end of line characters.
(Carriage return line feeds should show up as a single shaded CRLF 'character'.)
Select one of the CRLF 'characters' (put the cursor just in front of one, hold down the SHIFT key, and then pressing the RIGHT CURSOR key once).
Copy the CRLF character to the clipboard.
Make sure that you don't have the find or find/replace dialog open.
Open the find/replace dialog.
The 'Find what' field shows the contents of the clipboard: in this case the CRLF character - which shows up as 2 'box characters' (presumably it's an unprintable character?)
Ensure that the 'Regular expression' option is OFF.
Now you should be able to count, find, or replace as desired.
Image with CRLF
Image without CRLF
The way I found it to work is by using the Replace function, and using "\n", with the "Extended" mode. I'm using version 5.8.5.
In 2013, v6.13 or later, use:
Menu Edit → EOL Conversion → Windows Format.
To find any kind of a line break sequence use the following regex construct:
\R
To find and select consecutive line break sequences, add + after \R: \R+.
Make sure you turn on Regular expression mode:
It matches:
U+000DU+000A -CRLF` sequence
U+000A - LINE FEED, LF
U+000B - LINE TABULATION, VT
U+000C - FORM FEED, FF
U+000D - CARRIAGE RETURN, CR
U+0085 - NEXT LINE, NEL
U+2028 - LINE SEPARATOR
U+2029 - PARAGRAPH SEPARATOR
Assuming it has a "regular expressions" search, look for \r\n. I prefer \r?\n, because some files don't use carriage returns.
EDIT: Thanks for the feedback, whoever voted this down. I have learned that... well, nothing, because you provided no feedback. Why is this wrong?
Use the advanced search option (Ctrl + R) and use the keyboard shortcut for CRLF (Ctrl + M) to insert a carriage return.
If you need to do a complex regexp replacement including \r\n, you can workaround the limitation by a three-step approach:
Replace all \r\n by a tag, let's say #GO# → Check 'Extended', replace \r\n by #GO#
Perform your regexp, example removing multiline ICON="*" from an html bookmarks → Check regexp, replace ICON=.[^"]+.> by >
Put back \r\n → Check 'Extended', replace #GO# by \r\n
Go to View--> Show symbol-->Show all character
// Its worked for me
Make this setting. Menu-> View-> Show Symbol-> uncheck Show End of the Line
I opened the file in Notepad++ and did a replacement in a few steps:
Replace all "\r\n" with " \r\n"
Replace all "; \r\n" with "\r\n"
Replace all " \r\n" with " "
This puts all the breaks where they should be and removes those that are breaking up the file.
It worked for me.
I was totally unable to do this in NP v6.9.
I found it easy enough on Msoft Word (2K).
Open the doc, go to edit->replace.
Then in the bottom of the search box, click "more" then find the "Special" button and they have several things for you. For Dos style, I used the "paragraph" one. This is a cr lf pair in windows land.
Just do a \r with a find and replace with a blank in the replace field so everything goes up to one line. Then do a find and replace (in my case by semi colon) and replace with ;\n
:)
-T&C
To change a document of separate lines into a single line, with each line forming one entry in a comma separated list:
ctrl+f to open the search/replacer.
Click the "Replace" tab.
Fill the "Find what" entry with "\r\n".
Fill the "Replace with" entry with "," or ", " (depending on preference).
Un-check the "Match whole word" checkbox (the important bit that eludes logic).
Check the "Extended" radio button.
Click the "Replace all" button.
These steps turn e.g.
foo bar
bar baz
baz foo
into:
foo bar,bar baz,baz foo
or: (depending on preference)
foo bar, bar baz, baz foo
Maybe you can use TextFX plugins
In TextFX, go to textfx edit → delete blank lines