Sublime Text 2, faster alternative to find and replace regex? - sublimetext2

$_POST['daily_limit'];
$whatever = $_POST['smoke'];
$_POST['soup'] + $_POST['cake'];
to
$this->input->post('daily_limit');
$whatever = $this->input->post('smoke');
$this->input->post('soup') + $this->input->post('cake');
In this example, is there any faster way to switch from $_POST[] to $this->input->post() without writing up a regular expression find and replace? I don't care if it takes multiple steps. Writing the regex for this (find: \$_POST\[(.*?)\] replace: \$this->input->post\($1\)) takes longer than changing them all manually (maybe I'm just not good at regex). Any ideas?

I'm making a brash assumption here, that you have only one variable within each pair of brackets and that the variables only contain alphanumeric characters. ['soup'+'bacon'] will break this trick, as will ['soup-with-bacon'].
With your cursor, highlight an instance of $_POST[ - nothing else.
Hit Alt+F3 if you're on Windows/Linux (Cmd+ShiftG in Mac?)
Try to scroll through and see if everything that's selected is everything you want to replace.
Type $this->input->post( - nothing else.
Press → to move all cursors to the right of the first quote.
Press Ctrl+→ (this is the only remotely wtfh4xxy part of the process, and only if you're not used to navigating by word with the cursor) to navigate over the variable.
Press → twice to move all cursors to the right of the next quote.
Replace the ]with a ).

#nnnn I did a variation of your version to remove the wtfh4xxy part.
select:$_POST
altf3
type: $this->input->post(
ctrlshiftm
ctrlx
ctrlshiftm
ctrlv
type: )
Sublime text ftw!

Related

Fold / Collapse the except code section in sublime text 2

Is there any plugin or shortcut to hide all except code section in sublime text 2?
I need to fold all except section at a time , Not fold one section at a time.
Thanks~
If you'll hover with the mouse over the line numbers you'll see arrows - clicking on any of them will fold/collapse the code
If you want to collapse/expand all - you can do so by going to edit->code folding and choose "fold all" or "unfold all":
In addition to the other answers it is also possible to fold based on level as well. So for example looking at the default key bindings for fold.
Searching for fold key bindings.
So for example a foldall, or folding level 1 would be to hold Ctrl followed by pressing the sequence k and then 1:
Or folding level 2 would be to hold Ctrl followed by pressing the sequence k and then 2:
Or unfolding all would be would be to hold Ctrl followed by pressing the sequence k and then 0 or in my defaults I also seem to have it bound to the letter j:
Warning.
Pressing Ctrl+k twice will remove a line or a count of lines.
But not really cause you can put them back one by one by Ctrl+u
One thing you can do is select the Except code bloc using a regular expression, for instance using except(.|\n)*?raise.* in your case. You can then select "Find all" in the search bar, then Edit->Code Folding -> Fold .
Windows shortcut : Ctrl-Shift-[
Mac shortcut: Cmd-Alt-[
All the Except bloc will then be collapsed.
I know this is an old question, but it still comes up high in search results and none of the answers quite do what the OP wanted.
select the code you don't want to be hidden
use "Selection" -> "Invert Selection" to select the code you do want to be hidden instead
use ctrl + shift + [ or Command + Option + ] to collapse the selection(s)
This will leave you with just the code you originally had selected visible.
Fold and UnFold function or class base only for MAC:
* Fold: command + K, command + 1
* UnFold: command + K, command + J

Shortcut to paste multiple lines - Sublime Text 2

I am using Windows 8 OS
I have some projects where I repeatedly add the same tags to different types of elements, but the format of how the elements are presented through code always stays the same. I'm looking for shortcuts that will help me do these tasks quickly. Is there a shortcut that lets you add the same tag for multiple lines that you specify? I know you can do (CTR + F3) To select clone tags and change all of them, but I want to add tags to elements that previously had no tag. Is there a way you can make your own shortcuts for your tags, like if I type in "li" It will automatically put in "" and all I have to do is hit enter?
Here is an example of the elements and tags I added:
<ul>
<li type="square">Extra Grip
<li type="square">Made of Titanium
<li type="square">Built in Selsoft Processor
<li type="square">Portable</ul>
<b>MBS:</b> 44 kN (10000 lbf)<br>
<b>Weight:</b> 1 lbs 13.2 oz (828 g)<br>
<b>Length:</b> 14.40" (36.6 cm)<br>
<b>Width:</b> 3.75" (9.5 cm)<br>
<b>Height:</b> 1.00" (2.5 cm)<br>
<b>Material:</b> Titanium
Ctrl+C, Ctrl+X and Ctrl+V let you copy/cut/paste lines if you don't select anything. So, Ctrl+X doesn't "delete" a line, it cuts it. To delete a line, the default shortcut is Ctrl+Shift+K :)
Highlighting a bunch of lines then hitting Cmd (Ctrl?) +Shift+L gives you multi-cursors on each line. I do that, followed by Cmd + Right arrow (End?) to get easily get a cursor at the end of a series of lines to simultaneously type something after each.
Ctrl+Shift+J expands the selection to the indentation level of the current line. So if you want to select a block of code with the same indentation it's really useful.
Alt + F3 select all occurrences of current word for multiple editing. Very useful.
A few written about in more detail: http://whiletruecode.com/post/7-handy-text-manipulation-tricks-sublime-text-2
Have you tried to make your own snippets? It may not be exactly what you are asking for, but could be another way to do it.
Try the New Snippet command in the Tools-menu and add the following and save it:
<snippet>
<content><![CDATA[
<li type="square">${1:Item} ${2:}
]]></content>
<tabTrigger>li</tabTrigger>
</snippet>
This will enter an <li>-tag in the current file if you type li and then press Tab.
You can also add a <scope> tag to limit it to HTML-files.

Why doesn't the cursor stay at the same place?

I have a very strange problem which I can't resolve after trying the whole morning.
This is my function and mapping:
nnoremap ,zz :call SwapAW("2-2","5")<CR>
nnoremap ,zc :call SwapAW("2-2","5")<CR>
function! SwapAW(keyw,number)
let li = line('.')
let co = col('.')
exe "call cursor(li,co)"
let linew= line('.')
let conew= col('.')
echo linew conew
endfunction
,zc --> moves the cursor to the right
,zz --> the cursor stays where it is
I changed everything but can't find out why the cursor doesn't stay where it is in both cases.
I changed p.e. the mapping, from ,zc to ,zd and ,zz to ,zw (tried others as well) and changed the leaderkey from , to \, and tried to swap both mapping lines.
Nothing changes.
What's happening?
What did I wrong?
Thanks in advance.
You have a trailing <Space> character in your ,zc mapping (after the <CR>). That moves the cursor after the function invocation.
With mappings, one must be careful with hidden characters. Therefore, it's advisable (and sometimes, e.g. at the beginning of a mapping, necessary) to literally write the special form <Space> when you actually need to include one.
My ShowTrailingWhitespace plugin will highlight those trailing spaces and tabs, as many code guidelines frown on them. The plugin page also contains links to alternatives.

Deleting entire function definition in Vim

I've been trying Vim for any text editing work for almost a week now. I want to know the fastest way to select a C function definition.
For example, if I have a function like this:
void helloworlds( int num )
{
int n;
for ( n = 0; n < num; ++n ) {
printf( "Hello World!\n" );
}
}
How would I be able to delete the whole definition including the function name?
As is common in Vim, there are a bunch of ways!
Note that the first two solutions depend on an absence of blank lines.
If your cursor is on the line with the function name, try d}. It will delete everything to the next block (i.e. your function body).
Within the function body itself, dap will delete the 'paragraph'.
You can delete a curly brace block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).
You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter
]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect. In addons like Python-mode, these operators are redefined to mean exactly what you're looking for: move from function to function.
How to delete the whole block, header included
If you're on the header/name, or the line before the block, da} should do the trick.
If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.
Plugins
I don't do much C programming in Vim, but there are surely plugins to help with such a thing. Try Vim Scripts or their mirror at GitHub.
To delete an entire function, including its definition, such as:
function tick() {
// ...
}
Move to the line with the function name.
Move the cursor to the opening brace, f{ should do it, or simply $.
Press V%d (Visual line, move to matching pair, delete)
If your functions look like this:
function tick()
{
// ...
}
Move to the line with the function name.
Press J (join the current line with line bellow. This also puts your cursor at the last character on the resulting line, {, just the one we need for the next command.)
Press V%d (Visual line, move to matching pair, delete.)
or
Move to the line with the function name.
Press V[Down]%d (Visual line, move one line down, move to matching pair, delete.)
If you are willing to install plugins vim-textobj-function will give you vif for Visual select Inside Function and vaf for Visual select A Function.
daf will delete the function, both the line with the signature and the function body ({})
The text object defined by this plugin are more specific and they don't rely on the function body being a contiguous block of text or { being placed at the first character on the line.
The drawback is that you depend on an external plugin.
You can use this shortcut to delete not only the function, also the lines between curly braces, i.e the code between if-else statements,while,for loops ,etc.
Press Shitf + v [Will get you in visual Mode] at the curly brace start/end.
Then Press ] + } i.e ] + Shitf ] - If you are in start brace.
Then Press [ + { i.e [ + Shitf [ - If you are in end brace.
Then DEL to delete the lines selected.
The simplest and most direct way way is as follows (works anywhere inside function):
v enter visual mode
{ move to first brace in function (may have to press more than once)
o exchange cursor from top to bottom of selection
} extend selection to bottom of function
d delete selected text
The complete command sequence would be v{o}d. Note that you can do other operations besides delete the same way. For example, to copy the function, use y (yank) instead of d.
Use this simple way
1.Go to the function definition
2.dd - delete function definition
3.d -start delete operation
4.shift+5(%) - delete the lines between { to }
If your function were separated by the blank lines, just type:
dip
which means "delete inner paragraph".
Another way is to go to the line of the start of your function and hit: Vj% (or V%% if your style puts the opening brace on the same line). This puts you into Visual-Line mode and the percent takes you to the matching closing brace. In the second style, the first % takes you to the opening brace on the line that you selected and the second to its matching closing brace.
Also works for parentheses, brackets, C-style multi-line comments and preprocessor directives.
See the manual for more info.
Pre-condition: be somewhere inside the function.
Go to the previous closing curly bracket on the first line using
[]
Then delete down to the next closing curly bracket on the first line using
d][
Most posted methods have a downside or two. Usually, when working withing a class definition of some object oriented language, you might not have an empty line after the function body, because many code formatters put the closing braces of last method and class on consecutive lines. Also, you might have annotations on top of the function. To make matters worse, there might be empty lines within your function body. Additionally you'd prefer a method that works with the cursor anywhere within the function, because having to move it to a specific line or worse, character, takes valuable time. Imagine something like
public class Test {
/* ... */
#Test
public void testStuff() {
// given
doSetup();
// when
doSomething();
// then
assertSomething();
}
}
In this scenario, vap won't do you any good, since it stops at the first empty line within your function. v{o} is out for the same reason. va{V is better but doesn't catch the annotation on top of the method. So what I would do in the most general case is va{o{. va{ selects the whole function body (caveat: if your cursor is within a nested block, for instance an inner if statement, then you'll only get that block), o puts the cursor to the beginning of the selection and { selects the whole paragraph prepending your selection. This means you'll get the function definition, all annotations and doc comments.
the most easy way I found is:
Get to the line where the function starts and do this: ^^vf{% to mark the entire function and then whatever you like.
^^ - start of the line
v - start visual mode
f - jump to the next search character
{ - this is the search character
% - jump to the closing brackets
This is also very logical after you have used it a few times.
non-visual way:
d/^}/e
... delete by searching for } at line beining, including it for deletion.
without /e (not mentioned in above answers), solution is incomplete.
with /e - searching goes to end of match, so closing bracket is included, and command is well for yanking too:
y/^}/e
if you use neovim version :>0.5
the modern way is to use treesitter and build your model, then you can be selected or yanked or deleted...
Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited
I suggested this video on youtube to learn how to use treesitter to build your model : Let's create a Neovim plugin using Treesitter and Lua
I tried all the top answers here, but none of them works except the one by Nick which suggests to press f{ to get to the opening curly brace. Then V%d to delete the whole function.
Note that, the whole function gets yanked, so you can paste it elsewhere. I come across this use-case frequently, especially when moving if blocks inside another.
I use this map. It work for me
"delete function definition
"only support function body surround by {}
nnoremap <LEADER>df {v/{<cr>%d

How do I best display CheckBoxes in SQL Server Reporting Services?

One of the many quirks of Reporting Services we've run across is the complete and utter lack of a CheckBox control or even something remotely similar.
We have a form that should appear automatically filled out based on information pulled from a database. We have several bit datatype fields. Printing out "True" or "False" just looks silly, as this is supposed to look like a form that has been auto-filled out, so we want to have a series of checkboxes and labels that are either checked or unchecked.
We are running SSRS 2005 but I'm not aware of SSRS 2008 having added a CheckBox control. Even if it did, we'd need to have an alternative for the time being. The best we've found so far is:
use Wingdings
use images
use text boxes with borders and print a blank/space or a capital X
All three approaches require IIF expression shenanigans.
The Wingdings approach seemed to work acceptably, and was the most aesthetically pleasing except that for whatever reason it didn't always print correctly. More importantly, PDF exports, also for whatever reason, converted all fonts (generally) to Arial and so we got funky letters instead of the Windings dingbats.
Images, being a pixel-based raster, don't do so well when printed along side vector stuff like text. Unless handled carefully, they tend to stretch, pixelate, and do other unprofessional looking things.
While these methods do work (some with limitations as mentioned above) none of them are particularly elegant.
Are we missing something obvious? Not so obvious? Does someone at Microsoft have a good reason why such a control was not provided in SSRS 2000, let alone 2 versions and 8 years later? This can't be the first time this issue has come up...
I, along with others in my shop, have used images, toggling the hidden attribute based on the field value (true or false). We haven't had any problems with blurring or scaling, unless we tried to increase the scale of the image beyond 100% obviously.
Another option I've used is similar to the wingdings idea, but I just use a plain old "X". On our forms at least, it is not uncommon for someone to use an X in a box instead of a check mark, so it looks completely acceptable. Plus, you don't have to worry about strange characters when printing.
As for why Microsoft does not include a checkbox control, I can't answer that as I've been wondering the same thing myself for a long time now.
I just wanna share the idea on this blog. SSRS: How to Display Checkbox on Report
First create a textbox
Then change the font family to Wingdings
Insert an expression on the textbox and write this expressions.
=IIF(Fields!Active.Value,chr(254),"o")
Fields!Active.Value could be anything from your query that should return a boolean value 1 or 0.
Then click Preview and see the checkbox ;)
More styles can be selected on the blog that I shared above.
Here is an example of my output
What I have used to display a check box (or ballot box):
1- create textbox (that will become your check box)
2- change font to Arial Unicode MS
3- in the expression window use:
ChrW(&H2611) for a filled-in checkbox
ChrW(&H2610) for an empty checkbox
Besides the different methods already presented, as of SQL Server 2008 R2 there's a built-in control that can be used for checkbox-alike functionality: the Indicator!
Have a look here for details on how to use it: https://web.archive.org/web/20190916105459/http://blog.hoegaerden.be/2012/08/04/displaying-checkboxes-in-an-ssrs-report/
To be able to use a field of type bit, you'll have to cast it to int first. This can be done either in the dataset query or by adding a calculated field to the dataset.
If you want the NULLs to come up as yellow, then you'll need to build the expression that way so it takes that requirement into account as well.
Here's a possible expression for a calculated field:
=Switch(
IsNothing(Fields!YourBoolean.Value), 50,
Fields!YourBoolean.Value = False, 0,
Fields!YourBoolean.Value = True, 100)
Depending on the meaning of your fields - is False good or bad - you may need to swap the zero and 100.
Another way to do thisd is go to "Placeholder properties" of TextBox and check Html - Interpret HTML tag as styles
Then in the Value - Expression put this line of code for checked:
="<font face=""Wingdings 2"" color=""green"">" & Chr(81) &"</font>" & "some other text"
Or this code sample for unchecked:
="<font face=""Wingdings 2"" color=""red"">" & Chr(163) &"</font>" & "some other text"
This way you can have checkbox and text in the same textbox.
Later edit:
If you are having problem displaying Wingdings 2 on Azure, then use Wingdings.
Apparently it works.
="<font face=""Wingdings"" color=""green"">" & Chr(253) &"</font>" & "some other text"
Or this code sample for unchecked:
="<font face=""Wingdings"" color=""red"">" & Chr(168) &"</font>" & "some other text"
You can also use a string calculated field like "[X]" or "[ ]". It's less pretty than the textbox with border but you don't have to put a specific control for the value and you can fill table or matrix with this.
At least there is some solution for the checkbox. I'm still looking for full justification for my text (In fact I'm looking for another solution than SSRS know).
ACCESS 97 could make this kind of thing but not SQL SERVER 2012.
I think there is a bug with SSRS and embedding font characters above 128 (some thing todo with ANSI encoding). Basically you can use 1-128 fine, the rest show up as tall rectangular blocks.
I like NY's idea of the textbox with a border and an optional X - this sounds simple and effective.
This is building on Dragos Durlut's answer. I don't have a high enough reputation to comment but I can answer...
I needed a checkbox as part of text that is passed as a parameter. The parameter contains HTML and is used in a placeholder set up just like Dragos suggests: HTML - Interpret HTML tags as styles.
Instead of having to switch between the HTML and the strings, you can use the HTML Escape Codes (& + # + CharCode + ; --> ¨)
="<font face='Wingdings'>¨</font> Empty checkbox"
Since mine is a parameter, it just pass in the string:
<font face='Wingdings'>¨</font> Empty checkbox
If you need the checkbox selected, you would pass in either ý or þ instead:
<font face='Wingdings'>ý</font> filled with an x
<font face='Wingdings'>þ</font> filled with a checkmark