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
vi command 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 --versionVIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)Included patches: 1-1689Extra patches: 8.0.0056Modified 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:
Move left one character on the current line
Move down to the next line
Move up to the previous line
Move right one character on the current line
Move to the next word on the current line
Move to the next end of word on the current line
Move to the previous beginning of the word on the current line
Scroll forward one page
Scroll backward one page
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.
move to top of screen
move to middle of screen
move to bottom of screen
jump forwards to the start of a word (words can contain punctuation)
jump forwards to the end of a word (words can contain punctuation)
jump backwards to the start of a word (words can contain punctuation)
jump backwards to the start of line
jump to the first non-blank character of the line
jump to the end of the line
jump to the last non-blank character of the line
go to the first line of the document
go to the last line of the document
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
key( for inserting)
insert text before cursor, until hit
insert text at beginning of current line, until hit
append text after cursor, until hit
append text to end of current line, until hit
open and put text in a new line below current line, until hit
open and put text in a new line above current line, until hit
replace single character under cursor (no needed)
replace characters, starting with current cursor position, until hit
change the current word with new text, starting with the character under cursor, until hit
change N words beginning with character under cursor, until hit; e.g., c5w changes 5 words
change (replace) the characters in the current line, until hit
change (replace) the entire current line, stopping when is hit
Ncc or cNc
change (replace) the next N lines, starting with the current line, stopping when is hit
key (for deleting)
delete single character under cursor
delete N characters, starting with character under cursor
delete the single word beginning with character under cursor
delete N words beginning with character under cursor; e.g., d5w deletes 5 words
delete start of line till the cursor
d$ and D
delete the remainder of the line, starting with current cursor position
delete entire current line
Ndd or dNd
delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines
key(for cutting and pasting)
copy (yank, cut) the current line into the buffer
Nyy or yNy
copy (yank, cut) the next N lines, including the current line, into the buffer
put (paste) the line(s) in the buffer into the text after the current line
put (paste) the line(s) in the buffer into the text before the current line
search forward for occurrence of string in text
search backward for occurrence of string in text
move to next occurrence of search string
move to next occurrence of search string in opposite direction
replace first old with new in just that line
replace all old with new in just that line
replace all old with new throughout file
replace all old with new throughout file with confirmations
same as above but case insensitive
remove highlighting of search matches
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.
That will write the file to the existing filename,but don't exit.
If you don’t have a filename or want to write out to a different file name
quit (fails if there are unsaved changes)
Quit editing without saving.
write (save) and quit (throw and error if file is not writable)
try to write and quit (if it is not writable, quit without saving! )
ZZ and :x
Exit and save the file if modified
q! or ZQ
quit and throw away unsaved changes
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.
it will discard local changes and reload.
Need help? you can use
:help keyword and vim will open help for keyword.