103.8. Perform basic file editing operations using vi
Description: Candidates should be able to edit text files using vi. This objective includes vi navigation, basic vi modes, inserting, editing, deleting, copying and finding text.
Key Knowledge Areas:
- Navigate a document using vi
- Use basic vi modes
- Insert, edit, delete, copy and find text
Terms and Utilities:
- /, ?
- i, o, a
- c, d, p, y, dd, yy
- ZZ, :w!, :q!, :e!
The need to learn how to use text editors in Linux is indisputable. Every system administrator and engineer deal with configuration (plain text) files on a daily basis, and most times this is done purely using one or more tools from a command-line interface (such as nano, vi, or emacs).
The vi editor (visual editor) is almost certainly on every Linux and UNIX system. In fact, if a system has just one editor, it’s probably vi, so it’s worth knowing your way around in vi.
Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file.
vi editor is great even trough ssh sessions!
Most Linux distributions now ship with the vim (for Vi IMproved) editor rather than classic vi. Vim is upward compatible with vi and has a graphical mode available (gvim) as well as the standard vi text mode interface. The
vicommand is usually an alias or symbolic link to the vim program.
There are several versions of vim: tiny, small, normal, big, and huge. We can find out what version of vim we are running and what features are included by using the command(ubuntu 16.04 here ):
[email protected]:~# vi --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)
Included patches: 1-1689
Extra patches: 8.0.0056
Modified by [email protected]
Compiled by [email protected]
Huge version without GUI. Features included (+) or not (-):
+acl +farsi +mouse_netterm +tag_binary
+arabic +file_in_path +mouse_sgr +tag_old_static
+autocmd +find_in_path -mouse_sysmouse -tag_any_white
-balloon_eval +float +mouse_urxvt -tcl
-browse +folding +mouse_xterm +terminfo
++builtin_terms -footer +multi_byte +termresponse
+byte_offset +fork() +multi_lang +textobjects
+channel +gettext -mzscheme +timers
Inorder to read, create or modify a file with vi, give the file name to it:
Okey lets start learning vi(m):
Vim actually has three modes: insert mode, command mode, and escape (last-line) mode. Let’s start with the default mode you’ll see when you start up Vim–command mode.
- command mode : When you run vim filename to edit a file, Vim starts out in command mode. This means that all the alphanumeric keys are bound to commands, rather than inserting those characters.(save, quit, search/replace, navigate around, execute macros,...)
- insert mode : To enter the insert mode, type i (for “insert”) and now the keys will behave as you’d expect. You can type normally until you want to make a correction, save the file, or perform another operation that’s reserved for command mode or escape (last-line) mode. To get out of insert mode, hit the Escape key.
- escape (last-line) mode : Once you press Escape, you’re in command mode again. What if you’d like to save your file or search through your document? No problem, press : and Vim will switch to escape (last-line) mode. Vim is now waiting for you to enter a command like :w to write the file or :q to exit the editor.
If that all sounds complicated, it’s really not. It does take a few days to start training your brain to move between the modes and memorizing the most important keys for movement, commands, and so on.
The first thing you’ll want to learn is how to move around a file. When you’re in command mode, you’ll want to remember the following keys and what they do:
If you type a number before any of these commands, then the command will be executed that many times. This number is called a repetition count or simply count. For example, 5h will move left five characters. You can use repetition counts with many vi commands.
Now that you can open a file in vi, move around it and get out, it’s time to learn how to edit the text in the file
If you’re in insert mode, hit Escape. Then enter : and you’ll see a line at the bottom of the screen with a cursor ready to take input.
Press ESC to return back to the normal command mode!
When you want to write and exit from a file consider you permissions, the bellow command write out the current file using sudo::w !sudo tee %
- :w – Write a file (actually buffer).
- !sudo – Call shell with sudo command.
- tee – The output of write (vim :w) command redirected using tee.
- % – The % is nothing but current file name.
Need help? you can use
:help keywordand vim will open help for keyword.