removing m_ prefix from variable names - regex

Initially we planned to have old convention of having m_ prefix for class variables. But now requirement has come to replace all the m_VaribaleName to this.variableName, i.e. remove the m_ and make the first character after m_ lowercase.
I can search and replace m_ with this. but this doesn't rename the variable's first character to lowercase. After search and replace if I use re-factoring tool to rename VariableName to variableName this also renames the property already exists with VariableName.
I am wondering is there any regex, tool, macro to make this task automated.

Resharper will highlight all such errors in a solution, and fix them individually, but I don't think it can fix all of them with a single command. Still, it's easy enough to navigate between errors that it finds.

You can do this in emacs.
Open your file (C-x C-f) and do M-x replace-regexp.
Let's say the name of the variable is Variable.
Your regexp query would replace \(V\)ariable for \,(downcase \1)ariable.
The \, tells emacs the following statement is a lisp expression.
Additionally, if you wanted to replace the m_ at the same time you could do replace m_\(V\)ariable for \,(downcase \1)ariable.
This will take care of all instances in a file at the same time and emacs does remember your last replace-regexp query so you do not have to retype it for multiple files. Furthermore, there is a way using DIRED mode to do the replace-regexp on multiple files at the same time.
Open up a directory on DIRED mode (C-x C-f DIR_NAME), mark the files you want by going over them (you can navigate using n and p) by pressing m. Once the files you want to process are marked press Q (capital-case). You will get the same regexp prompt as if you did a single file, enter the regexp, RET, the replacement, and RET. It will open up each file at a time, press ! for every file to replace all reg-exps in that file.
After that, you still have to save the files. Which you can do with C-s and then !.

Related

Sublime C++ namespace keyword color scheming

I am having an annoying behavior in Sublime.
When I start typing out the line...
using namespace SomeNamespace;
The keywords 'using' and 'namespace' are properly colored the keyword coloring. Then when I add the semicolon to the end of the line, the namespace keyword goes white (default text color). I know this is not that significant, but it really annoys me.
Has anyone noticed this behavior before? The code compiles without errors or warnings, so I know sublime is not detecting some so of code problem.
Does anyone have any suggestions on how to fix this problem?
The problem is in this particular regex in the C++ syntax definition:
\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(;|=|,))
At the very end, in the negative lookahead - (?!...) - we see that semicolons are excluded from the match, meaning that if a semicolon is present at the very end of the line, there's no match.
To fix it, you'll need to install the very useful PackageResourceViewer plugin from Package Control. Then, open the Command Palette, type prv to bring up the PackageResourceViewer options, select the Extract Package one, then scroll down and select C++. There will now be a C++ directory in the directory opened by choosing Preferences -> Browse Packages.... Go into that directory and you'll see a bunch of files. Depending on what version of Sublime Text 3 you're using, you'll want to open either C++.tmLanguage or C++.sublime-syntax in Sublime. The .tmLanguage format is XML, so you can pick that for syntax highlighting if you wish, while the .sublime-syntax file is in YAML.
Once the appropriate file is open (you'll either have one or the other, not both), search for the regex above, or just search for namespace, you should find it pretty easily. Delete the ;| from near the end, making the whole thing:
\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(=|,))
Save the file, and that's it! Your C++ source files should update their behavior immediately - if not, just close and reopen them, and in the worst case you can just close them, restart Sublime, then reopen them.

Using locals to construct a path in Stata

Suppose I have a file called hello.txt in a directory c:\test\, then the following runs without problems in Stata:
local folder "c:\test"
confirm file "`folder'\hello.txt"
but the following won't
local folder "c:\test"
local file "hello.txt"
confirm file "`folder'\`file'"
How do I combine more than one local into a path like c:\test\hello.txt that can be used in e.g. the confirm file command?
Thanks in advance.
The problem is that \ can act both as escape character and as part of a Windows file path.
When you type something in Stata that contains a local macro, then Stata's first action is to evaluate that macro, i.e. look at its content and replace the macro with that content. Sometimes (rarely, but it can happen) you want to prevent that. That is what an escape character is for.
So what Stata sees in your second example is
c:\test`file'
which is not a valid path.
The easiest solution is to use a / instead of a \, which results in a valid path as far as Stata is concerned and that won't act as an escape character.
For more see:
Nicholas J. Cox (2008) Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3): 446--447.
http://www.stata-journal.com/article.html?article=pr0042

Can Notepad++ save out search results to a text file?

I need to do quite a few regular expression search/replaces throughout hundreds and hundreds of static files. I'm looking to build an audit trail so I at least know what files were touched by what searches/replaces.
I can do my regular expression searches in Notepad++ and it gives me file names/paths and number of hits in each file. It also gives me the line #s which I don't really care that much about.
What I really want is a separate text file of the file names/paths. The # of hits in each file would be a nice addition, but really it's just a list of file names/paths that I'm after.
In Notepad++'s search results pane, I can do a right click and copy, but that includes all the line #s and code which is just too much noise, especially when you're getting hundreds of matches.
Anyone know how I can get these results to just the file name/paths? I'm after something like:
/about/foo.html
/about/bar.html
/faq/2012/awesome.html
/faq/2013/awesomer.html
/foo/bar/baz/wee.html
etc.
Then I can name that file regex_whatever_search.txt and at the top of it include the regex used for the search and replace. Below that, I've got my list of files it touched.
UPDATE What looks like the easiest thing to do (at least that I've found) is to just copy all the search results into a new text file and run the following regex:
^\tLine.+$
And replace that with an empty string. That'll give you just the file path and hit counts with a lot of empty space between each entry. Then run the following regex:
\s+\n
And replace with:
\n
That'll strip out all the unwanted empty space and you'll be left with a nice list.
maybe you need power of unix tools
assume you have GNUWin32 installed in c:\tools\gnuwin32
than if you have replace.bat file with that content:
#echo off
set BIN=c:\tools\gnuwin32\bin
set WHAT=%1
set TOWHAT=%2
set MASK=%3
rem Removing quotes
SET WHAT=###%WHAT%###
SET WHAT=%WHAT:"###=%
SET WHAT=%WHAT:###"=%
SET WHAT=%WHAT:###=%
SET TOWHAT=###%TOWHAT%###
SET TOWHAT=%TOWHAT:"###=%
SET TOWHAT=%TOWHAT:###"=%
SET TOWHAT=%TOWHAT:###=%
SET MASK=###%MASK%###
SET MASK=%MASK:"###=%
SET MASK=%MASK:###"=%
SET MASK=%MASK:###=%
echo %WHAT% replaces to %TOWHAT%
rem printing matching files
%BIN%\grep -r -c "%WHAT%" %MASK%
rem actual replace
%BIN%\find %MASK% -type f -exec %BIN%\sed -i "s/%WHAT%/%TOWHAT%/g" {} +
you can do regex replace in masked files recursively with output you required
replace "using System.Windows" "using Nothing" *.cs
The regulat expression I use for this kind of problem is
^\tLine.[0-9]*:.
And it works for me
This works well if you have Excel available and want to avoid using regular expressions:
Ctrl+A to select all the results
drag & drop the selected results to Excel
Create a Filter on the 1st row
Filter out the lines that have "(Blank)" on the 1st column
Select the remaining lines (i.e. the lines with the filenames) and copy/paste them to another sheet or any wanted destination
You could also Ctrl+A, Ctrl+C the search results, then use the Paste Option "Use Text Import Wizard" in Excel, say that the data is "Fixed width" and put one single break line after the 2nd character (to remove the two leading spaces in the filename during import), and use a filter to filter out the unwanted rows.

Show all matching grep results in new tab in Vim

I am attempting to map a hotkey to do the following in Vim:
Match the current c-word currently marked by the cursor
Generate a list of all found occurrences in current project
Open a new tab showing the results
So, one example I have that uses CTAGS opens the declaration of a variable/function in a new tab like so:
map <C-\> :split<CR>:exec("tag ".expand("<cword>"))<CR>
When it hit CTRL-\, a new tab is opened with the declaration of the variable/function the cursor is on.
The command I am trying to map is shown below:
:lvim /\<\(text_i_want_to_find\)\>/gj *.c
:lw
When I run this command, a new tab is opened showing a list of all .c files containing the text text_i_want_to_find. I need to modify this to do two things different from what it does now:
Search all files with extensions of .c, .h, .cpp, .mk, instead of just .c, and also search files named "Makefile"
Search for the c-word under the cursor like the CTRL-\ mapping I have shown above, as opposed to having to manually type in the text text_i_want_to_find
Here is the code in my .vimrc file for the mapping. I'm not entirely sure if CTRL-/ can be mapped at all, so there's one more problem for me to solve.
map <C-/> :split<CR>:lvim /\<\(.expand("<cword>")\)\>/gj *.c *.h *.cpp *.mk Makefile
Does anyone have any tips on fixing this Vim mapping?
EDIT:
After reviewing the answers provided to me, here's the final version of my code:
command! -nargs=1 SearchAll execute " grep -srnw --binary-files=without-match --exclude={*~,tags} --exclude-dir=.svn . -e " . expand("<args>") . " " <bar> cwindow
map <C-g> :SearchAll <cword><CR>
I mapped it to CTRL+g. I can also invoke it like so as a colon command:
:SearchAll my_text_to_search_for
Hope this helps others as well!
Of course, it does not work: you forgot :execute, :lvimgrep command on its own does not accept expressions, so you are searching for expand("<cword>") that is not followed by a keyword character (\>) and preceded by any character that does not follow keyword character (\<.). \( and \) are useless here. Other notes:
Don’t use map: you need neither this mapping working in visual and operator-pending modes nor ability to use other mappings.
Some patterns can be merged: *.[ch] *.cpp *.mk Makefile or even *.{[ch],cpp,mk} Makefile.
<C-\> can be mapped, but not <C-/> (I hope only currently, but no real work on fixing it is done): \ is 0x5C and <C-\> is 0x5C-0x40=0x1C. But / is 0x2F and 0x2F-0x40<0. On my system pressing <C-/> in terminal transforms into <C-_>, so does <C-->.
:split command does not open a new tab, it opens a new window. Prepend it with :tab to do this.
:lvimgrep is not going to show you the results. It is not either going to jump to the first result unless you remove the j flag.
You forgot final <CR> as well as :lw<CR>. I use :lopen below, in this context they produce just the same result.
You forgot <C-u> after first : or <C-\><C-n> before: it is needed to discard count.
You don’t really need :execute in either of your mappings because there is <C-r>=expand("cword")<CR>, or a default shortcut <C-r><C-w>.
Don’t write :exec(str): it is confusing because :execute is not a function and this syntax encourages others to think it is. Use :execute str.
Thus the final mapping is:
nnoremap <C-\> :<C-u>tab split \| lvimgrep /\V\<<C-r><C-w>\>/gj *.{[ch],cpp,mk} Makefile \| lopen<CR>
. Replace it with
nnoremap <C-\> :<C-u>lvimgrep /\V\<<C-r><C-w>\>/gj *.{[ch],cpp,mk} Makefile \| tab lopen<CR>
if you don’t want to see the current file in a newly opened tab.
Both mappings assume you have neither / nor \ in 'iskeyword' option, if you do replace <C-r><C-w> with <C-r>=escape(expand('<cword>'), '/\')<CR>.

Filename extraction with regex

I need to be able to only extract the filename (info.txt) from a line like:
07/01/2010 07:25p 953 info.txt
I've tried using this: /d+\s+\d+\s+\d+\s+(?.?)/, but it doesn't seem to work ...
How about
/\S+$/
I.e. the longest possible string of non-whitespace at the end of the line.
(Hard to know for sure without more info about the possible inputs.)
As #J V pointed out, filenames with spaces in them (like his username) will not be parsed properly by the above regexp. We don't know from the question whether that's possible.
But I have a suspicion that we're looking at the output of Windows DIR command, or something very similar. In that case, the most reliable approach might be just to hack off the first 39 characters and keep the rest:
/^.{39}(.+)$/
Then $1 will contain the filename.
Better option:
But if you are using Windows DIR (as per your new comment), and you can control the DIR command, try
DIR /b
which removes the unneeded cruft (assuming you don't need the date, size etc.) and gives you one filename per line.
OK, you're using a Unix dir (per newer comment). The CentOS dir I have outputs one file per line, nothing else, when you give it no command line options. Chances are very good that whichever dir you're using can be persuaded to output filenames like that... then you wouldn't have to worry about using a regex that may or may not be correct for every possible input. Try man dir or dir --help to find out what command-line options to use.
\d\d:\d\d\w\s+\d+\s+(.*?)$
$1 will be the file name
The problem with your original regex is that it forgets the special characters :, /, and (?.?) means nothing...
Assuming that the files have extension as .txt you can try.
(?<=(\s)*)\w*.txt
Why not just use the following regex:
\w+\.\w+