Shlomif's Technical Posts Community [entries|archive|friends|userinfo]
Shlomif's Technical Posts Community

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| Shlomi Fish's Homepage Main Journal Homesite Blog Planet Linux-IL Amir Aharoni in Unicode open dot dot dot ]

New Vim Plugin: Add to Word Search [Apr. 24th, 2012|10:51 am]

shlomif
[Tags|, , , , ]
[Current Location |Home]
[Current Mood |lazylazy]
[Current Music |Lutricia McNeal - Stranded]

I’ve released a new plugin for the Vim text editor, called "add-to-word-search" ( GitHub repository, Vim Scripts page), and I’d like to introduce it here. If you like Vim, please let me know what you think by commenting below.

In order to properly introduce the plugin, one first should introduce the different (and useful) commands of Vim of * and #. What they do is search forward or backward for the complete word under the cursor (or somewhat before it or after it). Bram Moolenaar (the creator of Vim) covers them in his “Seven habits of effective text editing” document (there’s also a video available), and I think I covered them in a previous Vim tip.

Now, here is the use case that often bugged me: sometimes I searched for a certain function, found it in the text and then found a function that called this function (or often in the case of C code, a preprocessor macro that wrapped it), and wanted to look for its occurrences as well as those of the previous term. I wasn't aware of any good way to do it, so I ended up writing the “add-to-word-search” plugin.

After installing it, and after having searched for a word using * or #, one can press \** to search forward for an additional word under the cursor (or \## to search backward), and then use it more times to add additional words.

After publishing this plugin and mentioning it on #vim, “ironcamel” reported an issue that it gives an error if you have set nowrapscan. I fixed it, but was only able to do so by temporarily disabling nowrapscan, and then enabling it if it was previously enabled. (Apparently, vimscript’s exception-handling cannot handle some of the built-in errors.)

I also demonstrated it to my (now former) co-worker, who had been trying to get used to Vim, and he said it looked useful, but asked if there was an easy way to remove terms from the search query (which there is not at the moment), and I noted it may be a good idea.

Anyway, this Vim plugin is open-source and available under the MIT/X11 licence. Enjoy!

Link2 comments|Leave a comment

Vim Tips: scp URLs, "set tabpagemax" and fixing C indentation [Feb. 18th, 2012|06:24 pm]

shlomif
[Tags|, , , , , ]
[Current Location |Home]
[Current Music |Simple Minds - Don't You Forget About Me (Live)]

Here are some Vim tips I ran into recently. First of all, when opening scp:// URLs, one should use two slashes after the hostname instead of 1, like scp://hostname//home/myuser/foo.txt instead of scp://hostname/home/myuser/foo.txt. I don’t know why that is the case, but it does not work properly without it. It also seems that netrw is buggy as it displays an irritating grey line on the cursor, the syntax highlighting tends to be off and saving a file displays several lines at the bottom.

Another tip is that gvim limits the number of tabs it opens when doing gvim -p [file1] [file2] [file3]. As a result, it is possible that not all files will be opened. If you want to change it you can set set tabpagemax in your .vimrc.

Finally, I noticed that Vim c-indentation tends to indent parameters to functions on subsequent lines using 8 spaces instead of 4 by default. I was able to change it to 4, which is my preference by adding set cinoptions+='(0,W4' to my .vimrc. There is plenty of other nifty stuff available in the cinoptions parameter.

Enjoy!

Link2 comments|Leave a comment

More Vim Tips [Jul. 18th, 2011|12:09 pm]

shlomif
[Tags|, , , , ]
[Current Location |Home]
[Current Music |George Harrison - I got my mind set on you]

Here are some more Vim tips I collected recently:

  1. One can repeat the last t, f, T or F command which moves to the next specified character in a line, or right before that, using ;, and repeat in the opposite direction using ,. I discovered those after starting to read through the vim usr_* manuals.

  2. One can get the Unicode value of the character under the cursor using the :ascii command-line command or the ga normal mode command.

  3. If you want to find how many lines/words/etc. are in a visual block, you can select it using "v" and friends and then type g-Ctrl+G.

  4. Finally, you can pipe the contents of the buffer or a range to an external program and view its output temporarily. So you can say :w !grep 'FOO' | wc -l to find the number of lines containing 'FOO', or :'a,'bw !grep 'FOO' | wc -l within a range.

I should note that one thing that annoyed me lately, is the fact that after I have a visual selection (v or V), I sometimes press u instead of y (because they are so nearby) and so cause the selection to become lowercase instead of yanking it.

Happy Vimming!

Link1 comment|Leave a comment

Perl: Using Vim's snipMate for Perl 5 snippets [Jan. 21st, 2010|06:26 pm]

shlomif
[Tags|, , , , , , , , , ]
[Current Location |Home]
[Current Music |Ronald Jenkees - Let's Ride]

I noticed that there was some Perl 5 code that I had to type or copy-and-paste again and again on many occasions. So I decided to find a way to put it in one place and then recall it. I thought of writing it myself, but then recalled the snipMate snippets extension for Vim (which Peteris Krumins covered in a blog post), which allows that. After a little reading, I was able to prepare the snippets.

I placed the following under ~/.vim/snippets/perl/_slurp.snippet :

sub _slurp
{
	my $filename = shift;

	open my $in, "<", $filename
		or die "Cannot open '$filename' for slurping - $!";

	local $/;
	my $contents = <$in>;

	close($in);

	return $contents;
}

And now I can type "_slurp<TAB>" to recall it. The existing "slurp" snippet in ~/.vim/snippets/perl.snippets is quite evil, with an ugly inline line, typeglobs, and a two args open without a die statement. Thanks, but no thanks.

Afterwards I added the following in ~/.vim/snippets/perl/_ltestb.snippet

	local $Test::Builder::Level = $Test::Builder::Level + 1;

And now I can type "_ltestb" (short for "local Test::Builder") and put this line there to create my own custom Test::More/Test::Builder tests.

Thus, using snipMate, you can create your own short-hands for commonly-used snippets like that.

Link4 comments|Leave a comment

Vim Tip: Finding the First Non-Matching Line from the Cursor [Dec. 30th, 2009|06:47 pm]

shlomif
[Tags|, , , , , , , , , , , , ]
[Current Location |Home]
[Current Mood |productive]
[Current Music |Atomic Kitten - Whole Again]

For a long time I've been viewing and editing vim text files, for example those of the UNIX find output or of archive contents, that contained many consecutive lines of similar data. What I wanted to do is find the line where these consecutive data end where there was something else instead. Among the workarounds I tried was to jump to the end of the file (using G) and then search backwards for the last matching line (which may not be very reliable). But a few days ago I decided that I had enough and sought a better way, and I indeed found one.

You can do it using a negated pattern. If for example you enter / home\/shlomi\/\(Arcs\/\)\@! you will find the first line that contains "home/shlomi" that is not followed by "home/shlomi/Arcs/". For information see :help \@!.

LinkLeave a comment

Random Stuff: Word Count Vim Tip, Mirroring CPAN Using rsync, and Mozbot [Nov. 30th, 2009|11:45 pm]

shlomif
[Tags|, , , , , , , , , , , , , , ]
[Current Location |Home]
[Current Music |Roxette - Run To You (YouTube)]

This is a "random stuff" entry because I'm too lazy to write separate entries for each of the topics.

First of all the vim tip: you can do word count in Vim, by selecting a text using "v" and then typing "g" and then "Ctrl+g". This will display statistics on the text including word count. I always assumed it existed in a form but didn't had to use it until recently when I wrote a scene in a screenplay and wanted to see how long it was. This tip can also be found at ":help word-count".

The second tip is that in order to mirror a mini-CPAN that contains only the most recent releases using rsync instead of minicpan's HTTP or FTP download, one can use mst's mirror.sh in conjuction with the list of CPAN rsync mirrors. I needed to modify mirror.sh slightly in order to mkdir the "work" directory which it requires. I changed:

cd $LOCAL

to:

cd $LOCAL

if ! test -e "work" ; then
    mkdir "work"
fi

Finally, a little "recent hacktivity log": at one point someone on irc.mozilla.org asked me if I could help them with their IRC bot called firebot and yesterday I decided to take a look at its core called "Mozbot", and maybe try to improve it. I took a look and detected many vestiges of "Ancient Perl" there, and other anti-patterns such as long subroutines or use of Net::IRC and decided to work on improving it.

After reading the "Using Mercurial locally with CVS" document on the Mozilla site (first hit in a Google search for "hg cvs") and setting up a "mozbot-shlomif" Mercurial repository to work on it, I submitted my first patch and have done more changes in the mozbot-shlomif repository. The code is not perfect, but at least it has "use strict;", the "-w" flag and the "-T" flag, so it could be much worse. I'd still like to move away from Net::IRC to POE-Component-IRC and maybe even try to port the entire Mozbot to buu's buubot. But it will take some time.

LinkLeave a comment

Source for the "E Text Editor" Was Released [May. 14th, 2009|01:03 pm]

shlomif
[Tags|, , , , , , , , , , ]
[Current Location |Home]
[Current Music |Paul Simon - 50 Way to Leave Your Lover]

Apparently, the source for E Text Editor, a multi-platform Textmate clone was released under a not-entirely-open-source licence. This is good news despite the fact that it is not a fully-open-source licene, because now Textmate-like functionality is available for Linux and other platforms, and not only on the Mac.

I'm still sticking with Vim, though, which is fully cross-platform, open-source and even GPL-compatible.

LinkLeave a comment

Several Vim Tips [Apr. 30th, 2009|12:58 pm]

shlomif
[Tags|, , , , , , , , , , , , , , , ]
[Current Location |Home]
[Current Mood |productive]
[Current Music |Nivel del Mar - Welcome Wilderness]

I have collected several vim tips in the past weeks, so here they are:

  1. One can use the :sp(lit) command to split a window into two. :new splits and starts editing a new buffer. Finally, both of these commands followed by a filename stats editing the specified file in the new viewport.

  2. This tip is long overdue. You can make Vim behave more like a Windows-like editor by adding source $VIMRUNTIME/mswin.vim to your .vimrc. Reportedly, it causes many problems and is not recommended, but I still like to use it, afer all these years.

  3. One can match any char including newline using \_.. The "\_" construct works for other character classes, and allows you to match all of them as well as newline.

    It took me a long time to find that, back when I did, so I'm documenting it here.

  4. After one indents or unindents several lines using "<", ">", "<<", ">>", etc. and the indentation level needs to be promoted or demoted further, one can use the "." (= repeat) command to repeat the shifting again (and again) until it is right.

    I discovered this trick by accident, but immediately found it useful.

Hope all Israelis had a nice Independence Day. Happy Vimming!

Link1 comment|Leave a comment

Reports on Recent Open-Source Meetings [Jan. 10th, 2009|07:26 pm]

shlomif
[Tags|, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]
[Current Location |Home]
[Current Music |Ajax - I see Dead People (LeChuck's Theme from Monkey Island).]

I attended four open-source-related meetings this week. On Sunday, Gabor Szabo, gave a presentation about Padre, the Perl IDE, which he described as a "Notepad with a big ego". The presentation was originally titled "wxPerl programming and Padre, the Perl IDE", but for better or for worse Gabor did not talk about wxPerl at all. The presentation was mostly focused about what Padre can do now, Goals for next releases, and a call for volunteers. I found the most impressive part to be the fact that it had some rudimentary Perl 5-syntax-aware refactorings, like lexically change a variable name.

10-20 people came to the meeting, but many of them left before it was done. (don't know/remember why), and only 4 of us went to the cafe afterwards. Still, a lot of fun was had.

For another review of the meeting see xsawyerx.

Next on Wednesday, I attended the Perl-Haifa meeting. I took the train to Haifa, and after I arrived at Matam, I met a regular Haifux attendee on the way, whom I haven't seen in a long time and we talked about stuff. Not many people came to the meeting, but one or two ended up late.

Uri Bruck presented about Perl in Software Art. He presented a lot of unorthodox art which was software based. I daresay the projects he presented there were more strange than I expected it to be. One problem we had was with the intermittent Wireless LAN connection on his computer, but that wasn't too bad.

After that, Shmuel Fomberg gave a presentation about Data-ParseBinary. The story of the module is interesting. I originally attended a presentation about PyConstruct given by one of its developers on a Python-IL meeting. Then, when I helped Alan Haggai Alavi with the (Perl/CPAN) Archive::Zip TPF grant, I suggested that as part of the grant, he'll write a PyConstruct-based parser for .zip files, to help in analysing different archives and see where Archive::Zip goes wrong. Now Shmuel saw it mentioned in the grant, and liked the idea, so he ported it to Perl as Data-ParseBinary. And then, Data-ParseBinary was good enough that Alan Haggai decided to use it as the parser for the .zip format for his Archive::Zip grant.

Before the presentation, I talked with Shmuel, and thanked him on behalf of Alan. He said that he knew of Alan's use of Data-ParseBinary based on a web-search and that he was his only known "customer". Even Shmuel himself did not make an active use of Data-ParseBinary in production. (But he said he's planning to soon).

In any case, in the talk, Shmuel gave a relatively comprehensive overview of the module's features, and how to use it for many use-cases. One attendee kept asking him questions about why he did things this way instead of the other, and Shmuel answered with the right way to do things. He said that Data-ParseBinary's interface was originally based on Construct's, and so many of the problems the attendee found with it were derived from there. We also had some fun correcting the pronunciation of certain English words.

Data-ParseBinary seems very cool and useful in case you need to analyse or modify binary formats.

On Thursday, I went to a Herzelinux (Linux in Herzeliyah) meeting. Eitan Isaacson gave a presentation about accessibility in GUI software in general and about Accersiser, which is a tool he wrote in order to help test the accessibility of programs. Accersiser basically lived at the lowest layer of the accessibility API and acted as an accessibility aid that shows what is available there. The presentation was interesting, but from my impression, it seems that Accersiser is too crowded with information so one cannot see the forest from the trees.

He mentioned KDE/Qt applications, and said that they don't yet interface with the accessibility framework used by GNOME, Java and Mozilla because it is based on CORBA, and they don't want to have to depend on it. They said they're planning to add a DBUS-based transport, which will hopefully enable Qt and KDE to interface with it as well.

After the Herzelinux talk, someone drove me to the Israeli Ruby meeting at a pub, nicknamed "Beer on Rails". Since I ate a lot of refreshments at Herzelinux, I only ate some spicy fries there (which were very good). I engaged in a few talks there, but was soon left out because I sat in the corner between two conversations that I could not hear well. One talk that I remember was that someone told me he tried to use Vim instead of Emacs, but found it frustrating that while in Insert mode he could not move to the previous or next characters without pressing Escape. I told him to use the cursor keys, but he said he'd rather not because they are too far away. Then I told him that he can bind certain key bindings to do something else while in Insert mode, but he said "Yes, but that would no longer be vi.".

I'm personally not too bothered by this, because I have no problem using the arrow keys for certain tasks, and because I find that when working on editors on other systems, I only do minimal tasks until I can set up an environment that I like like I am used to. But I can relate to such problems.

In any case, I eventually got too tired, paid, and took the bus home. I hope the next meetings will be in a different format, and somewhat earlier during the day.

So it's been a busy week, and a lot of fun was had. Bye for now.

LinkLeave a comment

Three Tips: Perl with Vim, Subversion Problem and mailto: links in Firefox [Oct. 19th, 2008|11:59 am]

shlomif
[Tags|, , , , , , , , , , , , ]
[Current Location |Home]
[Current Music |Monkees - Daydream Believer]

First of all, my home-site's back and you're gonna be in trouble! Otherwise, this entry contains three technical tips that I collected recently:

  1. Perl and Vim - sometimes one encounters a case where he needs to handle many errors in his Perl script. I encountered a similar case, when I had to work on a script that was written without "use strict;" and "use warnings;" and shouted at me senselessly when I added them.

    To greatly help with that install perl-support, and look at its Perl → Run menu - there's "update, run script" (Ctrl+F9) and "update, check syntax) (Alt+F9) there which place the errors in the quickfix buffer and allow to quickly navigate them.

  2. Subversion and Apache - I recently encountered this error when trying to mkdir the first directory in an empty Subversion repository, that was served using Apache:

    shlomi:~$ svn mkdir -m "Creating TO-DEL trunk" http://localhost:8080/svn/TO-DEL/trunk
    svn: OPTIONS of '/svn/TO-DEL': 200 OK (http://localhost:8080)
    shlomi:~$
    

    This surprised me because my client was up-to-date and the service used the same libraries, and it worked with other repositories. As it turned out the problem was that the Location entry in Apache's httpd.conf looks like this:

    <Location /svn/TO-DEL/>
        DAV svn
        SVNPath /var/svn/TO-DEL
    </Location>
    

    The problem here is that /svn/TO-DEL/ in the "Location" tag has a trailing slash which confuses Subversion. Eliminating that issue solves the problem.

  3. Firefox and mailto - if "mailto:" and other non-"http:" links stopped working in Firefox, make sure that NoScript is upgraded to the latest version. There were problems with some recent versions that broke "mailto:", "ed2k:", etc.
LinkLeave a comment

navigation
[ viewing | most recent entries ]
[ go | earlier ]