Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr1714 :: Vim Hints 001

Hints and Tips for Vim users - part 1

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2015-02-26 is flagged as Explicit and is released under a CC-BY-SA license.
vim, gvim, editor. 3.
The show is available on the Internet Archive at: https://archive.org/details/hpr1714

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:17:50

Vim Hints.

Various contributors lead us on a journey of discovery of the Vim (and vi) editors.

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

https://www.vim.org/about.php

Introduction to Vim

This is the start of the Vim Hints series.

As a Linux user there are many editors available to you. Which one you want to use depends on your needs and the amount of time you want to dedicate to learning how to use it.

One of the editors from the early days of Unix is vi, written in 1976. Contemporary with it is Emacs, also originating in 1976. However, it seemed to become the norm (in my experience anyway) that vi rather than Emacs was provided as standard with versions of Unix, and this has often continued into Linux.

I originally started using Unix around 1988 and found vi available to me. I learnt how to use it in a rudimentary way since I knew I'd find it on any Unix systems I came across.

Many derivatives and clones of vi have been created. The one which has become the most popular and available is Vim, the name of which is an acronym for Vi IMproved, created in 1991 by Bram Moolenaar. This is what I use, and I have not wanted to learn another editor since adopting it, even though I have experimented with several. This is the editor we will be looking at in this series.

What's the series about?

The thinking behind this series is:

  • You may already be using Vim; there are features you may not be aware of that can be revealed here
  • You may be using a different, simpler editor; you might want to use Vim and gain from its advanced features

Of course, you may prefer to learn Emacs instead. That's fine; you should choose the tool that best suits your needs. Both Emacs and Vim have quite steep learning curves, but the broad range of capabilities you gain from knowing either is considerable.

I am not an expert in Vim. In fact I am continuing to learn new Vim features on a regular basis. However, I have been using it for many years and would like to share some of what I have learnt.

Why use Vim?

With simpler editors you can move about a file, add, remove and change text and save the results. The editor might have syntax highlighting and some degree of knowledge of the programming language you are typing. You might have spell checking as well.

With Vim and other more advanced editors you have all of this and a lot more. You can perform global changes throughout a file, process many files at once, add plugins to the editor to change its behaviour, and so on. Also, there is a language behind the scenes which can be used to build extensions.

Using Vim

Usually, typing the command vi at the command line actually invokes vim. Vim runs in vi-compatible mode by default, which results in Vim enhancements being unavailable.

Vim uses a configuration file, which is called .vimrc on Linux. (Vim will also run on Windows, OSX and other operating systems but we will not be covering these implementations in this series.) Vim also has a GUI interface invoked by the command gvim, and it has its own configuration file .gvimrc.

I you don't have a .vimrc create one with touch ~/.vimrc before you start. This will stop Vim running in vi-compatible mode. We will look at what the .vimrc can be used to do later.

You can start Vim on its own without pointing at a file, but normally you use it to edit a file, which need not already exist. So, to create a new file called testfile invoke Vim with the command: vim testfile

Once running, Vim shows the contents of the file. All the lines on the screen where there is no content are marked with a tilde "~" character. If you are creating a file the first line on the screen will be blank, and last line will contain the name of the file followed by "[New File]" and some other details which we will examine later:

"testfile" [New File]       0,0-1         All

All the rest of the lines will contain a tilde.

Vim is a modal editor. The mode you usually start in is normal mode where you can move around the lines of the file and perform actions, but nothing you type is actually written to the file. In fact, the keys you type are actually editing commands. This is one of the features of Vim that causes problems for new users.

Since this is a new file there is not much you can do other than enter text, and to do this you need to switch to insert mode. Do this by pressing the i key. The message -- INSERT -- will appear on the bottom line of the screen. Now type some text, pressing the Enter key at the end of each line.

You might notice that in insert mode you can press the arrow keys and move back to text you have already typed. This is a Vim feature and was not available in the original vi editor.

When you have finished entering text, press the Esc key to exit from insert mode. Now you can move around in normal mode, but remember that the keys you press are now commands not data to be entered into the file.

To move around in normal mode use the arrow keys or the home row keyboard keys: k to move up, j to move down, h to move left and l to move right.

This brings us to the last mode we'll look at: command mode. To enter this mode press the : (colon) key in normal mode. This moves the cursor to the last line of the screen, which starts with the colon you just typed. Here you can enter another class of commands. This time, we'll just look at how you can save the file and exit Vim.

Saving the file is achieved with the w command, and to exit from Vim the q command is used. These can be typed together, so :wq writes the file and exits.

If you were to use :q on its own, having entered data into Vim, this would not work. Vim prevents you from throwing away your work this way. If you really meant to quit without saving then the q must be followed by an exclamation mark ("!"). So :q! lets you exit Vim without saving.

Summary so far

  • Vim usually starts in normal mode
  • Arrow keys or h, j, k and l for left, down, up and right for navigation in normal mode
  • i enters insert mode
  • Esc exits from insert mode and reverts to normal mode
  • : in normal mode enters command mode
  • :w in normal mode writes the file
  • :wq in normal mode writes and exits
  • :q in normal mode exits but only if nothing was changed or added
  • :q! in normal mode exits regardless of any changes

Errata

  • I was wrong about the contents of the last line of the Vim screen in the audio. The notes have been corrected.

History

Books

Other resources


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2015-03-26 09:35:28 by 0xf10e

Nice introduction to Vim!

`vi` on a Linux system (say CentOS) normally is a stripped down Vim as far as I can tell. On FreeBSD `vi` is part of the base system and thus has the "can't go back in insert mode" limitation (I, too, stumble upon now and then…).

I think OS X comes with Vim out of the box but it sure behaves as on any other BSD/unixiod OS in regards to `~/.vimrc`.

BTW - for anyone wanting Vim on their server: the not full-blown-including-X11-support pkg on FreeBSD is "vim-lite" and "vim-nox" on Ubuntu

Comment #2 posted on 2015-03-26 12:38:43 by Dave Morriss

Stripped down Vim

Hi 0xf10e,

Thanks for the comments. You are right about the "standard" Vim on Linux. I had forgotten. Recently when I set up a new Raspberry Pi (Raspbian) I found the issue you mention and had to install several extra vim-related components to get what I wanted.

I have actually put full-blown Vim on my server, then if I want gVim, I have transferred the screen back to my workstation over X. I use

ssh -x user@server

to login, then when I type 'gvim file' the window appears on my desktop. I may have had to do other configuration that I have forgotten about to make this happen, probably something with 'xhost'.

Comment #3 posted on 2015-03-30 06:30:42 by 0xf10e

`ssh -X` should do fine for gvim without additional configuration (as long as the server's sshd is configured to allow X-Forwarding). But I prefer vim staying in the terminal and _not_ responding to any mouse input ;)

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?