(I wrote this as a response to an Ask Hacker News post about learning Vim, but I thought it deserved a life of its own.)
This is one of my favourite Vim features. Say you have the following code:
do_something_with(some + long * complicated * expression)
^
Say your cursor is where the caret indicates. Typing ci) (“change inside parens”) in normal mode will:
delete all the text between the two matching parens
place you in insert mode with the cursor between the two (now adjacent) parens
put the deleted text in the yank buffer so that p will paste it.
The use case here is obviously so you can assign a name to that long complicated expression. ci) is much easier than selecting it with the mouse, and keeps your hands on the keyboard where they belong ;)
With nested parentheses, it does what you expect (affects the text contained by the innermost matching pair to contain your cursor - try it and see).
Other equally useful variants:
i" - “inside double quotes” - everything between double quotes
i' - “inside single quotes”
iw - “inside word” - the word the cursor is on
is - “inside sentence” - great for editing prose
ip - “inside paragraph”
There are also similar motions beginning with “a”:
a) - like i) but includes the parens (e.g. da) deletes everything inside parens and the parens themselves)
a" - similarly
aw - like iw but includes trailing whitespace.
For another great taste that goes great with this, see the surround.vim plugin. To whet your appetite: six keystrokes to wrap your current selection in <div> tags; four to change a string from “double-quoted” to ‘single-quoted’.
Don’t let the Government rush draconian internet laws through Parliament before the election: write to your MP! I did.
Dear Meg Hillier,
Thank you for your thoughtful response of 17 December regarding the Digital Economy Bill. I appreciate your taking my concerns on board, and indicating that you shared some of them.
I’m writing today to ask you to demand a full Commons debate on this highly controversial Bill, and not to support Government plans to rush it into law before the election.
In our previous correspondence, I told you about my concerns about the measures in the Bill to tackle illegal file-sharing: that besides their intended effect of incentivising creation, these measures would
stifle the UK’s digital businesses (including my own software consulting business, and the businesses of my clients) by creating a restrictive and uncertain legal environment;
damage British social justice, by exacting disproportionate penalties on a potentially huge number of citizens (and voters), without a required standard of evidence or right to redress for false accusation (points with which you indicated you shared my concern), and without a court trial or presumption of innocence; and
harm British democracy, by building in “reserve powers” for unelected officials to change copyright law without Parliamentary scrutiny, and by allowing massive and largely evidence-free lobbying from special interest groups to unduly influence the legislation.
Especially in light of point 3 above, I have been extremely concerned to read in the press that the Government is likely to force the Bill into law before the upcoming election. It is disturbing that the Government would deny MPs the chance to fully consider the implications. It is downright scary that crucial details of the most controversial proposals might be worked out behind closed doors in the “wash-up”.
As a constituent, I am writing to you today to ask you to do all you can to ensure the Government doesn’t just rush the bill through and deny us our democratic right to scrutiny and debate.
There is no evidence that the need is so pressing as to justify passing bad laws. The content industry lobbyists asking for this hasty lawmaking claim that they urgently need the measures in the Bill to keep their industry alive. This is an extraordinary claim in light of recent news that, in the midst of a recession, online music revenues grew by 73%! The sky is clearly not falling.
The concerns raised by myself and many others are still largely unaddressed, despite the large number of amendments proposed during the debate in the Lords (itself evidence of the importance and contentious nature of this Bill). The proposed legislation will still fail to support authors of copyrighted works, and it will still do great harm to Britain’s digital economy.
I apologise for extending this already long letter, but I would like to close by quoting from a post written today on the LabourList blog:
The bill which is supposed to provide the structure for Britain’s digital future, is currently being opposed by the internet service provders who are central to that future – not because they are worried about their profits, but because they don’t want to police their customers in this way, especially not on the basis of an assumption that has not been proved. Consumers are worried that this clause could have serious effects on their lives. Consumers vote.
Is this clause so important that it is necessary to push it through at any cost? Is it really wise to have this bill become an easily recalled symbol of Labour’s record on civil liberties, and a ‘sidestepping’ of democratic process? This close to this election?
Once again, please demand a full democratic debate on the Digital Economy Bill.
Yours sincerely,
Sam Stokes
The genesis of a building. Witnessing this is pretty cool from a “making things” point of view. Shame it’s happening right outside our flat.
In honour of the Functional Programming eXchange, on a bus to which I am writing this.
(a (program (writtenp :in 'lisp))
(may 'be (and (simple) (elegant) (crisp)))
(but (cry '(C pros) "too *fancy();"
"for (i; simply(); &cant++ + ++see)"
"{ the code; for (;;) { the parenthesis; } }")))
My first Manhattan
Made enchiladas.
My MP, Meg Hillier, was kind enough to respond by post to my email about the proposed “three strikes” legislation in the UK. The main content of her response was to forward me a letter she received from Stephen Timms, the Minister for Digital Britain (his actual job title, I’m not making this up), to “clarify the Government’s position on this issue”.
The forwarded letter mostly just rattled off the party line - illegal file sharing is illegal, artists need to be compensated, the usual unjustified claims of urgency - but it did mention the recent report by the UK Intellectual Property Office, awkwardly entitled © The Way Ahead, which is actually pretty encouraging reading: I will discuss it in a separate post soon.
My previous email focused on problems with the “three strikes” approach. Since the Digital Economy Bill was published we’ve discovered that “three strikes” is only one of several nasty tricks up Mandy’s sleeve. Therefore, and because I felt from her letter that Ms Hillier hadn’t really taken my concerns on board, I wrote back.
Update 2009/12/02 - since the Digital Economy Bill is currently being debated in the House of Lords, I also sent an edited version of this email yesterday to Lord Clement-Jones, who is taking part in this afternoon’s Second Reading debate.
Dear Ms Hillier,
Thank you very much for your swift response to my email of 17th November. Thank you also for forwarding a copy of Stephen Timms’ letter outlining the Government’s position, reasoning and intentions regarding illegal filesharing. I am grateful to him for drawing my attention to the UK Intellectual Property Office’s report © The Way Ahead, which I have read with interest.
Unfortunately, Mr Timms’ letter does not reassure me. On the contrary, it reinforces my perception that the policies set out in the Government’s Digital Economy Bill were drafted in haste, based on the shrill lobbying of special interests rather than empirical evidence and objective reasoning.
I do not believe the available evidence justifies imposing technical sanctions on internet users without trial in court, as the Government proposals would allow. The UKIPO report, published only a month ago, finds that “a lack of strong evidence across the world makes it difficult to establish the case for changes to aspects of copyright law and practice”. Meta-analyses of the economic literature have similarly observed a poor quality of available evidence and a lack of consensus.
Mr Timms’ letter does not even include the word “evidence”; I note with concern that this omission seems to be typical of Government communications on this topic.
This is not the only thing wrong with the Digital Economy Bill. I wrote my previous email before the Bill was published. Since then I have read with alarm section 17, which grants the Secretary of State power to amend by statutory instrument the Copyright, Designs and Patents Act of 1988 “if it appears to the Secretary of State appropriate to do so”. To grant such broad powers to an unelected official is incredible, undemocratic and indefensible.
I acknowledge the problem this “power to amend” is designed to tackle: technology changes too quickly for primary legislation to keep up. However, the case has not been made that this justifies circumventing the democratic process, especially in such an important and controversial area as copyright law. To me it rather suggests, to paraphrase the UKIPO report, that Parliament should amend copyright law to be more technologically neutral.
Therefore I once again urge you to reject the Digital Economy Bill unless it is substantially amended, including the removal of section 17, and the addition of a guarantee of trial in court before technical limitations can be imposed on internet users. I would also appreciate it if you could pass on my concerns to the Minister responsible for the Bill, for example by forwarding him this email.
I am sure that, as a Government minister, you will be under considerable party pressure to support this Bill, and that I am unlikely to convince you. However, I must reiterate that I will not vote for any MP or party who does support it, nor will many of my personal and professional acquaintances.
Finally, I would like to inform you that I am publishing my emails to you on my blog, where my previous email has been read by over 70 people at time of writing. With your permission, I would like to include your responses in these blog posts.
Thank you again for reading this email.
Yours sincerely,
Sam Stokes
Meg Hillier sent a brief reply to this email, stating that she supports the Digital Economy Bill because “filesharing has a big impact on people’s livelihoods”. This is a disappointing response, as it basically ignores the content of this second email. I will continue the correspondence, but as this blog is already more politics-heavy than I intended, I will stop publishing my emails unless there is a noteworthy development.
Reading Patrick McKenzie’s excellent practical example of metaprogramming, I came across a line of code I didn’t understand:
That line taught me three new things about Ruby:
[] allows multiple arguments. (It turns out I already knew this in another context: [1,1,2,3,5,7][2,3] => [2,3,5])"goal"[/[aeiou]/] => "o" (nil is returned if there is no match).n, then you get the nth capturing group of the first match: "xaabb"[/(.)\1/, 1] => "a" (or nil again if no match).That last one is interesting, because it means there’s a concise way I didn’t previously know about to achieve a common regex task: checking if an input string matches a given format, and if so, extracting part of the format. Say we want to pull out the domain from an email address, but complain if we can’t find it:
Before learning this trick I would have either used a temporary match object a la Java, or gritted my teeth and used a global variable Perl-style:
Both of those seem rather verbose. They can be golfed into one-liners, but the readability starts to suffer:
So I’m left wondering what’s the most readable and/or idiomatic style for regexes in Ruby. TMTOWTDI indeed! Even now I know what it means, "xaabb"[/(.)\1/,1] makes me double-take slightly - it’s an unusual way to use [] - but I guess it’s just another Ruby idiosyncracy I’ll come to know and love.
An email I wrote to my MP (using the very handy WriteToThem) regarding the UK Government’s so-called “three strikes” proposals to combat illegal filesharing.
Dear Ms Hillier,
I am writing to ask you to vote against the Government’s forthcoming Digital Economy Bill, unless the currently proposed measures to impair or disconnect without trial the internet connections of those accused of illegal filesharing are removed from said Bill.
Copyright holders are already entitled to initiate private lawsuits against those they believe to be infringing their rights. This of course involves a trial in court, with the accused party presumed innocent until proven guilty. Methods of detecting infringement are known to be unreliable, leading to false accusations, but currently this is the accuser’s problem.
The Government’s plans would remove the inconvenient (to the rights holder) need for a court case to punish the accused. The unreliability of detection would become an issue for every internet user, since these plans do not require proof of infringement, only repeated accusation.
This circumvention of justice is justified only by evidence produced by the rights holders themselves. It ignores evidence that the music industry has actually grown over the last five years, and that filesharing may be an opportunity for the content industries rather than a threat.
I am also very concerned about the precedent that would be set by these proposals. Music and film are not the only industries whose business models have been shaken by the emergence of new technology. The news industry, for example, is considering charging for its previously free online content. Without question people will find ways to circumvent these “paywalls” and access the content for free. Will we then allow news publishers to join in selecting people for disconnection? I myself, as an independent software developer, am a creator of copyrighted works. Can I disconnect people I believe have used my work without permission? If not, why can the BPI do it but not me?
I believe the Government’s plans to be short-sighted in conception, wrong in principle, and likely in practice to do more harm than good. I will vote against any politician who supports them.
I also ask you to sign Early Day Motion 1997, which echoes many of these sentiments.
Thank you for reading.
Yours sincerely,
Sam Stokes
Ms Hillier was kind enough to respond by post to my email. I will not reproduce her response here, as she has not given her consent to publish it; however I have blogged about the further email I sent in reply.
Since the Digital Economy Bill was published between my first email and my second, the latter brings up some new points, in particular regarding the lack of evidence to justify these measures and the broad powers to be given to the Secretary of State.