Guides Open Source Software Scripting in Vim

Open Source Software Scripting in Vim

More about Vim and open source software

I’ve been using the open source editor Vim for an alarming number of years now, but I only very recently encountered its scripting capabilities. Here I’ll look at writing a quick script in Vim’s
built-in scripting language. You can also use another scripting language if you prefer.

Get even more out of the open source software editor, Vim, by taking advantage of its scripting capabilities.

Vimscript is function-based. Here’s an example of a function to put in your ~/.vimrc that puts a single text underline (a row of dashes) under the current line. Note that function names must start with a capital letter to distinguish them from built-in functions.

function! SingleUnderline()

The first line copies (:t, a synonym for :copy) the current line (.) to the line below it. The second line substitutes - for all characters in the new line, using the substitute function. In a function, you don’t need to use : to call a built-in function.

Next, you must be able to call the function. Start up Vim, write a line, hit ESC, then type

:call SingleUnderline()

(Vim will tab-complete function names for you.) The current line should now be underlined.

However, that’s a lot of typing for a short function! Instead, you can set up a mapping, again in your ~/.vimrc:

nmap ,u :call SingleUnderline()
imap ,u :call SingleUnderline()o

The first line maps ,u to the SingleUnderline() command in Normal mode; the second deals with calling it from Insert mode, then returning to Insert mode afterwards on a newline underneath.

This is a fairly simple example, but Vim functions can get about as complex as you like. Check out the list of built-in functions by type to help you construct your own functions; there’s also a bunch of scripting tips on the Vim wiki.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of “Linux System Administration Recipes: A Problem-Solution Approach” (Apress, 2009).

Follow ServerWatch on Twitter

Latest Posts

How to Convert a Physical Computer to a Virtual Machine

Many organizations are implementing virtualization technology into their networks to convert physical computers to virtual machines (VM). This helps reduce overall physical hardware costs,...

HPE ProLiant DL380 Gen10: Rack Server Overview and Insight

The HPE ProLiant DL380 series has consistently been a market leader in the server space. The Gen10 released in 2017 further increased HPE's market...

Best Server Management Software & Tools 2021

Finding the best server management software tools for your organization can have a major impact on the success of your business operations. Manually handling...

IBM AS/400: Lasting the Test of Time

Some server operating systems (OS) were built to survive the test of time – the IBM AS/400 is one such system.  The AS/400 (Application System/400)...

What is Disaster Recovery?

The modern organization's heavy dependence on using data to drive their business has made having a Disaster Recovery (DR) plan in place a necessity....

Related Stories