Tuesday, July 18, 2017

Commonly Used Vi Vim Commands

REFERENCE: https://manandharsabbirk.appspot.com/articles/populate_article.html?id=mjdr45v1gsfwzyc/vi_common_commands.html

Show Line Numbers

:set number
This command is used to displaying line numbers in the editor.
:set nonumber
This is the counter command to the first numer. That is, this command will hide the line numbers.

Auto Indent

:autoindent
This command will auto indent the line as the previous line.
At any time, if you need to remove the indentation of the line you can press Ctrl+d.

Switching to New File

If you are working on a file in VIM and want to switch to a new file then you can use the following command:
:e    <file_name>

Split Screen

Sometimes we want to compare 2 files or the segments of same file. At such time, split feature of vim comes in handy. Screen can be easily split in vim. 

Split Screen Horizontally

:split
This code will split the screen horizontally. You can pass optional filename, if you want to open new file in the new window. If you don't pass any value, then the same file will be opened.

Split Screen Vertically

:vsplit
This code will split the screen vertically. Similar, to horizontal split, you can also pass a filename to open in the new window. 

Navigating the Splitted Screens

You can navigate the splitted screens by pressing Ctrl+w TWICE.
You can also do this by pressing Ctrl+w and then using the ARROW keys.

Close / Unsplit the Splitted Screens

CLICK HERE

Move to the End of Line

$

Move to the Last Line

:$

Move to a Line Number

:<line_number>
Note: :$ will move to last line.

Delete All

:1,$d

Copy/Paste Lines

yy
This command will copy the current line.
<number>yy
This command will copy <number> lines starting from the current line.
p
or
P
This command will paste the copied text.

Cut/Paste Lines

dd
This command will cut the current line.
<number>dd
This command will cut <number> lines starting from the current line.
p
or
P
This command will paste the copied text.

Copy/Cut Paste Selected Text

  1. Place the cursor to the position from where the text is to copied.
  2. Press v to begin the text selction.
  3. Move cursor to select the text.
  4. Press y to COPY or d to CUT the selected text.
  5. Press p or P to paste the text.

Insert Text (Spaces) in Multiple Lines at once

  1. Within VIM select block visual mode, by pressing CTRL-V .
  2. Use the cursor keys to select the lines you want to add the spaces to.
  3. Then enter :'<,'> norm I
  4. This will then insert a single space to the beginning of your select lines.
Lets break down the command :'<,'> norm I
  1. :'<,'> : For the line I selected
  2. norm : Execute the following sequence of keystrokes as if I was in normal mode
  3. I: Insert at the beginning of the line the following characters
  4. [space]: The character(s) you would like to insert