103.8. Perform basic file editing operations using vi
Weight: 3
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:
vi
/, ?
h,j,k,l
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).
vi
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!
vi or vim ?
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 ):
Inorder to read, create or modify a file with vi, give the file name to it:
if you just type vi(m) and hit enter, the vi(m) version will be displayed:
Okey lets start learning vi(m):
vi modes
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.
Moving the cursor
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.
Jumping
Editing text
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
searching
replacing
Exiting
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.
reloading
VIM Tip : running external commands in a shell
It usuaslly happens that we need to run a command in shell to see the result of file which we are edditing. It could be happened in two ways:
1.We could suspend your session (ctrl-Z), and then run the command in your shell.That’s a lot of keystrokes, though !
2.So, instead, you could use vim’s built-in “run a shell command”:
:!cmd Run a shell command, shows you the output and prompts you before returning to your current buffer.
help
Need help? you can use :help
or :help keyword
and vim will open help for keyword.
.
.
.
https://developer.ibm.com/tutorials/l-lpic1-103-8/
https://www.tecmint.com/learn-vi-and-vim-editor-tips-and-tricks-in-linux/
https://www.geeksforgeeks.org/vi-editor-unix/
https://www.linux.com/tutorials/vim-101-beginners-guide-vim/
https://www.cs.colostate.edu/helpdocs/vi.html
https://www.endpoint.com/blog/2009/03/10/vim-tip-of-day-running-external
.
Last updated