LFCS – VIM

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
340
Reaction score
367
Credits
11,754
VI is a text editor for Linux. It is a basic text editor which is command-line based and is not Graphical User Interface (GUI) based. VIM is an ‘Improved’ version (Vi IMproved).
The command ‘vim’ or ‘vi’ can be used to start VIM. The ‘vi’ command is aliased to start VIM.
The LFCS exam covers the use of VIM and its uses. Make sure you understand the uses of the various commands.

VIM Installation

For a CentOS system the following command will install VIM:

sudo yum install vim-X11 vim-common vim-enhanced vim-minimal

For a Ubuntu system the following command will install VIM:

sudo apt-get install vim vim-runtime


VIM Configuration

You may want to start using VIM, but getting the program configured may be the best way to start. Some of the settings may not be understandable until you use the program. You can go back and change the settings as you need.
Once you have VIM installed you can start a tutor program called VIM Tutor with the command ‘vimtutor’.
The configuration file should be named ‘.vimrc’ and is placed in your HOME folder. Each user that logs into a system can have their own configuration file in their own HOME folder so each user can have different settings.
To create your configuration file use the following command:

vi ~/.vimrc

NOTE: You can also use the command ‘vim ~/.vimrc’. The parameter after the command is the name of the file to edit or create.

Once you have opened the file for editing you can use the following commands to change your settings for VIM. If typing does not work press the ‘i’ key to change to insert mode.

COMMAND USE
set number numbers the lines
set showmode shows current typing mode at bottom of screen
set nonumber turns off the line numbering
set nohlsearch no highlighting when searching
set ai enables auto indent
set ts=# sets the # of spaces for a TAB
set expandtabs TABs are stored as actual spaces when file is saved
set noic When searching case is ignored

There is also the prefix of ‘inv’ which can be used with any command to make the value the inverse of the current value. The commands can be typed in at a prompt in VIM.
The best use of inverse is when you set up a shortcut key as follows:

nmap <C-L> :set invnumber<CR>

The command maps to the CTRL+L key the command ‘set invnumber’ followed by the ENTER key. The ‘invnumber’ will enable the line numbering if it is disabled and vice versa.
Another big help is the abbreviation command. If you create a script file for BASH, you can use the following command:

abbr _bash #!/bin/bash

The command allows you to type the abbreviation ‘_bash’ and it will be replaced with ‘#!/bin/bash’ to specify the script is for BASH.
To save the file press the ‘ESC’ key to enter into command mode. Press the ‘:’ key to accept a command and then type ‘wq’ to ‘write’ the file and ‘quit’ VIM.
The best way to learn VIM is to go through the VIMTutor program.

VIM Tutor

The VIMTutor program is basically a file which is loaded into VIM and is used to teach the various commands and key presses. The tutor file can be edited while you are learning the various commands as instructed. Using the tutor is a very easy way to learn VIM and I highly recommend it. If you use VIM often then you should easily remember the various commands.
Do not simply go through the tutorial, but learn the commands since they are covered in the LFCS Exam.
If you should use the ‘set showmode’ option then you can see the mode at the bottom of the VIM screen. If there is no mode showing at the bottom then you are in ‘Normal’ mode. In Normal mode you can type the shortcut keys, like ‘i’ for ‘insert’ mode. If you are not in Normal mode and want to get into Normal mode then press the Escape key. Once in Normal mode you press the colon ':' key to start the command-line entry.

Let me give a brief description of what you should learn through VIMTutor:

  • Use arrows or the keys K (up), J (Down), H (Left) and L (Right) for maneuvering the cursor. Hold down a key to move faster.
  • Use Escape to enter into Normal mode. Type a colon :)) to enter into Command mode. Use ‘:q!’ to quit without saving changes.
  • In Normal mode use the ‘x’ to delete a single character.
  • In Normal mode use the ‘i’ to insert text before the cursor.
  • In Normal mode use ‘A’ to append text.
  • Use Escape ‘:wq’ to write the file and quit VIM.
  • Put the cursor on the first letter of a word and press ‘dw’ to delete the word. If the cursor is on another letter then the highlighted letter to the end of the word will be deleted.
  • To delete from the cursor to the end of the line use the keystroke ‘d$’. The ‘$’ means to the perform the delete command to the end of the line.
  • Use the ‘d’ key to designate ‘delete’. The ‘d’ key can be followed by a motion key. The motion keys are:
    • w – start of next word not counting first character
    • e – end of current word including last character
    • $ - end of line including last character
  • Motion keys to move the cursor are as follows:
    • w – first letter next word
    • e – last letter of the current word
    • $ - end of the current line
    • ^ - beginning of the current line
  • Use a number before a motion to specify number of words, etc. For example, to delete 2 words use the command ‘d2w’ to delete two words.
  • Use a number before a motion command to move the cursor. For example, ‘2w’ to move the cursor two words. Use with ‘w’ and ‘e’.
  • Use ‘dd’ to delete a whole line. Can use numbers to delete more, such as ‘3dd’, will delete three lines.
  • To undo a change use ‘u’. To undo all changes made to the current line use ‘U’.
  • Use CTRL+R to redo or undo an undo.
  • Use ‘p’ to Paste a line. The ‘p’ can be used after a ‘dd’.
  • Use ‘r’ to replace the character under the cursor. So, ‘rc’ would replace the current character with the letter ‘c’. After replacing the character the editor will go into Insert mode. Press Escape to enter back into Normal mode.
  • ‘c’ is used to change characters and can be used with a motion. For example, ‘c$’ will delete the line from the cursor to the end of the line and go into Insert mode to change the text. Use ‘cw’ for erasing and changing a single word.
  • CTRL+G will show file status and cursor location at the bottom of the screen. To always see the cursor position use the command ‘:set ruler’.
  • To move to the end of the file use ‘G’. To move the cursor to the beginning of the file use ‘gg’. To go to a specific line use #G. For example, to go to line 11, use the keystrokes ‘11G’.
  • To search for a string of text in the document use ‘/’ followed by the string to find. For example, to find the word ‘test’ type ‘/test’. The search will find the first occurrence after the cursor. To find the previous occurrence before the cursor use a ‘?’. For the previous example, it would be ‘?test’. At the end of the file the search will then start at the beginning of the file. To move forward to the next found search string use the ‘n’ key or the ‘N’ to go backwards. CTRL+O goes backward and CTRL+I goes forward a found search string.
  • To search for a string and ignore case use a ‘\c’ at the end of the search string. For example, ‘/test\c’ would find all variations of lower- and upper-cased text. For example, ‘test’, ‘Test’, ‘TEST’, ‘tesT’, etc.
  • Place the cursor on a bracket character (, ), [, ], {, or } and use the percent key (%) to find the matching paired bracket.
  • To substitute words you will use command mode. To substitute a single word use ‘:s/word1/word2’ where word2 will replace word1. To substitute all found words on a line use ‘:s/word1/word2/g’. To substitute all found words from line1 through line2 use the command ‘:line1,line2s/word1/word2/g’. To substitute all found words within the file use ‘%s/word1/word2/g’. To replace all matching words in a file and prompt to replace them use ‘%s/word1/word2/gc’.
  • To execute an external command from outside of vim use ‘:!<command>’ where ‘<command> is your external command. The command will be run and then you will be prompted to press <ENTER> to close the command window. For example, ‘:!ls’ will display a listing of the files in the current directory.
  • To write a file using a new filename use the command ‘:w filename’. For example, to save the current text in a file named ‘frog’ the command is ‘:w frog’.
  • To select text by highlighting it press the ‘v’ key in Normal mode. Use the cursor movement keys to select the text you want.
  • Any selected text can be saved to a file with the command ‘:w filename’ once text is selected.
  • Selected text can be deleted by selecting the text and pressing ‘d’.
  • Selected text can be copied to the clipboard using ‘y’ and pasted at the cursor’s position using ‘p’.
  • The contents of another file can be inserted at the cursor’s position by using the command ‘:r filename’ where ‘filename’ is the name of the file to insert.
  • To open a blank line below the cursor use the command ‘o’. To place a blank line above the cursor use the command ‘O’.
  • To append text after the cursor use the command ‘a’. ‘A’ inserts at the end of the line. To insert before the cursor use ‘i’.
  • To replace characters, or overwrite mode, use ‘R’.
  • To copy a word to the clipboard use ‘yw’.
  • To get help use the F1 key or the command ‘:help’. To move from one window to another use CTRL+W and ‘:q’ to quit help.
  • When using a command you can have the command auto-completed by using the TAB key. For example, enter command mode (press ESC) and then ‘:sav’ then press TAB. The command will be completed to ‘saveas’. If multiple possible commands are available you can cycle through them by pressing the TAB key until the proper command appears.
These are the basics of VIM and I cannot impress enough to be familiar with its use. Use the VIM editor when you can to be very familiar with it before the LFCS Exam.
 

Staff online


Latest posts

Top