Table of contents
On this page... (hide)
- 1. Access your shell from inside VI
- 2. Selecting a block of text in Visual mode
- 3. Change the title of a window
- 4. Increment a number
- 5. Indent a block
- 6. Tabs in VI
- 7. Repeating a command with the period key
- 8. Open multiple files
- 9. Copy and Paste
- 10. Enable syntax highlighting
- 11. Showing matching matching brace or parentheses after entering a closing one
- 12. Read output of command into your document
- 13. Encrypt or password protect a document
- 14. Auto indention for coding
- 15. Return cursor to where you left off when editing a file
- 16. Getting the character code of any character
- 17. Moving around help
- 18. Swap lines
- 19. Redo and Undo
- 20. Indenting blocks of code or whatever
- 21. Add real line number to a file
- 22. Always display the status line
- 23. Highlight all matches of a search
- 24. Word completion like tab completion
- 25. Find open and closing parentheses, curly braces or brackets.
- 26. Search for the word under your cursor
- 27. Prevent extra spaces when pasting
- 28. Show line numbers
- 29.
.exrcfile options- 29.1 kill lines
- 29.2 saving
- 30. Heading
- 31. Page Comments (Click to edit)
1. Access your shell from inside VI
Type the following command,
:shell
and hit enter. Type exit at the shell to return to VI.
2. Selecting a block of text in Visual mode
Put your cursor at the beginning of your text and type a v. Now navigate to the end of the block you want to select. (You should see the text highlighted in vim.)
If you want to copy that text for pasting, type a y. To delete or cut the text, type a d. To paste the text your copied or cut, type a p.
3. Change the title of a window
ctrl+a A
4. Increment a number
Increment a number (integer) by placing your cursor on the number (last digit) and hitting ctrl + a.
5. Indent a block
Indent a block by placing your cursor on the top row, then hit shift + v, then type j to move your cursor to the last row. Now type a right angle ( > ). Indent further by hitting a period ( . ).
6. Tabs in VI
Open all files with the -b switch, and in Vi enter colon ( : ), the letter b and the number the file was in the list as in the example below.
Below is a bash script that should be named vi.project or whatever - just replace the first file in the list with that name. That way the first tab always shows you the number of the other files for reference.
#!/bin/bash # 1 2 3 4 5 6 7 8 9 10 vi -b vi.project index.php ajax.php css/default.css lib/config.php lib/functions.php lib/javascript.php lib/age.obj.php lib/php-library.php tmpl/index.tmpl
7. Repeating a command with the period key
Hitting period causes an action to take place again, kind of like an ampersand will perform a regular expression search and replace.
One example using the period to repeat an action is if you highlight a code block by positioning yourself on the first line of the block, then hitting shit + v and then pressing j until you get to the last line. This should highlight the lines. Then, anywhere in the highlighted block hit an angle, left or right, depending on which way you want to indent. This will indent the entire code block. Now hit the period any number of times, and it will repeat the indent as many times as you want. Hit u to undo if you go too far.
8. Open multiple files
Open your files with vi -p file1 file2 file3. When you're inside vi, issue the command :bn to go to the next file, and :bp to go to the previous.
9. Copy and Paste
The command Y or yy copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:
Y 2Y 10Y yG
To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:
P p
It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:
yw y$
The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.
Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.
http://www.tech-recipes.com/rx/219/copy-and-paste-text-with-vi-or-vim/ »
10. Enable syntax highlighting
:syntax enable
11. Showing matching matching brace or parentheses after entering a closing one
set showmatch
12. Read output of command into your document
While editing a document, say you want to put the date somewhere. In Linux, you can issue the date command from the command line to see the date. From within VI, you can also issue the date, but have it input wherever your cursor is by issuing r!date.
Another example, would be to put a directory listing in a document. You can do that similarly by issuing r!ls -l.
Basically, enter r! followed by a command.
13. Encrypt or password protect a document
While editing a document, just issue :X and you will then be prompted to enter a key (password).
After saving your document, when you open it again, you will have to enter the password.
To remove the encryption, open your document with the correct password and issue :set key= and that will remove it.
14. Auto indention for coding
Issue set autoindent
Also see, http://www.vim.org/tips/tip.php?tip_id=83 »
15. Return cursor to where you left off when editing a file
Just add the following two lines to your ~/.vimrc file,
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
16. Getting the character code of any character
Just put your cursor on a character and type ga and it will display the dec, hex and octal values for that character.
17. Moving around help
| e | move to the end of a word |
| w | move forward to the beginning of a word |
| 3w | move forward three words |
| b | move backward to the beginning of a word |
| 3b | move backward three words |
| $ | move to the end of the line |
| <End> | same as $ |
| 0 | move to the beginning of the line |
| <Home> | same as 0 |
| ) | jump forward one sentence |
| ( | jump backward one sentence |
| } | jump forward one paragraph |
| { | jump backward one paragraph |
| H | jump to the top of the display |
| M | jump to the middle of the display |
| L | jump to the bottom of the display |
| 'm | jump to the beginning of the line of mark m |
| `m | jump to the location of mark m |
| G | jump to end of file |
| 1G | jump to beginning of file |
| 50G | jump to line 50 |
| '' | return to the line where the cursor was before the latest jump |
| `` | return to the cursor position before the latest jump (undo the jump). |
| % | jump to corresponding item, e.g. from an open brace to its matching closing brace |
18. Swap lines
Just put yourself on the first of two lines, and type ddp and the two lines will be swapped.
If you type xp, it will swap the character under the cursor and the next character.
19. Redo and Undo
| u | undo last change (can be repeated to undo preceding commands) |
| U | return the line to its original state (undo all changes in current line) |
| CTRL-R | Redo changes which were undone (undo the undo's). |
20. Indenting blocks of code or whatever
You can do this a couple different ways.
One is, put your cursor on the first line of the block. However many lines are in your block, type that number, followed by >>.
The other way, that's a little easier is to put your cursor on the first line, then hit shift+v. Now move your cursor downward to the last line, and then text will be highlighted. Now, for however many tabs you need, type that number, followed by >> to tab to the right. If you need to tab back to the left, highlight your block again, type the number of tabs, and hit <<.
21. Add real line number to a file
Issue %!awk '{print NR,$0}'
22. Always display the status line
Issue set laststatus=2
23. Highlight all matches of a search
Issue set hlsearch
24. Word completion like tab completion
Just type ctrl+n after typing a few characters of a word.
25. Find open and closing parentheses, curly braces or brackets.
Just put your cursor on a line that contains a left parentheses, for example, and type %. It will highlight the open and closing characters.
26. Search for the word under your cursor
Just type * and it will search for the word under your cursor. Type # to search in reverse order.
27. Prevent extra spaces when pasting
Just do set paste and paste whatever, then set nopaste.
28. Show line numbers
Enter the following command,
set number
Just hit escape to make sure you're in the right mode, then hit colon ":", then type set number and hit enter.
29. .exrc file options
Put these commands inside your .exrc file in your home directory so that you don't have to issue commands everytime you fire up vi. Note: if you aren't putting these commands in your .exrc file, you must hit the : colon key before you type them.
#> turns tabs into 2 spaces (this is only preserved inside VI) set tabstop=2 #> show what mode you're in. (INPUT mode or not) set showmode
http://www.devdaily.com/unix/edu/un010003/ » http://www.jwz.org/doc/tabs-vs-spaces.html »
29.1 kill lines
dd kills the current line
To kill from postion a to b in a document, postition your self at b and type
ma
then position yourself at a and type
d'a
ma sets a marker a at the current position. You can set markers from a-z.
'a means go to that marker.
y'a would copy/yank the text into the kill buffer.
The equivalents would be,
- to copy/paste, set your marker, say
a, then go to the starting position, and type
y'a
then, go to the line you want to copy the text to and type
p
to add the text after the current line, or
P
to add the text before the current line.
- to cut/paste, just substitute
y'awithd'a
29.2 saving
To save edits to a different filename, do
:w newfilename
If you do
:wq newfilename
the original file will not be changed. You edits will be in newfilename.
