Tutorials

Tutorials about HTML, CSS, PHP, Javascript, and Photoshop

  • Home
    Home This is where you can find all the blog posts throughout the site.
  • Categories
    Categories Displays a list of categories from this blog.
  • Tags
    Tags Displays a list of tags that has been used in the blog.
  • Archives
    Archives Contains a list of blog posts that were created previously.
  • Login

Basic Vim Configuration

by in Photoshop
  • Font size: Larger Smaller
  • Hits: 4538
  • 0 Comments
  • Subscribe to this entry
  • Print
4538

After studyingVim for BeginnersYou’ll have seen that the relative line numbering for your files do not survive a reload. Though this behaviour can be defined in the Vim configuration file

The .vimrc File

All ofVimConfiguration information is in theVimrcFile found in the home directory. You can open it with the edit file command from insideVim:

:e ~/. Vimrc

This file is empty because it has not been used. This tutorial will show you how to use it

Commenting Code

You should document what is done, Before you insert any code. You might know what you are doing right now, but a month from now is a different matter. Always comment the code so that when you come back to it you’ll be reminded of why you coded something

For theVimrcAnything after a double quote to the end of a line is not used, file. Therefore, you can put anything there to help you remember what you did

Set number  "This turns on line numbering

Now you will always know that theSet numberCommand turns on line numbering.  

Options and Variables

You need to put this in to the , To turn on line numbers and relative numberingVimrcFile:

Set number          "This turns on line numbering
set relativenumber  "This turns on relative numbering

Save that and you no longer have to think about it. Every time VimOpens a file, it will now show relative line numbers. TheSetCommand sets any option that is inVimAn option is turned off by setting the opposite option. Opposite options are the same as the option withNoIn front

Use the following to turn off line numbering and relative numbering:

Set nonumber            "This turns off line numbering
set norelativenumber    "This turns off relative numbering

Don’t put both sets in to theVimrcFile. That will turn it on and back off. I will show you how to do that more programmatically, In a little bit

Toggling the option is another way to set the option, If you already know the state of an option. Options are toggled by putting aAfter the option name. This toggles on/off the options:

Set number. "This toggles the value of number
set relativienumber. "This toggles relative line numbers

Use a, If the current state of an option is neededAfter the option name. InVimType:

Set number

It will returnNumberThis enables you know the state of the option

You can set the, If you want to change the number of columns that the line number area showsNumberwidthTo a value. The following will set the column width to 4:

Set numberwidth=4       "Set the line numbers to 4 spaces

Try it out and see how many spaces you like and set that in to the VimrcFile

If you need a variable, variables are created using theLetStatement. For example:

Let name = "Richard Guay"   "Set my name in to the name variable

When you save that in the VimrcIt can displayed in command mode with, file (but use your own name):echo name

Seeing the Variable name

Options are treated as a variable by prefixing the option with an&Options can be printed by:

:echo &numberwidth

TheEchoCommand is only for variables, but the prefixing an option with an&TellsVimTo treat the option as a variable. This enables you to use the option’s value in math. Therefore, to increase the line number area width by one is done by using

:let &numberwidth = &numberwidth + 1

AnEcho &numberwidthShould now show5

All variables have their scope as well. Scope is defined by a letter:And the variable name. Undefined scope is treated as global scope. The different scopes are:

B:Buffer scope—Only usable from within the current buffer
W:Window scope—Only available from the current window
T:Tab page scope—Only available in the tab page
G:Global scope—available everywhere
L:Local scope—available locally to that function defined
S:Source scope—available only within the sourced file
A:Argument scope—only available within the function
V:Global scope—used to refer to a variable defined and used byVim

The proper definition for the name variable using global scope is:

Let g:name = "Richard Guay" "Set my name in to the name variable

Getting Information

You might wonder how do you find out the different options, Now that you know how to set options and variables. Searching the web is one option, but the information is inVimType:

:set all

AndVimWill show every option in the program. Typing the:letAnd an enter will show all of the variables

The:helpCommand is used to lookup the extensive documentation built intoVimTo get out of:helpUse:qInHelpMode, this does not exit the program, but puts the editor in to normal mode

Functions

Functions are very useful in theVimrcFile. Since theVimI will from here out refer to it’s true name:, ’s configuration files use a full programing languageVimScript

InVimScriptDefine a function using:

Function <function name>()
    <function body>
endfunc

I love the line numbering and relative numbering, but sometimes you do not want relative numbering. Typing the full string:set norelativenumberOr even:set relativenumberIs a lot of typing. A function to turn it on and off would be good

Function TRelative()
    set relativenumber. Endfunc

A function is run with the:callCommand

:call TRelative()

TheFunctionCommand does not allow you to overwrite an already defined function. If that function was already defined somewhere else or you tried to reload the file againVimWill error and stop processing the file at that point. If you use theFunctionCommand, it will overwrite the function without an error. It is good programming practice inVimScriptTo mostly useFunction

A function can be created to do that and it’s opposite, To turn off all numbering. Add this to the Vimrc:

Function. NoNumber()
    set nonumber
    set norelativenumber
endfunc

function. Numbers()
    set number
    set relativenumber
endfunc

You place a variable name inside the parenthesis, To pass parameters to a function. For example:

Function. Hi( name )
    echo "Hello" a:name
endfunc

You have to scope variables that are arguments of a function, As you see. You will get, When you call the function now with a name

:call Hi( "Richard" )
Hello Richard

Basic Key Mapping

One of the most important uses for the configuration file is to setup key mappings. For example, you toggle relative numbers on and off a whole lot. Instead of typing:call TRelativeAll the time, a hotkey is much faster. The, To do thatMapCommand will help

Map <c-t> :call TRelative()<cr>

With this line in the VimrcFile, you can now typeControl-TAnd the relative numbering will toggle on and off. Try it out by making a mapping for toggling all line numbering

But for every mode, Key maps are not just for normal mode. It will map the key for that mode, If you prefix the command with the first letter of a modeNmapAlso maps forNormal modeVmapMaps forVisual modeImapMaps forInsert mode

Now that you know these command, forget them. These commands can be dangerous. It will use the new mapping and can create an infinite loop in the mappings, If you remap a key again

To prevent that happening, you need to addNoreAfter the mode and before the map. ThereforeNmapIsNnoremapVmapIsVnoremapAndImapIsInoremapThese use the default mapping for any key command you reference in the macro. These are much safer to use in the configuration file

Conclusion

In this tutorial I have shown you the basics of creating aVimConfiguration file. Set/unset options and variables, create basic functions, I have shown you how to: comment the configuration file, and mapping keyboard shortcuts. The best way to remember is by practicing

Read more: Basic Vim Configuration

0
Trackback URL for this blog entry.

Comments

  • No comments made yet. Be the first to submit a comment

Leave your comment

Guest Wednesday, 20 January 2021

Testimonial

Thank you so much! We are very happy with our new website. It is easy to use and all of our customers tell us, they love it.

Contact Us

  • 13245 Atlantic Blvd. #4352
    Jacksonville, FL 32225
  • 904-240-5823