Cheat Sheet Vi & Vim Editor
Below I want to summarize some useful commands to deal with the iconic vi (pronounced vee-eye) and vim (Vi Improved) editor. I will add content here from time to time.
Vi was released in 1976. Nowadays Vi is often a symbolic link to Vim or an alias to Vim, an enhanced version of Vi.
Below for example on a SUSE Linux Enterprise 15 OS you can see that both, vi and vim will have a symbolic link to the same version (binary) vim-noxll.
To find the path from an executable file you can use the whereis <command> command btw.
One import point to first note are the different modes that determine how the editor behaves and how it responds to keyboard input. The primary modes are:
- Normal Mode
- Insert Mode
- Visual Mode
- Command-line
- Replace Mode
I will focus in this post on normal mode and insert mode.
When you start vi, you are in normal mode. In this mode, you can navigate through the text, delete, copy, paste, and perform other editing commands.
To enter normal mode just press the Esc key.
The insert mode allows you to insert or append text. In this mode, what you type is inserted directly into the document.
To enter insert mode just press the i key to insert before the cursor or I to insert at the beginning of the line. Further you can press A to insert at the end of the line or o to open a new line below the current line.
To exit insert mode just press the Esc key to return to normal mode.
- Quit Vi without saving
- Quit Vi and save File
- Move cursor to the first line and character
- Move cursor to the last line and character
- Copy all content from file to the clipboard and paste it to another file in Vi
- Duplicate a Line
- Delete a Line
- Delete all Lines
- Search for a Character String in Vi
- Change Color Scheme
- Links
Quit Vi without saving
In normal mode quit vi and do not save changes.
:q!
Quit Vi and save File
In normal mode quit vi and save file.
:wq
Move cursor to the first line and character
In normal mode type gg.
Move cursor to the last line and character
In normal mode type G.
Copy all content from file to the clipboard and paste it to another file in Vi
In normal mode type 99999yy. All lines are copied to the clipboard and the message below appears.
Now we can open a new file. Here for example I will enter vi and the name of the new file on the command line which will then open the vi editor.
# vi my_script_file02.sh
To paste the content from the clipboard to our new file, in normal mode enter.
+p
Duplicate a Line
To duplicate a line press the following keys in normal mode. First yy to yank the line (copy to clipboard) and then p to paste it into the next line (insert).
yy p
Delete a Line
To delete the line the cursor is positioned, in normal mode, just press dd.
Delete all Lines
To delete all lines in your file, in normal mode, just enter %d.
% means all the lines and d for delete.
Search for a Character String in Vi
To find a character string in vi, in normal mode type / followed by the string you want to search for in the file. Type n to go the next occurrence of the string. Type N to go to the previous occurrence.
Below for example I will search for the string HISTTIMEFORMAT within the /etc/bash.bashrc file.
To search forward type /HISTTIMEFORMAT To search backwards type (in this case n and N are reversed) ?HISTTIMEFORMAT
Change Color Scheme
Vi resp. nowadays mostly the enhanced version Vim has many color schemes built-in which you will find under the following path.
/usr/share/vim/<vimVersion>/colors
where <vimVersion> depends on your OS and in my case for SLES 15 SP5 this is vim90.
To change the color scheme, in normal mode enter.
:color <color scheme> :color desert
This will immediately change the color scheme. Below you can see that the comments now instead of dark blue will shown up in cyan.
This will only change the color scheme for the actual session and not persistent. In order to persistently change the color scheme you can use the following command. This will create a vim configuration file in the users home directory. You can add here of course more customization for vim.
# echo colorscheme desert >> ~/.vimrc
If you haven’t created a vimrc file, the defaults.vim file that comes with Vim installation will be used.
You can use the whereis <command> command to find all paths for vim to search for the defaults.vim.
Links
An introduction to the vi editor
https://www.redhat.com/sysadmin/introduction-vi-editorHow To Select All In Vim/Vi Editor? [VI/VIM select all]
https://monovm.com/blog/how-to-select-all-in-vi-editor/VIM Cheat Sheet PDF for quick reference [Updated]
https://monovm.com/blog/vim-cheat-sheet/How to add additional color schemes in “VIM” Editor?
https://access.redhat.com/solutions/2298501