Thursday, August 19, 2010

Moving

The Vim Blog is moving! I've got a web server set up and my house and I've decided that it would be a bit more professional to just host it myself. I'll be using Rails or maybe Wordpress. I like the flexibility of Rails, but I like that Wordpress will give me better search engine optimization. Anyway... I'll post a link to the new site once I get a domain registered and the app set up. After that, this blog will be deleted forever!

Monday, July 26, 2010

Overview of Regex in Vim's Command Interface

I've been asked about substitutions in Vim many times, so I figured this would make for a useful post. This is just a quick overview of this feature of Vim, but it should handle most of your needs. If you're interested in hearing more, email me and I'll add another post. It's also probably a bit helpful to be familiar with regular expressions when you read this.

First, I'll talk about the form of the regex. It looks like this:

restriction command flags

Think about it this way, and you'll be okay. Here's an example to help you wrap your head around the idea. This example substitutes all occurrences 'goodbye' for 'hello' in the first three lines:

1,3 s/hello/goodbye/g

The restriction in this case is 1,3 which signifies a range. The command is s/hello/goodbye/. The flag is g. Hopefully, you can clearly see the three distinct parts of the command. Let's talk about these a bit more in-depth. The first part I'm going to talk about is the command, because it's the most prominent of the three.

The Command

The command can be either a search:

/pattern/

or a substitution:

s/pattern/string/

Using a slash-delimited(/) regex will do a forward search/substitution. If you want to search backwards, you can delimit your regex with question marks(?) like so:

s?pattern?string?

The rest behaves essentially like a standard regular expression. Next, I'll talk about flags.

The Flags

There are only four flags and they go immediately after the final delimiter of the command. The four flags are g, c, i, and I. Here are their meanings:

g - global - continue searching after the first occurrence on the line; most searches/subs stop after they find a match
c - check - prompt to check if you want to continue with substitution; works only in substitutions
i - case-insensitive - ignore case when searching
I - case-sensitive - check case while searching; this is the default behavior

The Restriction

The restriction is used to specify which lines are affected by the search/substitution. It can specify one or many lines and can be specified by a line number or a regex that will return the number of the first line it finds a match in.

To specify one line, simply write the line number or the regex and then a space, like so:

3 s/day/night/

This will replace the first occurrence of 'day' with 'night' in the third line. To use a regex, do this:

/day/ s/day/night/

This regex finds the first line with 'day' in it and replaces the first occurence of 'day' with 'night'. To do a range of lines, place a line specifier (a line number or regex) before and after a comma like so:

10,/night/ s/day/night/

This takes all lines (inclusively) between the tenth one and the first occurrence of 'night' after that, and replaces the first occurrence of 'day' with 'night' in each line.

Now I know this might seem like a lot, but give some practice and it will make perfect sense in due time. Try to use this whenever possible to get extra practice in.

Hope this was helpful! Comment or email me directly for any questions, comments, or suggestions; I'd be more than happy to help! Don't forget to mark your reaction to this article below!

Sunday, July 25, 2010

Screencast #2: Managing Vim Files

Here's the new cast. Quality is pretty bad because I filmed too big of an area. I highly recommend that you view at one of the links below. Also, GitHub has some really great tutorials and walkthroughs for using Git with GitHub. Check out Yehuda Katz' tutorial on Git.

View the screencast here: FLV AVI MP4

Manuscript can be found here.

NOTE: Also, when creating a repository, you must go to GitHub to make it there before you try to add the remote origin.

Friday, July 23, 2010

Mapping the Leader and Some Useful Leader Commands

I mentioned the 'leader' in my previous screencast. The leader is a key that is central to many commands in normal mode. Think of the leader as another control key for Vim commands in normal mode. You should centralize many of your command maps around the leader as this is, by nature, an unused key. Here's how you map the leader and some useful commands to go with it:

The line necessary to map the leader is this:

let mapleader = "\\"

In this case, the leader is set to '\' (the other backslash is to escape that one so it doesn't escape the quote). This is the default, but you may have to explicitly use this. That's what I had to do. Even if you don't, it's probably good to put that in your vimrc file so you know you can always count on it. Also note that the leader doesn't have to be '\'. In fact, many people use '_'.

Here are some useful leader commands. As you can see, they are things you might see yourself doing frequently, and much easier, with the help of the leader. You can copy and paste this directly into your vimrc or into the command mode in Vim. For those of you that don't know, the quotes represent comments.


" Tab commands using the leader
map <leader>tn :tabnew<cr>
map <leader>tc :tabclose<cr>
map <leader>tx :tabnext<cr>

" NERDTree shortcut
map <leader>nt :NERDTree<cr>

" Shortcut for opening vimrc
map <leader>rc :sp ~/.vimrc<cr>


NERDTree is a plugin that closely resembles the tree-like file browser in things like TextMate or Aptana Studio. I'll talk more about this one later. Also, some of you may be unfamiliar with tabs. But don't fret! I'll also talk about that in another post.

Please, post any requests for tutorials, tips, tricks, or anything you can think of. I'd be more than happy to talk about them!

Thursday, July 22, 2010

Screencast #1: The "Leader-o" Command

Okay, here's my first shot at a screen cast about Vim! This cast is about a command map that gives you the ability to quickly open a line for writing a new code block between two existing blocks. Please feel free to leave me any suggestions or comments!
You can view the manuscript for this here.
Enjoy!