note this guide will be added to many times, the content is true, but all of vim will not be here as of now. Check back often. Everything will be covered here, from themes to adding lines, the whole lot. When I read documents I always wish that I was watching a video because I am a visual learner. Eventually after each vim item I will include a compressed gif file to show what happens visually. This guide is for those who want to learn vim.
Note, do not input the plus sign (+) on commands, even tho they are listed, this is just to tell you to not hit the enter key
Whats vim?
vim is a text editor that is included by default in
basically all Linux distro's. vim is not a GUI ( graphical user interface ) editor like leafpad or notepad, its a TUI ( text user interface ). vim is mostly used for programmers because it is extremely flexible and includes many keyboard shortcuts to improve coding speed and efficiency. vim is also widely used in the IT field.
An example of its speed and efficiency, would be too, imagine you insert about 20 lines of code, that are now deemed redundant by your higher ranking boss. In another editor to delete all 20 lines of code, you would have to move your hand from the keyboard to your mouse, highlight the redundant code and hit the "delete" key. While with the power of vim you can escape into "command mode" and insert
:1,20d
This will delete the lines 1 to 20 without having to move your hands from point A to B and then back to A.
I do not have it, where can I get it?
If you do not have vim on your install you can easily install it from your distro's repository.
For the Debian guys and gals:
sudo apt-get install vim
For the Fedora guys and gals:
yum update vim-minimal
and then
yum install vim
For the Arch guys and gals:
pacman -S vim
If you can get a 404 error update your mirrors by using the command:
pacman --Syy
Basics
Opening vim
Open your terminal and type in:
vim
And now you are inside vim. Off the bat you are presented with a "welcome screen" that gives you helpful commands to get you started. As well as what version you are running.
Modes
There are three modes in vim "command mode" "visual mode" and "insert mode".
After opening vim you are in "command mode". You can smash your keys on your keyboard as much as you like but either no text will show on the editor or only a couple letters out of a whole word.. In command mode you can not type text.
To type text you must enter "insert mode". To get into insert mode you must push the "i" key on your keyboard. On the bottom of vim, vim will tell you that you are in insert mode. You can now edit the document.
To leave "insert mode" for "command mode" hit the escape key and you will be back into "command mode".
RECAP
After starting vim you are in "command mode" a mode where you can not type text. In order to type text you must hit the "i" key on your keyboard to enter "insert mode".
Saving your file
Saving a file is easy. If you are in "insert mode" hit the "ESC" key to enter "command mode". Once inside "command mode" hit the colon key on your keyboard then the "w" key followed by the name you want to give to the file. Then hit enter.
I am naming my file linux.txt.
:w linux.txt
If you do not give the file a name, vim will not save the file. It will spit out "E2: No file name" letting you know that the file needs a name.
A prompt comes back with the name of the file then the number of lines that are in the document followed by the character count.
My linux.txt output was
"linux.txt" 1L, 5C written.
Where does vim save my text document?
vim saves the document in the current working directory. So if you open vim while you are in your home directory, create and then save a file, the file will be stored inside the home directory.
If you do not want vim to save your work in your current working directory, after the **:w ** enter the path to the directory you want your work to be saved, followed by the name of the file.
Example, I want to put this file under the LOTR directory: :w /home/Frodo/LOTR/linux.txt
Saving changes made to an existing file
Typing
:w linux.txt
every time a change is made to that file would be a real pain the butt. Luckily, we do not have to type out the full file name to save a file once the file already has a name.
After making the changes you want, enter "command mode" and hit the colon key and the w key.
:w
This will overwrite the existing linux.txt text and save the changes.
Quitting vim
To close or to quit vim, enter command mode and hit the colon key and the "q" key on your keyboard and then hit enter.
:q
This will bring you back to your terminal.
To quit vim and have your changes saved at the same time enter command mode and enter:
:wq
in your keyboard. This will write the changes to the file and will then quit vim.
To quit vim with out saving changes hit the colon key, the "q" key and the "!" key followed by the enter key.
:q!
If you do not input the "!" vim will show "E37: No write since last change (add ! to override)." Basically its saying "you just edited this file, don't you want to save this file?" Just as any other editor does.
Opening a file with vim
To open a file with vim, simply type in your terminal vim and the name of the file you want to edit/view.
To open my linux text file I would type:
vim linux.txt
and vim would open that file for me.
If I where to want to open linux.txt with vim and have my cursor on the 30th line, in my terminal I would type: vim +30 linux.txt
By default opening vim brings you to the top of the document. If I were to want to start editing at the end of linux.txt in the terminal I would type type: vim + linux.txt.
Navigating vim
In vim you can use the arrow keys to navigate around your file. However, moving your hand from the home row keys to the arrow keys and then back to the home row keys to start typing delays your ability to get work done.
To get around this vim uses the same principles video game studios use and put movement keys around or on the home row keys.
In vim, the movement keys are: j,k,h,l,w,b,0.$
- j is one line down
- k is one line up
- h is one character to the left
- l is one character to the right
- b moves one word to the left
- w moves one word to the right
- The 0 character moves you to the beginning of the line you are on.
- The $ character moves you to the end of the line you are on.
Now this is kind of tricky, because if you want to move down in the document hitting the "j" key may not bring you down in the document. This is because you are in the wrong mode. You must be in command mode to do this.
If you accidentally typed "j" or "k" just delete those letters hit "ESC" to get into command mode.
If you know what line you want to go to you can easily jump to that line by using the "G" key in command mode.
Say we want to go to line 15. On our keyboard we would input the numbers 1 and 5 and then the upper case G. This would take us to line 15.
1+5+shift+g
If you want to get the end of the document just use the "G" key.
shift+g
If you want to get to the top of the document enter the number one key "1" and then capital "G".
1+shift+g
If you would like to move 3 letters to the left enter the number "3" and the letter "h" on your keyboard. 3+h
Command Mode
If you want to enter insert mode at the end of a line use the upper case "A". So shift+a.
If you want to enter insert mode after the cursor use the lower case "a" key.
shift+i will enter you in insert mode and bring you to the first non-blank character.
The "i" key will enter you into insert mode.
To insert the date into your text use colon key followed by the "r" character followed by an "!' and the word date. :r!date This would read the output of the date command and place it on the line under your current cursor position.
-
To copy more then one word to the right of the current cursor position one would use the number of the words to the right that they would want to copy and then the"y" and "w" letters.
If I could want to copy 3 words to the right I would use the characters "3" "y" and "w". 3+y+w.
To paste on the same line your cursor is on use "p".
To paste above the current cursor position use the upper case "p".
To undo hit the "u" key on the keyboard and this will undo the changes you made.
To delete a character that is highlighted by the cursor hit the lower case "x" key.
To delete a whole line of text, arrow to the line that you want to delete and hit the "d" key twice on the keyboard. This also copies the line in vim's frame-buffer. This allows you to easily move that line to another part of the document if you placed it some where by mistake.
To delete a word and then enter insert mode to start typing hit the letters "c" and then "w". c+w over the word that you want to delete.
To delete the whole word that you are on use the keys "d" and "w" d+w with the cursor over the beginning letter of the word you want to remove.
-
To delete part of a word, use the "d" and "w" keys.
Say you meant to put st. instead of street, you would hover your cursor over the "r" and use the keys "d" and "w" d+w. This would delete the "r" the "e" the second "e" and the "t". Basically the character you are on to the end of the word.
To delete to the end of the line use the upper case d. So shift+d would delete from your cursor right to the end of the line.
To redo the changes hit the "ctrl" and the "r" key at the same time.
To replace a character with another character hit the "r" key and replace the character with whatever character you want to replace it with.
-
If you are looking for something, hit the "/" key and then whatever you are looking for.
If I were to be looking for ramen I would do /ramen and vim would bring me to the word ramen.
- Using the "n" key will bring me to the next occurrence of the word ramen.
-
To find and replace one would first look over this formula. :%s/Ubuntu/Arch
The percentage sign in the formula describes that I want vim to replace every occurrence of the word Ubuntu with Arch in the entire document. The lower case "s" means that I want vim to search for the word in the first set of back-slashes.
After hitting the enter key on the bottom left, vim tells you how many substitutions were made, and how many lines were affected by the substitution.
-
To make vim search for and replace for the word even if the case does not match use the "i" variable.
:1,$s/Ubuntu/Arch/i Would search for Ubuntu and replace Ubuntu even if it were lower case.
The arrow keys allow you to go back and forward in history, allowing for the user to quickly implement the command they want to use.
Insert Mode
Visual Mode
Visual mode lets you select text, copy it and paste it.
To enter visual mode hit the "v" key on your keyboard.
To select the line of text you are on and the line under it use the "j" key twice. After those two lines are selected use the "y" key. In the bottom left, it will say "2 lines yanked". This means you have successfully copied two lines of text.
.vimrc - - S/O @lucasblotta
Do not edit /etc/vim/vimrc doing so may damage/break your vim install. Most distro's do not include a a .vimrc file by default in their vim package. To create a .vimrc file to customize your vim editor in your terminal type:
vi .vimrc
You will now have your own editable .vimrc.
How to display line numbers
To expand vim permanently, one must edit the .vimrc file. To temporarily make changes to vim, one enters variables into the "prompt" - the text box at the bottom of vim.
One cool customization that every vim user has in their .vimrc file allows them to view line numbers in vim.
All one must do is add the line:
set number
and then save their .vimrc file. The next time you start vim line numbers will appear on the left side of vim.
If you want to temporarily turn this feature off, enter command mode and type:
set nonumber
now the line numbers will not show. This is of course temp. and the line numbers will show again the next time you start vim.
If you only want vim to show line numbers for a few cases and do not want to edit the .vimrc file you can enter command mode and type:
set number
this will temporarily turn on line numbers. As mentioned before, the line numbers will not show the next time you open vim.