Step-by-Step Commands to Write Your First Vim Function Example | Rules | Uses
You can create your own function to run on any file open with Vim editor. And it is simple to automate your mundane stuff on Vim editor.
Here is the step-by-step procedure for adding and calling a new function for Vim editor. I will also explain Vim function example and use cases.
Are you ready for writing your first Vim function? I am sure, you have a Linux system with Vim editor installed.
[Step-by-Step] Writing Vim Function:
Step 1: Open vimrc File
Note: It is not necessary you will be having this file. if it is not there, just create it.
Step 2: Write a New Function in the vimrc file
To make it simple I am writing a function that prints some message (says – First Vim function.)
function! FirstFunction() echom "First Vim function!" endfunction
Don’t confuse with the word
echom. For now, you can consider it as equivalent as bash command
echo. Other syntaxes of the function are self-explanatory.
Save the file. (Command to the file-
Step 3: Calling / Executing Vim Function:
You don’t need to compile the function. You can call this function from Vim editor with any of the file.
Open any of the text files in Vim editor.
Vim Command to execute the Vim function from the command line:
So, it will print the following message.
First Vim function!
Another Vim Function Example:
Here we are using the return command which returns the message to the calling function.
function! Play() return "Playing with Linux!" endfunction
Run following command:
It prints the message as below.
Playing with Linux!
Now, let’s check…
Some of the Important Rules for Writing Vim Function:
- You don’t need to give
;(semicolon) at the end of each statement, unlike most of the programming languages.
- The vim function should start with an Uppercase letter. (This is slightly different from the C and bash behavior where you can allow function name to start with upper and lowercase.)
- Can I use the
+= operator is included in the latest vim version. So it is available in Vim since version 7.0.
++ does not support yet in Vim function.
firstFunction- is invalid function name. FirstFunction- is valid function name.
- You can also pass different parameters to the Vim function.
- You can also call the Vim function recursively.
Use Case for Vim Function:
- Write a function to replace special character/word with new character/word. Pass both the words as parameters to the function.
- Formatting and aligning syntax in programming is not easy. You can write the function to reformat file based on the file types (ex CSS, HTML).
- Write a Vim function to replace the tab with 4 spaces in Vim file.
- If you are a programmer, many times you need to copyright your code. To automate this, you can write a Vim function to add PROLOG or copyright message in your coding file.
What’s the next?
There are also many built-in functions you can use with the Vim.
You can explore the various rules and commands for writing Vim function. These rules and commands are well documented in the Linux/Ubuntu.
In Vim editor, run following command to get the help from the document.
Explore the return functionality.
Also, Explore the Vim Hardway to Excel in Programming:
- Step-By-Step Commands for Vim Case-Insensitive Search
- How to Set Up Vi/Vim to Highlight Syntax for Programming?
- 6 Commands to Edit File even if there is no vi, vim on Your Linux
Let me know your view about this simple tutorial about Vim function example. If you have any point to discuss, write in the comment section.
Leave a Reply