Linux vim Editor command

About vim :

vim, which stands for “Vi Improved“, is a Text Editor. It can be used for editing any kind of text and is especially suited for editing computer program.

Description

vim is a text editor that is upwards compatible with Vi. There are a lot of enhancements above Vi: multi-level undo, multiple windows and buffers, syntax highlighting,  command line editing, filename completion, a complete help system, visual selection, and others.

Screenshot from 2018-07-28 18:25:46.png

Vim has a particular working method, there are two main modes:

the command mode and the other modes.

The command mode lets you select the working mode that you want to enter.

Available modes are: save, quit, copy, paste and that kind of things but you can’t edit the file in the command mode directly.

This is what many users that are new to vim puzzles and one has to get used to first.

Vim modes

There are several other modes, I’ll cover only the most widely used ones here.

Insert Mode

The Insert mode lets you insert text in a document. The shortcut is: “i” (insert text where the cursor is) or “o” (insert text at the beginning of the following line).

Visual Mode

The visual mode permits the user to select the text like you would do with a mouse, but using the keyboard instead of the mouse. Useful to copy several lines f text for example. The shortcut is: “V“.

Command Mode

Let’s now speak about the command mode, a command begins with the symbol “:”.

When you are in another mod you can use the escape key (sometimes you’ll need to hit it twice) to come back to command mode at any time.

Vim usage example

To start using vim, just run the “vim” command on the Linux shell followed by the path of the file that you want to edit.

Opening terminal: alt+cntr+enter,

then follow Example: Writing c code using vim editor

vim c_code_with_vim_editor.c

Screenshot from 2018-07-28 19:26:32.png

and then hit enter.Screenshot from 2018-07-28 19:30:14

The editor is now in command mode. To start editing the file content, enter:

:i[enter]

[enter] means to press the return or enter key on your keyboard.

The word –insert– will appear at the bottom of the editor window to show that you are in insert mode now.

crop_vim

Now you can edit the file by navigating to the line that you want to change with the cursor keys and then start typing the text.

Screenshot from 2018-07-28 19:51:19

When you are finished with editing, press the [esc] key to go back to the command mode.

Screenshot from 2018-07-28 19:52:17

To save the file and exit the editor, enter:

:x[return]

Screenshot from 2018-07-28 19:53:36

In case you want to quit vim, saving the file, enter:

:wq[enter]

Screenshot from 2018-07-28 19:54:49.png

you can see the file (c_code_with_vim_editor.c) you just have created,

typing following  command: cat c_code_with_vim_editor.c [enter]

Screenshot from 2018-07-28 19:57:41.png

and for compiling and running you may use following command’s :

Compiling: gcc c_code_with_vim_editor.c [enter]

Executing: ./a.out

Screenshot from 2018-07-28 20:00:47.png

 

Vim Command Reference

save: :w
save and exit: :wq
exit: :q
force: ! (example :w! :q!)
vertical split: open a document and then type: split /path-to-document/document and this will open the specified document and split the screen so you can see both documents.
copy: y
copy a line: yy
paste: p
cut: d
cut a line: dd

These are the very basic commands for vim, but they are useful as vim or vi is preinstalled on most Linux systems. I hope this will help you configure your Linux.

One comment

  1. VI was always weird to me – only because when I was in college, there were two major editors on the Vax – one was Vi and the other was Emacs and for whatever reason, I went with emacs and it seemed perfect. Then I’d see people on vi typing all of these :commands, etc and I’d be hitting ^k^s or some similarly cryptic. I probably should have learned both.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s