Home CPSC 225

Even More Vim

 

More Visual Modes

We have seen that the 'v' key brings us to visual mode. However, there are actually three distinct visual modes:

KeyMode
vCharacter visual mode
Shift-vLine visual mode
Ctrl-vBlock visual mode

The character visual mode allows us to put the end points of the selection on any character on any line. This is helpful if we want to select just part of a line.

The line visual mode is less fine-grained. In this mode we can only select entire lines at a time. It is helpful because most of the time we want to do things on entire lines at a time.

If we want to move a block of code, we can go into line select mode, and choose the lines without needing to worry about getting the exact end points right. Then we will press 'd' to cut the text and 'p' to paste it some place else.

Finally there is block selection mode which is a little more interesting. Block selection mode lets us select a portion of text by row and column.


 

External Commands

Vim allows you to run Unix commands, and view the results with the ":!" command. For example, if you need to know what time it is:

:!date

You can also use this to execute the current file in an interpreter (Vim interprets "%" as the name of the current file).

:!python %

Or check the number of words you've written:

:!wc -w %

Vim also lets you paste the output of a UNIX command into the current buffer with the ":.!" command:

:.!date

This runs an external command and puts the output into the file you are editing.


 

Key Mappings

Another way to customize Vim is to give custom key mappings. This can be done to shorten often used operations. These key mappings create new normal mode commands.

For example, we can map the 'n' key to mean toggle line numbering by putting the following into the .vimrc file:

nmap n :set number!<CR>

This maps the 'n' key to the key sequence on the right. The command "set number!" is to toggle the number option. The "<CR>" is a carriage return and refers to the enter key.

However, the problem with this is that 'n' already has a meaning in Vim - it means jump to the next result of the most recent search. In fact, nearly every key has some existing meaning. To get around this, Vim has a "leader key" that has no default meaning. The default leader key is the "\" key. To make the new mapping using the leader key, you could do the following:

nmap <Leader>n :set number!<CR>

Now entering the command "\n" will toggle line numbering.

We have as many commands on the right hand side as we want. The following mapping maps "\p" to a command which toggles both line numbering and Vim's "paste mode":

nmap <Leader>p :set paste!<CR>:set number!<CR>
This is helpful for copying and pasting to or from Vim since, we don't normally want to copy line numbers and Vim's paste mode disables auto-indentation, abbreviations etc.

You can change the leader key if you want to. Popular alternatives to '\' include ',':

let mapleader=","

And the space bar:

let mapleader=" "

 

Abbreviations

Another handy thing to put in your .vimrc is an abbreviation. This works by replacing a piece of text you type with another. For example, with the following line in the .vimrc:
ab tumw The University of Mary Washington

We can type "tumw" and Vim will replace it with "The University of Mary Washington".

Note that the key mappings discussed above are for normal mode, while abbreviations are for insert mode. The left part of an abbreviation should not be anything that you would type normally, as Vim will automatically replace it with the right hand side.


 

Completion

Vim has a simple ability to do completion. It is not based on semantic meaning (like that of an IDE), but only on the words that exist in the same file.

It can be triggered by typing Ctrl-N which will choose the next word that matches. If there are multiple, you can cycle through them with Ctrl-N and Ctrl-P.


 

Vim Backup Files

When you are editing a file with Vim, it makes a backup file to save your unsaved changes. If you close Vim normally, it will remove these backup files automatically and you will never notice them. However, if you lose your SSH connection, or close the terminal window without exiting Vim first, then the backup file will remain behind. The next time you open that same file with Vim, you will see a message like this:


Vim recovery file message

This means that Vim wasn't closed properly last time and so has a recovery file still there. It can also mean that you have another terminal window open that's editing the same file.

When you see this, I would recommend first checking the file as it exists on disk by choosing 'O' to open it read-only. If that brings up the file as you expect and want it to look, then close Vim and delete the backup file (either by choosing the 'D' option from the menu or just removing the hidden .swp file).

If opening the file with 'O' does not bring up you want, it's possible the backup file has changes you made which weren't saved. In that case, open the file again and choose 'R' to recover from the backup file. Save the file properly and quit. Then you can safely delete the backup file.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.