advanced_vim_tutorial.pdf

(69 KB) Pobierz
Advanced Vim Tutorial
Sunil Mohan Ranta
smr [at] students.iiit.ac.in
1
Introduction
Vim is not just an editor. It can be an IDE if used at its best. I have found
it more convenient to use than any other word processing tool i have used.
With proper knowledge of the countless features provided by vim, one can be
much more efficient in whatever editing job, and programming in my case.
Every time i wished for a feature, i found it there in vim. I didnt allow
myself to do anything the stupid way, and always figured out a better way
to do the same thing. Thats how i have got a decent expertise on vim. With
this tutorial i wish to give you a feel of what all is possible with vim, and
show you a direction to learn more.
Before i proceed, i would like to ask you a question. Suppose you have
got a file with about 50 lines only, and you have to make few changes to the
file. Lets assume that you are asked to add 10 to every number present in the
file. Lets assume that there are only 25 numbers in the file and rest is text.
Would you start doing it the stupid way or search for a efficient way to do
it ? Most of the times one will think that its just 25 numbers, and without
thinking about a better way to do it, starts editing it right away. Or even
if one does want to do it a better way he doesnt do it thinking that figuring
out the better way will take much more time than doing it the obvious way.
But what we always ignore is that its not just this time. We do it many
times, and thats why it makes sense to go for the better way to do it, even
if it takes more time at first time.
So next time when the thought “its just this much” comes to your mind,
just remember that “its not just this time” , and the choice is yours.
If we go by Brook’s words “The tool that save the most labor in a pro-
gramming project is probably a text-editing system” [The Mythical Man-
Month, proposition 12.10], then we should probably work on our editing
skills more than our programming skills.
In this tutorial I assume the reader to have a basic knowledge of vim.
Basic features like editing, movement, searching, replacing, opening, saving
1
etc are not covered in this tutorial. I’d recommend going through
vimtutor
for basic understanding of vim.
Remember :
“if its just this much, its not just this time”
2
Shortcuts
easily and efficiently execute more complex commands and statements.
2.1
Map
map is used to change the meaning of sequence of keystrokes, usually to
perform a job using easier keystrokes, which would otherwise need more
complex keystrokes. Sometimes simple but frequently used keystrokes are
also mapped.
o map can be used for
- changing meaning of typed keys
- execute a function on certain keystrokes
o mapping can be set for one or more of following modes using the com-
mands written against them.
normal
visual
insert
command-line
operator-pending
o two forms of map
-
map
-
noremap
to avoid recursion
o
unmap
remove the mapping
o examples
map
<C-S>
imap
<F3>
see
:help mapping
2
:w<CR>
<Esc><Right>n<Insert>
nmap
,
map
vmap
,
map
imap
,
map!
cmap
,
map!
omap, map
2.2
Abbreviations
change the meaning of a word. Can be used in insert , replace and command
line mode.
Abbreviations can be used for
- typos correction
iab
tihs this
- making abbreviations
iab
smr@ smr [at] smr.co.in
iab
#i #include
see
:help iab
3
Windows, Buffers and Tabs
Buffer is the vim’s copy of the file you are editing. Window is a viewport
onto a buffer. Tab holds windows.
3.1
Window Management
:split
or C-W s
split the active buffer horizontally
:vsplit
or C-W v
split the active buffer vertically
:new
or C-W n / :vnew
open a new buffer (empty)
other commands
all other commands work as they use to for single window
therefore
:w
for saving and
:q
for quitting
-o argument to vim
start vim with multiple windows each opening the files supplied
to -o
Navigation through windows
C-W w / C-W W / C-W C-W
- iterate through the windows (go to next/previous window)
see
:help windows
3
3.2
Buffer Management
:e
<file>
open new buffer (of the
<file>
supplied)
:ls
show current buffers
:b
<num>
switch to buffer
<num>
:bdelete
unload buffer
:bwipeout
unload buffer and deletes it
see
:help buffers
3.3
Tabs Management
Tabs are available only in version 7 or later. Tabs provide a traditional(like
in other GUIs) tabbed view over the vim windows and buffers. If mouse is
enabled in vim, tabs can be opened, closed or selected using mouse clicks.
Tabs shares registers, and therfor allow deleted/yanked text from one tab to
be pasted on others. Also undo history of every tab is maintained separately,
which allows switching between the tabs without loosing the changes history.
This is not possible in buffers. Also buffers does not allow switching between
buffers without saving the current buffer, which is now possible between tabs.
:tabe
<file>
Open
<file>
in new tab. Opens empty tab if no file is supplied
tabs
List opened tabs along with windows/buffers they contain
tabc
Close the active tab
tabn
and
tabp
Go to next(on tabn) or pervious(on tabp) tab
<Ctrl-PageDown>
and
<Ctrl-PageUp>
also works
CTRL-W gf
Open the filename under cursor in new tab
see
:help tabpage
4
4
Visual Mode
Visual Mode provides us flexible and easy way to select text and execute
command (operator) on it.
see
:help Visual
4.1
Visual Block
Rectangular block of selected text
invocation key : CTRL-V
4.2
Visual Line
Text is selected line-wise
invocation key : V (ie Shift-v)
4.3
Visual (Char)
Text is selected char-wise
invocation key : v
4.4
Role of Mouse in visual mode
setting mouse to ’n’ or ’a’ allows selecting visual area using mouse
click and mouse drags
4.5
Select Mode
select mode is similar to selection mode in Microsoft Windows Notepad. Typ-
ing any printable key replaces the selected text with the key.
Invocation keys : gh / gV / gH / G CTRL-H
see
:help Select
4.6
Important keys
gv
CTRL-G
<
>
select previous visual area again
switch between Visual and Select
shift selected text toward left
shift selected text toward right
5
Zgłoś jeśli naruszono regulamin