NoVIMber 14, 2014 - Some simple but useful commands

I didn't get the opportunity to catch up on posts today. I'm thinking that researching a topic and writing a post every single day was a bit too ambitious. To give myself some additional project time and avoid burn-out, I'm going to cut it back to 3-4 posts per week (probably during the work week). Today's post is going to be something simple and short.

Read Commands

:read FILE  Insert the contents of FILE below cursor in current file
:read !CMD  Execute CMD and insert its output below cursor in current file

If I had known about these read commands earlier, it would've saved me so much time. For the first, if no filename is specified, it defaults to your current file (in its "saved" form). For the second, it's important to note that CMD is a shell command, not a Vim command. Notice the ! filter command that I mentioned in NoVimber 06, 2014's post.

Replacing & Changing Text

r CHAR  Replace character under cursor with CHAR
R       Enter "replace" mode

I already knew about the replace command. It's easy enough to use and I use it very reguarly. As for replace mode, I've frequently found myself getting into replace mode by accident. Long story short, replace mode is basically insert mode that replaces text instead of shifting existing text to insert new text.

~  Changes the case of character(s) under cursor

I've been meaning to commit this one to memory for a while. This also works for highlighted/selected text in visual mode. I'll cover visual mode in more depth in a later post. A quick explanation - you can enter visual mode with v, select some text with the movement keys (h,j,k,l or arrow keys), then press ~ to change the case of all selected text.


That's it for today. Like I said, simple and short! I'd still like to cover tabs in Vim but I haven't had a lot of time to actually learn about them myself. That's a task for next week. Until then, Happy Viming!

Site has moved!

I had mentioned in my first or second post that this site was being hosted on a temporary server due to my current web server not supporting Django 1.7 (centos 6 with python 2.6). I've finally started rebuilding my primary web server. The first site to move over was this one! If you happen to notice any issues, please contact me via email (nathan at or twitter (@nbchowning).

NoVIMber 11, 2014 - Buffers round 2

Before we get into Vim's tabs, I'd like to cover a couple more things that you can do to manipulate buffers in Vim.

Adding & Deleting Buffers

In my previous buffer post, I talked about creating new buffers with the :e command. That will create a new buffer, load its contents, and attach it to your current window. Let's say that you want to create a new buffer but you don't want to load the corresponding file's contents into memory and attach it to your current window:

:badd filename    Add a new buffer for filename to the buffer list without loading its contents
:badd N filename  Same as :bad filename except positions cursor on line number N

I failed to cover the deletion of buffers in my previous post. There are a few different ways to "delete" buffers. You can unload the buffer (freeing the memory it was consuming), you can make the buffer "unlisted", or you can completely delete the buffer. In the following examples, BUFF can represent a unique buffer number or buffer name.

:bunload BUFF   Unloads buffer BUFF freeing the memory it previously consumed. It will remain in the buffer list
:bdelete BUFF   Delete (make unlisted) buffer BUFF
:bwipeout BUFF  Same as :bd BUFF except actually deletes buffer instead of unlisting

It's important to note that "unlisted" buffers will not be displayed in :ls or :buffers by default. You can display listed and unlisted buffers by adding ! to either command (:ls!).

Additional Buffer Movement Commands

We've already covered displaying a specific buffer with :b N and flipping through the buffers sequentially with :bn and :bp. There are a few additional methods to navigate buffers.

:brewind  Go to the first buffer in your buffer list
:bfirst   Same as :br
:blast    Go to the last buffer in your bufferlist

With pretty much all of these buffer commands, you can add "s" to the beginning (:sbn) to split your current window and load the resulting buffer into a new window. In the example case of :sbn, it would split your current window (horizontally) and attach your "next" buffer to the new window. To accomplish this with a vertical split, you would do something like :vert sbn.


That's all for today. I'd like to mention that I forgot to add "Reference Material" links to yesterday's post. This oversight has been corrected! Happy Viming!

Reference Material

Vim Wiki: Buffer FAQ

Vim documentation: windows - buffer-list

NoVIMber 10, 2014 - Continuing with Windows

Today we'll be picking up where we left off with windows in Vim. We're going to cover a couple more of the basic aspects: Moving windows around and resizing windows.

Moving Windows

There are a couple of ways to move windows around in Vim. The first one is "rotation" based. These rotation based movements will only work in the current row or column that the window exists in. It should also be noted that your cursor will remain in the same window when rotated.

CTRL-W r  Rotate windows right (row) or down (column)
CTRL-W R  Rotate windows left (row) or up (column)

You can also swap the current window with the "next" window. The next window would be the window to the right (row) or below (column) depending on your current layout. This command will move the cursor to the new window as well (which will be move to replace the previous window).

CTRL-W x  Swap current window with next window

When you're moving windows around, it's sometimes useful to change your current layout. Notice that the following movements correspond to the usual "h", "j", "k", "l" movement keys except they are capitalized. Also keep in mind that following commands will use the full height of your screen when moving left/right or the full width when moving up/down.

CTRL-W H  Make the current window the far-left window
CTRL-W J  Make the current window the bottom-most window
CTRL-W K  Make the current window the top-most window
CTRL-W L  Make the current window the far-right window

Resizing Windows

The basic operations for resizing windows in Vim are probably what you'd expect: you grow/shrink the current windows's height/width. As with most things in Vim, there is a bit more to it than that. To start off, let's look at making all of the windows consume an "equal" (or close to equal) height/width on your screen. This command can be a bit more complicated by the "winheight", "winwidth", "winfixheight", and "winfixwidth" settings. I'll touch on those in a later post. For now, let's just assume that none of those values have been set.

CTRL-W =  Make all windows equal in height & width

Now let's look at increasing/decreasing height/width of a window. Keep in mind that, as with pretty much every Vim command, you can preface these commands with a "count" to perform the action more than once. This can be particularly useful when resizing windows since adding/decreasing a single row/column at a time (the default if a count is not specified) is quite tedious.

:resize -N           Decreases window height by N rows
CTRL-W -             Same as :res -N

:resize +N           Increases window height by N rows
CTRL-W +             Same as :res +N

:vertical resize -N  Decrease window width by N columns
CTRL-W <             Same as :vertical res -N

:vertical resize +N  Increase window width by N columns
CTRL-W >             Same as :vertical res +N

Sometimes it's also useful to specify an explicit height/width of a window. We can do that too and as you'd imagine, the commands are strikingly similar to growing/shrinking a window. If a value is not given to these commands, they will default to "as big as possible" in which ever direction you're resizing.

:resize N           Set the height of the current window to N
CTRL-W _            Same as :res N
zN<CR>              Useful shortcut to get similar behavior to :res N. See note below

:vertical resize N  Set the width of the current window to N
CTRL-W |            Same as :vertical res N

zN does not share the same "default" behavior (if N is not provided) as :res. In my experience, z seems to have the same effect as zt (moving the current line to the top of the buffer).

That about covers it for today's post. At this point you should be able to move your windows around and resize them to fit your workflow more adequately. There are still other tricks that you can do to manipulate window sizes differently but that's a topic for yet another post! Happy Viming!

Reference Material

Vim documentation: windows - window-moving

Vim documentation: windows - window-resize

NoVIMber 09, 2014 - Using Windows (not the operating system)

In "yesterday's" post, we covered the basics of buffers. Today we're going to talk about windows in Vim. A window is basically a viewport that buffers can attach to. This gives us access to the buffer so that we can manipulate its contents. A buffer can be attached to mulitple windows or no windows. When you initially start Vim, a single window is created by default. If you're starting Vim with multiple files, you can use the "-o" or "-O" flags (on the commandline, not in Vim) to open all of the files in multiple windows:

-o  Open all files in windows split horizontally
-O  Open all files in windows split vertically

vim -o file1 file2 file2

As a general rule, window related commands will start with CTRL-W.

Creating & Closing Windows

There are lots of ways to create new windows:

:split    Split current window horizontally to create a new window. Both windows will be a viewport to the same file
CTRL-W s  Same as :split

:vsplit   Same as :split except split vertically
CTRL-W v  Same as :vsplit

:new      Same as :split except new window is an empty file
CTRL-W n  Same as :new

:vnew     Same as :new except split vertically

CTRL-W ^  Split current window and edit alternate file in new window

There are also several different ways to close windows. The most common would be :quit. That's right, the quit command that we all use regularly closes our current window. When there is only one window, this will exit Vim. Let's look at some more ways to close windows:

CTRL-W q  Same as :quit
:qall!    Closes all windows and exits Vim. Fails if any buffers are unmodified unless ! is given.

:close!   Closes the current window. If buffer is unchanged, hidden is set, or ! is given the buffer becomes hidden. Otherwise the command fails
CTRL-W c  Same as :close

:only!    Make current window the only window on the screen. Other buffers will be hidden. 
CTRL-W o  Same as :only

It's important to note that passing ! to :close and :only is "safe". If modified buffers would be closed, the buffers will become hidden and modifications will be preserved.

Moving Between Windows

The basic commands to move your cursor between multiple windows are the same as the default cursor movement keys - "h", "j", "k", and "l" - with CTRL-W in front of them. You can also use CTRL-W with the arrow keys.

CTRL-W h  Move cursor to window that is left of current window
CTRL-W j  Move cursor to window that is below current window
CTRL-W k  Move cursor to window that is above current window
CTRL-W l  Move cursor to window that is right of current window

CTRL-W t  Move cursor to top-left window
CTRL-W b  Move cursor to bottom-right window
CTRL-W p  Move cursor to previous window


There's a lot more that I'd like to cover (moving windows around, resizing windows, etc...) but I'll save those for another post. At this point, you should be able to reasonably use buffers and windows to edit multiple files inside of a single instance of Vim. As it stands, I'm not really sure if this would improve my workflow (multiple Vim instances inside of a screen/tmux session). Only time will tell! Happy Viming!

Reference Material

Vim documentation: windows