A gentle introduction to vim

vim is a great tool for editing any form of text, be it a blog post, text or programming code. Unfortunately there is a bit of a learning curve when first getting started with vim. In this article I want to provide an overview of some of the key features of vim and how to get started using this editor.

If you are using Linux, it is very easy to use vim. Just type vi in the terminal and vim will be open. In windows, I find the easiest way to use is to install cmder. Once it is installed, just start cmder and type vi just as one would in a Linux system.

Initially when you start up vim you will be in a new empty file and what is called normal mode. In normal mode, it is not possible to enter new text. To do so, we need to switch to insert mode, which is easily done by typing i. You can now write text to your heart’s content. Finally pressing Esc will get us back into normal mode.

To save our work, we must go into command mode, which is achieved from normal mode by prefixing commands with :. :w test.txt followed by enter will save the text we have entered and :q will quit the vim editor.

Switching between these modes may seem a bit cumbersome at the start but splitting up the task of working with text into these different modes allows for very efficient and effective workflows.

In the following I will first describe how each of the individual modes works and then show how we can use operators and text objects to unlock the full potential of vim.

Modes

Normal Mode

Normal mode is the default mode that vim will be in once its started and can usually be enabled by hitting the Esc key. It is the mode in which it is easiest to enable the other modes and also to navigate within a document. Some of the most useful commands for normal mode are the following:

  • j, k: Go one line up or down
  • h, l: Move one character left or right
  • i: Enable insert mode
  • v: Enable visual mode for selecting by character
  • V: Enable visual mode for selecting lines
  • :: Enable command line mode

Insert Mode

Insert mode is closest to how a normal editor works. It is possible to add and delete text. This also renders most keys on the keyboard unavailable for composing commands. So most importantly, we will need to remember the keyboard shortcut of only Esc to get back to normal mode.

Visual Mode

Visual mode is used for selecting text. The most important commands are:

  • j, k: Select line above or below
  • h, l: Select character to the right or left
  • d: Delete selected characters
  • y: Cut selected lines and place in register (press p in normal mode for pasting)
  • >, <: Indent lines to the left or right

Command Line Mode

Command line mode is for system level tasks such as saving or loading files and, importantly, for closing vim. Some important commands (as entered when in normal mode).

  • :wq: Save currently open file and quit
  • :q!: Quit vim and discard any unsaved changes

Operators and Text Objects

Since keys on the keyboard are only used for writing text in insert mode, the keys of the keyboard become available for other operations in the remaining modes. That is great for making complex operations quickly accessible using the keys that are the easiest to type. Since it is not required to use function keys such as Ctrl and Alt, it also becomes very easy to string different commands together. For instance, the following command will delete a word:

daw

Such more complex commands in vim are composed of operators, text objects and motions. In the example above d is an operator for deleting whereas aw denotes the text object of a word including its trailing space. So given that c is the operator for changing we could also write:

caw

This would enable us to change the word at our current cursor position rather than deleting it. Given that p references a paragraph text object, we could also write:

dap

This would delete a whole paragraph; with a paragraph being any block of text proceeded by an empty line.

Here a quick reference of some operators:

  • d: Delete
  • c: Change
  • y: Copy/yank into register

And some text objects:

  • aw: A complete word
  • as: A complete sentence
  • ap: A paragraph
  • i{: Everything within curly braces
  • i": Everything within quotation marks
  • a": The content of the quotation marks plus the quotation marks themselves.

There are usually two flavours of text objects. Those prefixed with a and those with i. The a prefix denotes that we are including whitespace, for instance the training whitespace after a word, whereas the i prefix does not include the whitespace. Therefore, when deleting a superfluous word, we would usually use daw whereas when we want to replace the word (and preserve the whitespace around the word), we would use ciw.

Motions

Motions can be used for two purposes. Without a proceeding operator, a motion can be used to navigate within the text when in normal mode. with a proceeding operator, the operator will be applied by whatever is captured by the motion.

For instance, entering $ in normal mode will get us to the end of a line. If we enter d$, we will delete all text until the end of the line.

Here a list of a few of the motions available in vim:

  • 0, $: Go to the beginning or end of a line
  • j, k: Go one line up or down
  • h, l: Move one character left or right
  • fc: Find the next occurrence of character c
  • Fc: Find the previous occurrence of character c
  • w, b: Go to the next/previous word
  • G: Go to the last line of the document
  • gg: Go to the first line of the document
  • (, ): Sentence backward / forward
  • {, }: Paragraph backward / forward

References and Further Resources

Free Tool for Renaming Files with Current Date

For many files, it is good to keep track of when they were originally created. In theory, every file created on any mainstream operating system should have its ‘creation date’ and ‘last date modified’ recorded as part of its metadata. This mechanisms, however, is often unreliable, especially when using file synchronisation tools such as Dropbox or Google Drive.

Thus it is often handy to prefix file names with the date they were created, such as:

2018 09 30 Letter.pdf

There are some nice tools available for this purpose, for instance Bulk Rename Utility. However, I often find these a bit too complex for what I need.

I have thus developed a little tool – Date Namer – , which does just the one thing I require: To prefix file names with the current date.

date-namer.PNG

Be welcome to download this tool from here:

Upcoming release will be published on this page: Releases. Further, all the source code for this tool is available on GitHub.

For those interested in the implementation details: For this project, I tried using Electron. This allows developing a Desktop application using Node.js. I found this overall quite easy to use. Internally this application will run an instance of Chromium to render the application. The running application takes thus around 50 MB of RAM. I think this is not too bad for this use case. The app performance is very good.

Free PDF Printing for Windows without Adware

Printing from any windows application to a PDF printer is not supported in the most recent Windows versions by default.

Most free applications for this purpose which can be found through a simple Google search unfortunately often come with unwanted Malware or Adware (hello, Ask.com toolbar!).

However, there is a free and easy solution which comes bundled without any additional software:

Ghostscript + The FreePDF printer

Just download and install Ghostscript from here: heise.de.

Then, download the FreePDF printer from here: freepdfxp.de/download_de.html

freepdf

The website is in German, so I highlighted the download link in the image above.

Next time you print a document, the FreePDF printer should be available as one of the windows printers:

freepdf-printer

You then can choose whether to save the file or sent it by email (as well as choose the quality):

freepdf-print

The FreePDF/GhostScript solution might not look shiny but it certainly does the job done (and leaves your system free of any unwanted guests).