Nicolás Brailovsky


A modern blog

Archive for the ‘Vim’ Category

Vim tip: Word count

author Posted by: nico on date Apr 20th, 2010 | filed Filed under: Vim Tips
Trying to count words is a common task. Whenever you’re writting a report for class, that is. There are some legitimate reasons but they don’t matter now: it’s a great chance to show off how great Vim is.

First method: Type ggVgY”*p to copy the whole text. Then paste it into word and use word count.
Second method: Type %!wc -w, which executes wc on each line.
Third method: Type g^g (g, CTRL+g) and watch the bottom of your screen.

As ussual, Vim rocks.

Vim tip: Remapping

author Posted by: nico on date Mar 31st, 2010 | filed Filed under: Vim Tips
We all know is the funnier editor in the world. You can even use it to rot13 all your code and have lots of laughs when committing. But what if you want to automagicaly change a random word into another word? Well then inoremap comes handy, just adding this to your ~/.vimrc:
  1. inoremap hola mundo

and suddenly whenever typing “hola” you’ll see “mundo” instead. LOL. I guess it’s useful to fix typos, or to autocomplete code. Or to change “Hello” into “Fuck you” in you friend’s vimrc, whichever you think it’s appropriate..

Vim Tip: Open file in new tab

author Posted by: nico on date Oct 21st, 2009 | filed Filed under: Programming, Vim Tips
How many times have you been on a project which #includes another file and wanted to see what that file was? Well you can navigate the source code tree in a gui if you use eclipse. You can go to File > Open and, again, fight with a nice gui in Notepad. Or you can open up that file with four keystrokes in a real editor, THE real editor. Just use “C-W g f” with the cursor over the filename and it’ll open in a new tab, ready for you to hack away more code. success_vim

Vim Tip: Folding FTW

author Posted by: nico on date Oct 6th, 2009 | filed Filed under: Vim Tips
I always say methods should have two levels of indentation at most, but even if your code is perfect like mine you may still have to dwell with other people’s code (which, obviously, is ugly code), people how may have lots of fun shaping the program like a pyramid. 0040-teddy-bear-sepuku

Not all is lost, you don’t have to commit sepuku (at least not for this one). Just use Vim’s indent method like this:


:set foldmethod=indent

That should give you a better view of the code flow. As always, use ‘%’ to navigate all those pesky { and }.

Vim Tip: Rot 13

author Posted by: nico on date Sep 17th, 2009 | filed Filed under: Vim Tips
vim_editor Are you still puzzled by last week’s C++ question, yet you are too lazy to actually search for a Rot13 decoder OR use gcc to check if it works? Well, Vim can do the trick, just use g? to convert text to Rot13

You may combine it with block selection or you can just convert the whole damn thing using “ggg?G”. gg goes to the beggining, g? converts to rot13, G goes to the end.

This is all very nice but I’m still trying to figure out a way to convert back from rot13 to normal text, can anyone provide a clue?

Nice Ubuntu fonts + Vim Tip

author Posted by: nico on date Sep 8th, 2009 | filed Filed under: Gnome, Linux, Programming, Vim, Vim Tips
manuscrito It’s not unusual to hear someone complain about Ubuntu fonts. Apparently they are ugly. I guess that depends very much on a personal opinion, however not everyone knows there are alternative fonts in the common repositories (I think multiverse even has non-free fonts ported from Windows).

Someone recommended me Inconsolata as a nice programming font (it’s monospaced). I’m using it right now and it’s not bad. Let’s see how can you install it:

  1. sudo apt-get install ttf-inconsolata

incoshow

Easy and it looks even better when used with gVim. You can go to Edit > Select font to change the font preference, however this won’t set a new default for the next time you start gVim. To do this we need to add it to the .vimrc, and again, to do this we need to know the font’s name.

Type “:set guifont?” to see the font’s name. In my case it’s “Inconsolata Medium 14″ (I changed size and type). Now add the following to your .vimrc:

  1. if has(‘gui_running’)
  2.      set guifont=Inconsolata\ Medium\ 14
  3.  endif

Notice I added a backslash before the spaces, otherwise Vim will try to parse Medium and 14 as separated parameters to “Inconsolata”, which obviously won’t understand. Have fun with your new fonts.

Vim: A little color (syntax highlighting)

author Posted by: nico on date Aug 13th, 2009 | filed Filed under: Vim Tips
vim_editor Short tip this time: how do you set Vim to show pretty colors for your source code? Easy enough, :syntax on is all you need!

Vim Tip: Select everything

author Posted by: nico on date Jul 11th, 2009 | filed Filed under: Vim, Vim Tips
vim_editor There’s an easy way to select the whole contents of the document: “ggVG”. May seem like a lot at first glance but let’s review it part by part:

gg: Go to the beggining of the document
V: Enter visual mode
G: Go to the end of the document

Easy, isn’t it? Now you can press any other command to copy, delete or whatever you like.

Vim Trivia

author Posted by: nico on date Jul 4th, 2009 | filed Filed under: Vim, Vim Tips

Did you knew that Vim can “Make your woman smile for a week”?
Me neither: http://www.vim.org/trivia.php.

vim_airlines1

Vim Tip: Easy Indent

author Posted by: nico on date Jun 14th, 2009 | filed Filed under: Vim, Vim Tips
vim_editor Editing source code means you’ll be doing a lot of indenting and reindenting. There’s an easy way to indent or de-indent a block, using just “<” and “>”.

Using “>” without anyother command will indent that line by itself; use visual mode (Shift + V) to select several lines and indent them in one keystroke. You could also press “v%>” while sitting at the beggining of a block to indent it. The same applies to “<”.