An Introduction to Vim for Sysadmins
Distro Quirks
The ancestral vi is long gone, replaced eons ago by Vim -- vi IMproved. Vim includes extensive documentation, unless your distro installs onlyvim-tiny
, which strips out the documentation
and other fripperies, which is another reason to know the basics without
having to look them up.Most distros symlink vi to Vim, so you should be able to start it with either
vi
or vim
. However, on some distros, notably Ubuntu, vi
starts Vim in vi-compatible mode, and it will behave like the ancestral vi. It will even tell you on the home screen:Running in Vi compatible mode type :set nocp for Vim defaults
The biggest hassle with vi-compatible mode is you can't use the arrow, home, end, page up, or page down keys for navigating your document without entering command mode. We'll get to Vim's modes in a moment; the short story is Vim is more comfortable to use than vi. There are two ways to return to normal Vim mode. One is to do what the home screen tells you: press the Escape key and type
:set nocp
, then press Enter.To change this permanently create
~/.vimrc
and enter this line:set nocp
A third way is to start Vim with
vim
instead of vi
. On Ubuntu use vim.tiny
.Remembering Vim Commands
Vim's commands are mnemonic, so it's not that farfetched that you will remember them.d
= delete, y
= yank or cut, p
= paste, w
= write, q
= quit. Your hands never leave the keyboard, so you are fast and efficient.Starting Vim
The biggest hurdle for new Vim users is its dual-mode system. It has a command mode for entering commands, and an input mode for typing your text. Vim starts up in command mode. Let's start with basic usage.vi
opens a new empty documentvi [newfilename]
opens and names a new documentvi [filename]
opens an existing document- Press the i or Insert key to enter input mode
- Press the Escape key to leave insert mode and enter command mode
Compact Keyboards
If you find yourself stuck with a keyboard that does not have Home, End, or arrow keys, enter command mode:j
moves the cursor down one linei
moves the cursor up one lineG
goes to the end of the documentgg
goes to the beginning of the documenth
moves the cursor to the left, one character at a timel
moves the cursor to the right, one character at a time
Saving Changes and Exiting
Now we come to the fun part: saving your changes and closing Vim.Save your changes as you go by pressing the Escape key to enter command mode, and then type
:w
Enter. You will see a confirmation that says something like "newfilename" 7 lines, 40 characters written".:sav [filename]
names a new document, or saves the file under a new nameTo quit and save your changes, enter command mode and type
:x
Enter or :wq
Enter.:q
Enter quits if you have already saved your changes.If you try to quit with unsaved changes, Vim won't let you. Make it obey with
:q!
Enter. (Or save your changes.)Common Editing Functions
Some common command mode editing functions:u
toggles undo/redo.r
replaces the character under the cursorx
deletes a single characterdw
deletes a single word, starting from the cursorD
deletes to the end of the line, starting from the cursordd
cuts one lineNdd
cuts N lines; e.g. 3dd deletes three lines starting with the current lineyy
copies one linep
pastes whatever has been cut or copied:set number
displays line numbers:set nonumber
turns off line numbering
Advanced Vim
This should be adequate for simple editing tasks like configuration files. To learn advanced Vim functions type:help
Enter to see all the built-in documentation. I recommend starting with
the tutor, which takes 30-60 minutes to complete. You can launch the
tutor outside of Vim by entering the vimtutor
command in your shell.
Can I publish your publish to my blog? I’ll add a one-way link to your forum. That’s one actually candy post. layanan pembuatan tautan usa
ReplyDelete