Home

Three Vim Tips

  • Mar. 6th, 2007 at 7:43 PM
EvilPHish evil fish shlomi fish

More silly Vim tips! In order to make your search case-insensitive use the \c modifier. In order to have vim display line numbers use :set nu. And speaking of the :set options, you can use :set nu! (with a trailing question-mark) to toggle the option between on and off, :set nu? to query its status, and :set nonu (with a leading "no") to set it off.

Happy vimming!

Vim Tip: Text Object Selection

  • Feb. 20th, 2007 at 8:37 PM
EvilPHish evil fish shlomi fish

The Vim text editor has a set of selection commands for selecting various parts of the text while being inside them. Some of them are very useful - here are some examples:

  1. To indent the inside of a block (after a copy and paste for example) use >i} or <i}. It beats writing >% and then doing <<%<<
  2. To change a string use ci".
  3. To delete a block use da}.
  4. To copy a paragraph use yap.

Etc. Oh and try typing :he holy-grail in Vim (this tip courtesy of LeoNerd from Freenode who found it).

Vim Tip: Useful Buffers Commands

  • Feb. 8th, 2007 at 6:48 PM
EvilPHish evil fish shlomi fish

Vim supports the concept of buffers, which are basically separate files that you can edit simultaneously in the same editor instance. I normally used the commands :bn (next buffer), :bp (previous buffer) and :bd (delete buffer), but I discovered other useful ones a few days ago.

One can display a list of buffers using the :buffers command, which can also be abbreviated as :ls. Besides each buffer is a number. One can then use the :b $NUMBER command to switch to that buffer. Note that if you're using gvim then the Buffers drop-down menu (Alt+B) will display a list of buffers with their numbers and will allow you to select a buffer to switch.

I'm off to a friend's wedding. Happy vimming!

millie O&M David C. Simpson

I have several vim tips in the pipe, so I think I'll start with one. If you're editing commands in the ex-like Command Line Mode of vim (the one that's displayed after you press ":" in Normal Mode), you can use the Ctrl+R family of keybindings to insert registers and other text there. So for example, if you wish to search for a word, you can do yaw in the buffer, and then type :grep -r <CTRL+R>" ..

Since this pattern is so common, the vim developers already thought of it, and you can simply position the cursor on the word and type :grep -r <CTRL+R><CTRL+W> .. Ctrl+R;Ctrl+A searches for WORDS.

Note that Ctrl+R by default executes the pasted expression as a mini vim program. If you want to insert the literal string, use Ctrl+R;Ctrl+R. Better safe than sorry.

Vim Tip: t/f/F/T

  • Feb. 1st, 2007 at 8:55 PM
EvilPHish evil fish shlomi fish

Third Vim tip in a row on this blog (must be a record for me) and this time a blast from the past. Well, at least something I learned a long time ago, but many people are not aware of.

By using the "f" command and then a character one can move to the next occurence of that character on the same line. So "ts" will move to the next occurence of the letter "s". "t" is the same except it moves to the character before that. "F" and "T" are for moving backwards. You can specify an optional count for jumping to the N'th occurence in the same line.

This for example is useful for quickly selecting tokens. If for example, you have a C-style string delimited by double-quotes, you can position your character to its start using the "f" command and then right (The right arrow key or "l"), and then select up to the end of the string using yt" , or alternatively replace it with something else using ct". Alternatively, one can select the string including the quotes, by positioning the cursor at the opening quote and typing yf".

For more information consult :help f.

EvilPHish evil fish shlomi fish

In Vim lowercase marks are specific to each file, while uppercase marks are global. Thus, one can type mA, mB, mC etc. to set such a mark in a certain position in a fileand then use `A or 'A to jump to it even from a different file. This is useful to keep several bookmarks in several files and jump from one to another.

Since one can use ` and ' as a motion command, I'm not sure how a jump to an uppercase mark from a different file is useful as an argument to a motion-accepting command such as y or d. However uppercase marks are still useful for what I demonstrated.

millie O&M David C. Simpson

A vim tip that I learned from my supervisor at work today is this: you can search for the next occurence of the word under the cursor using "*". There's also "#" which searches backwards, and there are other search commands which seem useful on the same page.

If you want to jump to a tag you generated using ctags you can use the "Ctrl+]" or the ":tag" commands (":tag" can be abbreviated to ":ta").

And I'll leave you with the following vim macro which I wrote. See if you can guess what it does. I inserted some linebreaks but there are none in the original version.

map <F2> i<CR><CR><div class="entry" title="<CR>
<ESC>!$date +\%Y-\%m-\%d<CR>k$Jx$a: <ESC>mda"><CR><CR><CR>
</div><CR><ESC>`da

Vim Tip: Pasting with "P"

  • Dec. 14th, 2006 at 2:57 PM
EvilPHish evil fish shlomi fish

Today I suddenly wondered that if "p" (in lowercase) was paste in Vim, what did "P" (in uppercase) do. I guessed that what it did was that it pasted before the line of the cursor, rather than after it. (In a similar fashion to the distinction between "o" and "O"). And that's what it indeed does (:help P).

This is useful because it behaves more like the MS-Windows paste, and also allows one to paste to the first line of the file. I'm now thinking it might be a good idea to go over all the uppercase letter commands in Vim and see what they do because I don't know them all.

Tags:

Grey Cat, Puzzles and some Vim Tips

  • Dec. 13th, 2006 at 8:56 PM
EvilPHish evil fish shlomi fish

When I went out today on a walk, I saw a pure grey (not striped) street cat walking on the sidewalk. I don't believe I've seen him before. He was very majestic and reminded me of a majestic grey cat who used to hang around the ground floor of my previous building. The fur of the cat I saw today was somewhat darker, though.

That put aside, I dedicated some time to work on my home site's puzzles section. I traced the solution of the Ravensborg's Guild puzzle, and as I discovered it had some missing information required to solve it. The corrected riddle along with the solution is on that page now. I also traced the solution of "Between the Screws. This time there wasn't any missing information but there were a few hints that I did not need.

Next on my agenda in the puzzle arena is to trace the solution (and the final outcome) for another puzzle, and to integrate the grammatical corrections an IRC correspondant reported on "Between the Screws".

And now for some Vim tips. One can use the vim black hole register (or null register) "_ (:help "_) to delete text using the "d" or "c" commands without affecting the other registers. So one can press "_d{movement}p to safely paste text. The second tip (which I discovered when looking for the reference for this one) is that pressing " followed by the uppercase letter of a register, will append text to this register.

I hope you enjoyed this entry. Until next time, stay cool!

Vim Tips (and other Stuff)

  • Nov. 29th, 2006 at 10:43 PM
millie O&M David C. Simpson

I apologise for not posting here in a few days, but publicising the Tel Aviv Linux club meetings, and working on this year's Perl Advent Calendar is relatively time-consuming. But now happy that I finished two articles for the calendar, I can blog a little about other stuff, like Vim Tips.

So here are two Vim tips I've found:

  1. When using the visual mode, one can use the o to move to the other end of the selection and modify it. Read more about it in the link.
  2. If you are in insert mode and your line is indented (say after pressing o on an indented line) but wish to start writing from the beginning of the line, you can press Ctrl+W to delete the leading whitespace and start typing at the beginning of the line.

That put aside the last Tel Aviv Linux Club meeting was very successful - many people came, Gilad gave a good presentation about Development Tools for Linux, and four of us went to the café afterwards. And here's some publicity for the next presentation in the series. (Ignore the title of the page and message - they're a leftover from the previous announcement).

Have a lot of fun!

Eclipse on Mandriva 2007

  • Oct. 17th, 2006 at 8:20 PM
EvilPHish evil fish shlomi fish

So I was editing XML using vim and again decided that there may be a better editor out there. A google search for "XML editor" or "open source XML editor" yielded nothing. Then I thought that maybe Eclipse has a good XML editing mode. So off to install Eclipse

I'm running Mandriva 2007 and the urpmi (Mandriva's package manager) command for installing Eclipse ("urpmi eclipse") completed successfully, after installing a lot of dependencies. Then I tried to run it from the command line. It took it a long time, and then it crashed. After consulting with the people on Freenode's #java, I was told that I should run it using Sun's Java Development Kit instead of with gcj.

So I downloaded it, and the people there told me how to run Eclipse using it. Just use the following commands:

export JAVA_HOME=/usr/java/jdk1.5.0_09
PATH="$JAVA_HOME/bin:$PATH"

Then I was able to run Eclipse. However, when I tried to edit an XML file, I got it without syntax highlighting, and without anything else. Turns out the default XML editing mode of Eclipse is pretty lame. And a Google search for "Eclipse XML" yielded only non-open-source solutions. So I still don't have a better XML editor than gvim.

Again on #java, someone recommended Vex, which is a visual editor for XML. But it provides a word-processor-like interface, that I won't like as I want to edit the tags and need to edit "data-style" XML documents.

If anyone can suggest a good open-source XML editor that runs on Linux, please comment here. In the meantime, gvim is all I have.

EvilPHish evil fish shlomi fish

Well, I stayed up all night during the night before last to backup. In the morning afterwards and during the rest of the day, I still really wasn't tired. One good thing that came out of it was that I covered several full chapters of "Perl Best Practices" (which I can now highly recommend).

During the day, I tried to get my Kubuntu system to work with my existing data which seems to be completely OK. I eventually upgraded to Ubuntu Dapper which is the most recent release. Even that is still missing recent software that exists in my Mandriva 2007 system, like Vim 7.0, or Apache 2.2.x, but it's still better.

At first I tried creating an account on my Kubuntu partition with the same UID as "shlomi" on Mandriva, but a different home directory location (/mnt/mandrake/home/shlomi). This worked well for some things, but eventually I opted for a better solution: create a symbolic link for /home/shlomi to point to the same directory in the Mandriva partition, and have the same username. This seems to work very well.

I had to install a lot of software to start hacking again, and as a result my 5.2GB Kubuntu partition is 90% full. I can install software using ./configure --prefix="", but I'd really like to enlarge the partition. Fortunately, due to backups and backup induced cleanups my mandriva partition now has 32 GB free space and I can comfortably reduce it. (Assuming the XFS tools can handle the bad blocks properly.).

I already updated my home site with two new ideas, and fixed some bugs I discovered there. I also discovered a very obscure Apache 2.0.x bug, which only Konqueror was affected from, and unsuccesfully tried to compile my homesite using perl-5.9.4. I'll try again later, but this may involve squashing some CPAN bugs.

Meanwhile, we bought a new hard disk (a 160 GB SATA one) to use as a replacement for the Linux one. This time I'll have one partition for home, and one or more partitions for the roots of the different distributions. I already burned a CD of Ehad 2006, where Ehad is the Israeli-oriented remastering of Mandriva, and am going to install it.

In case, I'm not going to write in this blog until Rosh Hashanah (which takes place this Friday) then Shanah Tovah. I expect that I'll again spend part of Yom Kippur summarising the previous year as reflected from my blogs.

Finally, today when I went to a walk, I took our good camera with me, and took some pictures. Expect them at a Flickr near you soonishly.

Latest Month

July 2008
S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  

Tags