blogs.longwood.edublogs.longwood.edu/.../2017/05/beginners-guide-to-vim …  · web viewthis is a...

9
Beginners Guide to Figure 0: http://rayhightower.com/blog/2013/01/12/why-i-use-vim/ Simhran Patel Longwood University

Upload: phungnhi

Post on 21-Mar-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Beginners Guide to

Figure 0: http://rayhightower.com/blog/2013/01/12/why-i-use-vim/

Simhran Patel

Longwood University

Beginners Guide to Vim This is a guide for beginners who are interested in learning vim on a Linux operating

system. This manual will show why vim is very useful for people who code. It will also show the new user how to navigate through vim and see the features that it offers to coders. This manual will be more helpful to readers who uses and understand Linux. The manual will first start off talking about what is vim and why you should use it. Then the manual will go into detail on how to use vim and how to navigate it. Pictures, diagrams, and tables will be shown to help you follow the instructions. By the end of the guide, you will understand and will be comfortable in using vim.

What is Vim and why use it?Vim is a type of editor that is very known to be used in a Linux operating system. This

editor is a modal text editor, which means that you use the keyboard to navigate and to use vim. Why coders should use vim is because of it being a modal text editor. This allows the user to keep their hands on the keyboard, making it faster and more efficient for the user to code. It is difficult to get a grasp on using vim, but once you get it, you will see an improvement in your coding. Any type of coding language can be used by vim, and you will see that vim will help you understand more of your favorite language.

How to use VimLet’s get started! First let’s talk about what you will need to start! Here is a list to help you know what you

will need.

A computer that has any type of Linux operating system A working keyboard You would need to know how to open your terminal on your computer.

Once you have all those things we can now start. Let’s first learn how to open a vim file.

How to open a vim file1. Open the terminal in your Linux operating system. 2. Once you are in the terminal, in the command line you will type in vim and then the

name of your text file name. Then press Enter.

Figure 1: what the screen should look like when you are typing in your command line to open your vim file

3. A blank screen will appear, and at the bottom will have the name of the file and in brackets will say New File.

Figure 2: What the screen should look like when you are in the vim file

4. You have now learned how to open a vim file!

Now let’s talk about how to write, move, edit, save and exit a vim file. To do this you will learn about the three different types of Modes that vim has.

The different types of Modes

There are three different types of modes to use in vim:

Insert mode Command mode Last-line mode

Insert mode

Insert mode allows you to type in the file. This is where you will type in your code. Here is how to get into Insert mode and type in it.

1. Open your vim file2. Once you are in your vim file just type in i and press enter3. It will show you the same blank screen and the word INSERT at the bottom of the file.

Figure 3: what the vim file looks like in Insert mode.

4. You can now type anything you want in this file.

Figure 4: Example of typing in a vim file

5. Once you are done typing, to get out of Insert mode, press the esc button on the keyboard. 6. The word Insert at the bottom of your screen will be gone, but what you have typed in

Insert mode will still be there.

You have now learned how to type in a vim file! Now let’s talk about moving and editing in your vim file. To do this we need to talk about the Command mode.

Command mode

Command mode is the default mode that vim will start up on every time you open a vim file. This is where majority of the editing happens in vim. There are many commands for editing and moving around in the vim file, and all is used with the keyboard. Before we start; make sure you are not in Insert mode, if you are in Insert mode the commands that you type in will show in your code.

How do you know you’re in Command mode?

Unlike Insert mode; to get into Command mode there in no typing on your keyboard to get in or out of it. When you open your vim file or esc from any mode you are in Command mode.

Moving in vim

Now let’s talk about how to navigate around the file, then we will look on how to edit the file. Here are some basic commands to help you move through the vim file fast and easy.

Basics commands for moving in vim

h Moves the cursor one character to the leftj Move the cursor down one linek Moves the cursor up one linel Moves the cursor one character to the rightO Moves the cursor to the beginning of the line$ Moves the cursor to the end of the line

w Moves the cursor forward one word

b Moves the cursor backward one word

G Move cursor the end of the file

gg Moves cursor the beginning of the file

Figure 5: a table for commands to move the cursor in Command mode

Practice moving around in vim with the new commands that are listed above in a vim file you created. You may want to have multiple lines written in your vim file so you can practice all the commands. You just learned how to move through a vim file!

Editing in vim

Now that you know how to move through a vim file, let’s talk about how to edit a vim file in command mode. Like moving through a vim file, you will be using the keyboard to edit your file as well. Here are some basic editing commands that every beginner should know.

Basic commands for editing in vim

dd Deletes an entire linedw Deletes a worddo Deletes to the beginning of a lined$ Will delete to the end of a linedgg Deletes to the beginning of the

filedG Deletes to the end of the fileu Undoes the last operation/text

Search for texts in a document

c To cut a liney To copy p To paste V Highlights one line at a time

v Highlights one character at a time Figure 6: a table for commands to edit in Command mode

These are the few commands that every beginner should know, if you are curious about more commands here is a cheat sheet that shows more commands. This cheat sheet also will help you remember other basic commands.

Cheat Sheet

Figure 7: a cheat sheet to show where commands are located on keyboard

If you like to see a bigger picture of the cheat sheet go to this link. http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

You should practice these commands in a vim file. You should have multiple lines so you can move around and edit your file. You have now master how to move and edit a vim file!

Last-line mode

Now that you know how to write, move, and edit your vim file, it’s now time to learn how to save your file. This happens in the Last-line mode, here are steps into getting into this mode.

1. Make sure you are in the command mode2. Type on the keyboard the colon button : (press shift and : ) then press enter3. You are now in Last-line mode

Now that you are in this mode, you can now save and exit your vim file. Like the command mode, you will be using the keyboard to save and exit. To do this here are commands that allows you to do this.

How to save and exit

:w Save a file:q Exit a file without

saving it:wq Save and exit the file

Figure 8: a table of commands in Last-line mode

Practice these new commands with a vim file. Remember if you are editing a vim file and want to exit it, be sure to save the file before exiting, or all your work will be gone. You have now learned how to save and exit a vim file!

You are all set!You now understand how vim works and how to use it. You understand the three modes of vim and what each mode does. You’ve learned commands to help you navigate, edit, save and exit a vim file. You also practice in each mode to understand how they work. Even though it is difficult to only rely on the keyboard to use a vim file, it will get easier to use with more practice. Best of luck to all new vim users!