| Intro to Vi ( Vim ) |
|
|
| Saturday, 29 April 2006 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This is bascially a
primer for vi. Even if you're not new to vi, you will probally find
something. It covers the basics needed to edit files on your system
using one of the most widely available editors on UNIX/Linux systems.
It a very complex beast, but with a basic understanding and memorizing
a few commands it becomes a valuable tool.
Back when I first started with vi I wish I had something like this. After using vi for as long as I have, I'd be lost without it. It's definately worth learning if you are editing a lot of text or code. It'll be in plain english. Written in a manner where it would be considered a tutorial and then notes to go back and use as a reference. If I mention something else along with an item i'll also try to explain it quickly. I'm not a great writer. My punctuation and spelling are not the best, but I've been told I can explain stuff easily and get the point across. You'll just have to Suffer :) So lets get started!
Ver 0.1.1 - Last updated: 10/05/2000 Using VI
DESCRIPTION
"Vim is a text editor that is upwards compatible to Vi. It can be used to edit any ASCII text. It is especially useful for editing programs. There are a lot of enhancements above Vi: multi level undo, multi windows and buffers, syntax highlighting, command line editing, filename completion, on-line help, visual selection, etc.. See ":help vi_diff.txt" for a summary of the differences between Vim and Vi. If you install the packages above you will have 'vi' and
'vim' installed on your system. What I like to do is create an alias for vi so I can just
type 'vi' instead of 'vim'. This keeps the 'vi' in /bin for emergencys
and makes it easier for me to start up vim instead So...any examples in this section where I use vi it's actaully vim One of the great things about learning vi is it's
widespread use on UNIX systems. Every system i've every run across has
at least vi on it. It's also available for Windows based systems.
To open an existing file or create a new file if it doesn't exisit:
To open an existing file at a certain line:
Open inetd.conf at line 35: To open an existing file at first occurance of a string:
Open inetd.conf to first occurance of "ftp": > vi +/ftp inetd.conf
"Hey, I started vi one time and I couldn't even quit the program! It's a nightmare." Learning the basics of vi can be challenging. Once you understand the difference between command mode (ex) and the editing mode things get a lot easier. Let's start a new file and try a few things: "testing-vi" [New File] 0,0-1 All Of coarse we already know the file name. But the status line (also command line) comes in handy to see what mode your in or file information when we save and don't exit. It'll give the filename and what was written to the file. The 0,0-1 is the [row],[col] were at in the file. -1 means the file is empty Before we start let me say that the Caps Lock will torture you in this program. Make sure it's off. Also if your commands aren't doing what you expect, look to see that you didn't hit it by accident Let's "insert" some text. Hit 'i' for insert mode and the
status bar in the bottom will have "--INSERT--". This will start
insterting text where the cursor is located. Type: It'll stay in insert mode until you hit the <esc> key, so do that now. You'll notice that the cursor will stay at the point we were last editing at. There are a few ways to move the cursor around. Use the 'Home' and 'End' keys to move to the begining and end of the lines. You can also use shift-4 and shift-6. I use the shift method because not all terms map the Home and End keys correctly. You don't have to use that method, but it's nice to know when they aren't mapped correctly. The arrow keys will move the cursor around one character/line at a time. Now let's change the line to read "This is test sentence
number 1" <esc> will always take you out of edit mode. When in doubt, smack <esc> You might be thinking "Hey, I could do this a lot easier in _____ editor with all this hitting esc and stuff". For the basics, maybe, but when you start learning a lot of the shortcuts in vi you won't be thinking that anymore. Try out the 'w', 'b' and 'e' keys. You should be able to easily jump around the line. You'll notice all of these commands are easy to remember. Along with 'i' to go into insert mode there is 'a' to go into append mode. 'a' will start inserting text AFTER the cursor while 'i' inserts it at the cursor. Lets copy (yank) the first line. You can be anywhere on
the line that you wish to copy. Now go to the 1 on the end of the second line Now we know 'w' is word. And we know 'y' is yank. Let's
combine these 2 together. We now have "This is a test sentence2This". Doesn't make
any sense. Lets delete the word 'This' that we just added Now f we did 'dw' and we were at the start of the 'T' in 'This' would have deleted the entire word in one shot. Hitting 'dd' anywhere on the line would have deleted the entire line. Before using any movement commands, make sure your not in
any of the insert modes by hitting<esc> a few times. You can also tell vi to do the command multiple times by
prefixing the command with a number. Another helpful one is 'shift-J' which takes the line
below the current line and appends it to the current line. vi has an undo history. "What's the : all about?" Visual Mode Hit 'v' and then move around with the arrow keys to
highlight your selection. Experiment with it awhile Cutting and pasting between terms ( terms in X ) -This is very easily done. Make sure the vi in the term
your going to paste into is in insert mode and the cursor is where you
want the text to go That's not just a vi thing, but works anywhere if you didn't already figure it out
Here are the important ones: :q Quit file :q! Quit file ( discarding any changes ) :w Write. By default it saves the file as what it was named when vi was started. :w! Write file ( overiding protections - example: write a file that's read only ) :30,50wfilename write lines 30 thru 50 to filename :30,50w>>filename write lines 30 thru 50 and append it to filenameSo 'w' is write and 'q' is quit. So :wq is write and quit and is the same as ':x' You can also write what you are editing to another file
easily by doing
Searching '/' puts vi into search mode So '/test' will search for the word 'test' stopping at
the first match. Searching backwards: Replacing There are simple string replacements or you can use regular expressions. We'll stick to the simple stuff for now If your familiar with 'sed' this will look familiar to you. If not, well your getting a basic lesson in using 'sed' too :) Replace the first occurance of 'old' with 'new' on the
current line Now if we add a 'g' on the end for 'global' we replace
the all occurances of 'old' with 'new' on the current line Now if we add a '%' to the beginning of the line it will
replace every instance of 'old' with 'new' in the
entire file. You can specify the lines to replace between by listing
them first You'll find this will work with basic words. Once you try searching for [ ] > < \ / and others your going to run into problems. That's because they are part of the syntax for 'regular expressions'. reg exp is a WHOLE other howto. I'll give you some basics here. I have a long way to go before I even think I know enough about reg exp to write a tut on it:) We'll be using the '\' character. You can use this to tell it that the next character is to be treated like a normal character, not a special one. Let's say we want to search and replace /home/jack in your entire file with /home/joe. If we look at the syntax for the search we see that the division between the old text and the new text is a /. So right off the bat we can see this is going to freak it out. We need to protect those characters in our search string and we'll use the \ to do it Wrong way: That basically says replace '' with 'home' and it'll probally crap out. Right Way: Notice the \ directly before each / in the strings
telling it "Hey, the next character isn't yours..it's mine" :)
This one of the things I love about vim. From /usr/doc/vim-commoni-version/doc/syntax.txt Syntax highlighting enables the possibility to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations. Lexical highlighting might be a better name, but everybody calls it syntax highlighting, so we'll stick with that. Vim supports syntax highlighting on all terminals. But since most ordinary terminals have very limited highlighting possibilities, it works best in the GUI version, gvim. The below example is a section of perl file with color
syntax highlighting.
All the colors are configurable in both the '.vimrc' and
'.gvimrc' files in your home directory. Mine is customized, but also has a description for each
type and a list of the colors. I also use Eterm which has much nicier
colors than the rxvt or Xterms do. So it might be hideous in those :)
set background=dark
" The next line used to work, then it made your cursor disappear in later redhat distos..called a feature
" set term=xterm-color
" Turn on the colors!
syntax on
" shut off that STUPID 'highlight your searches' crap in the latest RedHat release of vim
nohlsearch
" NR-16 NR-8 COLOR NAME ~
" *cterm-colors*
" 0 0 Black
" 1 4 DarkBlue
" 2 2 DarkGreen
" 3 6 DarkCyan
" 4 1 DarkRed
" 5 5 DarkMagenta
" 6 3 Brown
" 7 7 LightGray, LightGrey, Gray, Grey
" 8 0* DarkGray, DarkGrey
" 9 4* Blue, LightBlue
" 10 2* Green, LightGreen
" 11 6* Cyan, LightCyan
" 12 1* Red, LightRed
" 13 5* Magenta, LightMagenta
" 14 3* Yellow
" 15 7* White
" highlight Normal ctermbg=black ctermfg=darkgrey
" the mouse cursor
" highlight Cursor ctermbg=none ctermfg=white
" comment that start with #
highlight Comment ctermbg=none ctermfg=lightblue
" any constant
highlight Constant ctermbg=none ctermfg=red
" a string constant: "this is a string"
highlight String ctermbg=none ctermfg=white
" a charater constant: '\n', 'c'
highlight Character ctermbg=grey ctermfg=red
" a number constant: 234, 0xff
highlight Number ctermbg=none ctermfg=red
" a boolean constant: TRUE, false
" highlight Boolean ctermbg=none ctermfg=green
" a float constant: 2.3e10
highlight Float ctermbg=none ctermfg=green
"########
" variables without $ as in declaring
highlight Identifier ctermbg=none ctermfg=cyan
" function name ( also: methods for classes - spaces in sub" "balh" " {
highlight Function ctermbg=none ctermfg=green
"########
" Any statement quotes > for in read echo
highlight Statement ctermbg=none ctermfg=green
"if, then, else, endif, switch, etc
highlight Conditional ctermbg=none ctermfg=brown
"for, do, while, etc
highlight Repeat ctermbg=none ctermfg=brown
"case, default
highlight Label ctermbg=none ctermfg=green
" "sizeof","+","*", etc
highlight Operator ctermbg=none ctermfg=lightblue
" Any other keyword
highlight Keyword ctermbg=none ctermfg=red
"########
"next does `expr' type stuff..not much really
highlight Special ctermbg=none ctermfg=red
" int, long, char, etc.
" highlight Type ctermbg=none ctermfg=green
The gvimrc is very similar except the lines have 'guibg=' and 'guifg=' instead of the 'ctermbg' and 'ctermfg'
Here are some of the commands for vi.
Aditional information: man vim - manpage vimtutor - Vimtutor starts the Vim tutor. Another way to learn Vim /usr/share/vim/vim56/doc/*.txt - Vim docs :help - in vim For recent info read the VIM home page: www.vim.org Also, if you don't already have it, get Eterm. It's by
far the best term. This page coded in Vim :) This text written by Denis Hruza - Homepage Any comments, suggestions, corrections or additions should be sent to hruzaden@ exit18.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated ( Saturday, 29 April 2006 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < Prev | Next > |
|---|

