HOW TO GET STARTED WITH VIM
- Category : Server Administration
- Posted on : Sep 05, 2017
- Views : 1,489
- By : Marcus J.
Vi is a text editor created for Unix users. The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Bill had written with Chuck Haley. The ex 1.1 editor was released as part of the first BSD Unix release in 1978. With the release of the 2.0 version of ex, in May 1979, the editor was installed under the name “vi”. Today this is commonly referred to as vi or vim editor.
Vim is an improved version of Bill Joy’s vi. It was written by Bram Moolenaar and was released in 1991. Vi is commonly used by Linux users to edit configuration files and also by developers working over shell scripts or other popular coding languages.
Here are some of the reasons why you should familiarize yourself with vi:
- vi is the closest to universal you’ll ever get
- It’s plagued with useful shortcuts
- It’s free
- It can be used to write documents, code, shell scripts, you name it
- It has a very sophisticated search system, including regex pattern matching, among other things
In today’s post, we will show you how to use vi more effectively.
VI EDITOR BASICS:
To open an existing file such as test.sh, you will have to use the command- vi test.sh. If the file does not exist, then the file will be created. By default, when you open a file using vi editor, the editor will be in view-only mode.
In order to edit this file, you need to press “i” on your keyboard. This will allow you to edit the file.
To save a file, press Esc and type :wq. This will save the file and exit the editor. You can do the same by typing Esc and “ZZ”
In case you want to quit without saving changes, you can use Esc and type :q!
To enter any command, you first need to get to the command mode by pressing Esc.
These are some of the most basic shortcuts for vi
CURSOR MOVEMENT OPTIONS:
These are shortcuts which help you position your cursor within the file.
- h – moves the cursor to the left. Similar to backspace
- j – moves down
- k – moves up
- l – moves right. Similar to a space bar
- Return key/Enter Key – Takes you to the beginning of the next line
- ^ – takes cursor to the first non-blank column of the current line
- w – moves to the beginning of the next word or a punctuation mark
- W – moves past the next space
- b – moves to the beginning of the previous word or a punctuation mark
- B – moves to the beginning of the previous word and ignores punctuation mark
- e – End of next word or punctuation mark
- E – end of next word and ignores punctuation mark
- H – moves the cursor to the top of the screen
- M – moves the cursor to the middle of the screen
- L – moves the cursor to the bottom of the screen
SCREEN MOVEMENT OPTIONS:
These shortcuts are useful while editing a large file.
- G – moves to the last line in the file
- xG – moves to line x within the file
- z+ – moves current line to the top of the screen
- z- – moves current line to the bottom of the screen
- ^F – move forward one screen. Similar to page down
- ^B – move backward one line
- ^D – move forward one half screen
- ^U – move backward one half screen
INSERT OPTIONS:
These shortcuts help you to insert or append within a file.
- r – replaces the character highlighted by the cursor with the next typed character
- R – keeps replacing the characters until escape is hit. Used when more than one character needs to be replaced
- i – Insert before the cursor position
- a – append after the cursor
- A – append at end of the line
- O – provides empty line above the cursor to add any content
DELETE OPTIONS:
These shortcuts will help you quickly delete contents within a file.
- x – deletes a character at the cursor position
- dd – deletes complete line under cursor position
- dw – deletes word under cursor position
- db – deletes word before the cursor position
CODING OPTIONS:
- yy – Also referred as Yank, it can be used to copy lines and then paste by using the put (p) command. Precede with a count to copy multiple lines
- P – Put command, Brings back previous deletion or yank of lines or words or characters. Places this before the cursor position
- p – Put command. Places this after the cursor position
- . – A dot is used to repeat the last command
- u – undoes the last command
- U – undoes all executed commands on one line
- xp – Character swap. Used to swap two characters
- J – Joins current line with next line
- ^G – displays current line number
- % – If at one parenthesis, will jump to its end or start matching parenthesis
- mx – Marks current line with character x
- ‘x – Finds line marked with character x
- :$ – Move to the last line of the file
- :# – The command followed by a number will move to the line number which is specified
- :w – saved the current file without quitting
- :r filename – copies contents of the file mentioned in the command and places it in the current file after the cursor position
- ? – Command is used with a word to be searched. Finds a word going backward
- / – Command is used with a word to be searched. Finds a word going forward
- f – finds a character on the line under the cursor going forward
- F – finds a character on the line under the cursor going backward
These commands should be more than enough to get you started with vim.
Once you get comfortable with it, you’ll never look back!
Categories
Subscribe Now
10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!Archive Calendar
Sat | Sun | Mon | Tue | Wed | Thu | Fri |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Recent Articles
-
Posted on : Jul 25
-
Posted on : Jul 07
-
Posted on : Apr 07
-
Posted on : Mar 19
Optimized my.cnf configuration for MySQL 8 (on cPanel/WHM servers)
Tags
- layer 7
- tweak
- kill
- process
- sql
- Knowledge
- vpn
- seo vpn
- wireguard
- webmail
- ddos mitigation
- attack
- ddos
- DMARC
- server load
- Development
- nginx
- php-fpm
- cheap vpn
- Hosting Security
- xampp
- Plesk
- cpulimit
- VPS Hosting
- smtp
- smtp relay
- exim
- Comparison
- cpu
- WHM
- mariadb
- encryption
- sysstat
- optimize
- Link Building
- apache
- centos
- Small Business
- VPS
- Error
- SSD Hosting
- Networking
- optimization
- DNS
- mysql
- ubuntu
- Linux