Vim Basics

These are my notes on Vim Basics.

Samsung Store on Amazon! If you buy something, I get a small commission and that makes it easier to keep on writing. Thank you in advance if you buy something.

Installing Vim

The best way to install Vim is to use the package manager for your operating system. All operating systems have one. Windows uses Chocolatey, Macs use Brew, and Linux distributions all use a differnet one. You can see if you have Vim installed by opening a terminal and typing: vim. If it is installed, a new window will open and you will see it says Vi improved. Vi was the default editor before Vim. So, Vim is just the improved version of Vi. Vim comes installed on most versions of Linux. If it is not installed then use your package manager. I am using Fedora so we will install it like this:

sudo dnf install vim

Starting Vim

There are multiple ways to start Vim. I usually start with a new file myself. There is no particular reason other than I am editing a certain file or starting a new file like this one.

vim vim_basics 

If the file does not exist then Vim creates the file with the name you specified.

You can start a file with options. I suggest knowing about these as they may be useful at some point later on.

vim filename --clean 

This will imitate a fresh install of neovim. It skips initializations from files and environment variables. It will also exclude user directories.

vim filename --noplugin 

This option does not load any plugins. It can be useful when evaluating the effects of a particular plugin.

vim filename --startuptime 

This option can be used to find out how long plugins affect your startup time.

Saving Your File

There are many ways to save your file, all with different nuances. Saving is done in command mode. To save your file do this, :w. This will save your file. Again, you must be in command mode to do this. You can save and quit at the same time by typing, :wq. Remember, all commands must be entered in command mode.

Exiting Your File

There are many ways to exit your file in Vim. It will be done in command mode. So, if you are in edit mode, hit escape to exit to command mode. :q (Closes and does not save any changes) :quit (Same as above, closes and does not save any changes) :quit! (Also, same as above, closes and does not save any changes) :qall (Closes all windows and does not save any changes)

Editing Your File

Editing a file is a little different than doing so in a software suite. The reason is because Vim has different modes. These modes allow you to do many things. One mode lets you enter commands, another lets you type text, and so on. The keys on your keyboard do different things depending what mode you are in.

The (normal) mode lets you enter commands. You start by typing the colon character (:). The colon character starts a command. That is why when you are wanting to exit your file, you type (:q). The colon character starts a command, the (q) letter is short for quit and Vim will then exit your file after you press enter. To edit your file or just start typing, you want to be in (insert) mode. From the (normal) mode, press the letter (i). This puts you in edit mode and you will see (-- INSERT --) at the bottom left of your terminal screen.

This lets you know you are in (insert) mode and ready to type. Hit the (escape) key to exit (insert) mode and go back to (normal) mode. Try it a couple times to get familir with entering and leaving different modes.

Movement Commands

These commands are used in (normal) mode.

(Shift-I) enters (insert) mode and moves your cursor to the beginning of the line.

(Shift-A) enters (insert) mode and moves your cursor to the end of the line.

(Shift-O) enters (insert) mode and opens a new line above your current line.

(H) moves cursor left

(J) moves your cursor down

(K) moves your cursor up

(L) moves your cursor right

You can combine numbers with these commands to do them (x) times. If I want to move my cursor left 5 spaces, then I type: 5h. If I want to go down 3 lines I type: 3j. If I want to go up 7 lines I type: 7k. Then, if I want to go right 6 spaces I type: 6l. I am sure you get the idea now. You can also combine the numbers with the arrow keys to get the same results.

We can also move around from word to word. We use the: w to move forward word to word. Also, we can reverse that by using the: b letter to go backwards word to word Then, we can use the letter: e to move to the end of the current word. In normal or visual mode, use 0(zero) to move to the beginning of a line.

When you have line numbers enabled, you can move to a certain line by typing :50, this will move you to line 50. You go to the end of the file by pressing :$.

Settings In vim

Vim has many setting you can activate to customize your editing style. Line numbers are nice, so let's do that. In (normal) mode type, :set number. You will now see line numbers in your file. Another cool thing you can do with line numbers is make them relative, :set relativenumber This shows line numbers away from your cursor.

This can be very helpful but it does take some getting used to. Just remember it is an option. Many experienced Vim users do not use a mouse when editing a file. However, if you need to for some reason there is a setting for it, :set mouse=a. You can adjust your tab behavior, :set tabstop=5 Your colorscheme is modified like this, :colorscheme blue You can see the color options by pressing tab after :colorshcme

Saving Your Settings

All these settings are cool, but when you close your session, the settings go away. We want to keep these settings every time we open Vim. Therefore, we need to use a settings file, also called a configuration file. The settings file is called (.vimrc). Type:

vim ~/.vimrc

This will open up a blank file, unless you alread did this step before this tutorial.

We will just enter some setting I mentioned above. Then, when this is set up, every time you open Vim your settings will still be there. So, in your blank (.vimrc) file, type:

set number

set relativenumber

set tabstop=4

set shiftwidth=4

set autoindent

set mouse=a

colorscheme blue

Modes In Vim

There are different modes in Vim, as mentioned earlier. There is a purpose to each and it is a good idea to be able to switch and work in the different modes as needed.

Normal = In normal mode, it is the top most mode, use commands here to modify the bahvior of your environment.

The commands I have listed above are in normal mode.

Insert = In insert mode, type i from normal mode, this allows you to enter text and edit files. As may be obvious now, insert mode lets you type. I am in insert mode now as I type this.

Visual = In visual mode, type v from normal mode, this allows you to select text and perform operations on it.

In visual mode, go to the beginning of a word and move tom the right. You can also go to the end of a word and go to the left. Once selected, you can do various things to the selection. Typing (d) in visual mode with something selected will delete it. Selecting 3 chars and hitting (d) will just delete those 3 characters. With something selected you can type (y) to copy the selection. Go somewhere else in the file and type (p) and it will paste what you just copied.

Numbers work with all of these actions. (10p) will paste the selection 10 times. Typing (dd) will delete the entire line. Typing (yy) will copy the whole line.

Visual Line = To use this mode, hit (shift-v) and then we can select whole lines at once. Once lines are selected, we can hit (y) for copy, or (p) for paste, or (d) for deleting all of them at once.

Visual Block = To use this mode type (ctrl-v) and we can select columns at a time. We can then perform the usual operations just like the above examples.

Line Manipulation

Vim has many ways to move around and peform different actions on words and lines. These are also in (normal) mode. We can undo a certain action by pressing: u. We can do it 3 times by doing: 3u. There is also a redo action: ctrl-r. Then, we can redo a certian action 5 times by: 5 ctrl-r.

We can indent easily. Select a portion of text then type the: == characters. Indenting the whole file is done by: gg=G. This moves your cursor to the beginning of a file, then indenting until the end of the file denoted by the G character. Somethign else that is useful is moving to the beginning of a file. We do this by typing (gg) when not in insert mode.

Searching In Vim

We search in Vim by using the / character when you are not in insert mode. So I can search /mode and Vim will highlight the occurrences of that word. Once they are found, you can hit the letter (n) to move through each found word. When you are done, type :noh to clear the highlighting.

After we search for something, we can replace the word we searched for with something else. To do this we enter normal mode, then type

:%s/vim/neovim/g

The colon starts a new command, (%) refers to the whole file, (s) substitutes, / is the start of a search in Vim, vim is what we are replacing, neovim is what we are replacing vim with, and (g) then press enter.

Now, we can also select something and then replace a word with something else that only appears in the selection. We do this by selecting a portion of text, then typing

:s/character/symbol/g

and pressing enter. The colon starts a new command, (s) is for the selection only, character is what we are replacing, and symbol is the word we are replacing character with.

Waypoints In Vim

If you go to a certain line or word in a file over and over, it makes to set a waypoint. This is easily done and very useful when you have a file hundreds of lines long. To do this, hit the letter (m) followed by any other letter. I like to start with (a) and move down the alphabet for each way point. To move to the different waypoints, type ('a), ('b), or ('c) to move to those 3 waypoints.

Register

The register in Vim says what is stored in its memory. These can be things copied or deleted in its memory. Macros are also stored and you should see them there. You show the register by typing :reg Hit enter to exit.