documentation:vim
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
documentation:vim [2010/12/17 19:52] – created mtaft4 | documentation:vim [2011/02/05 19:26] (current) – mtaft4 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Vi IMproved==== | ||
+ | Vim is a moded text editor modeled after VI that offers the user with a much friendlier environment to type in. It adds support for the directional keys in editing so the user doesn' | ||
+ | ===Starting Off=== | ||
+ | First off, when you first start up vim you will be in the command mode. This means that most of your normal keystrokes won't put text to the screen but will rather perform a predetermined command. For instance typing the h key when in command mode will actually move the cursor to the left (assuming of course there is space in that direction for the cursor to move). So before you can get to any actual editing you need to be in the insert mode. Now there are several key combinations that will do this. | ||
+ | i,I - Insert text at the cursor, insert text at the beginning of the current line. | ||
+ | a,A - Append text after the cursor, append text to the end of the current line. | ||
+ | o,O - Insert a new line below the cursor and insert text, insert a new line above the cursor and insert text. | ||
+ | s,S - Delete the character at the cursor and insert text, clear the current line and insert text. | ||
+ | Now, all these options might seem daunting but with time being able to use this variety of options to begin editing your file will prove to be a boon. Once in insert mode Vim behaves just like any other text editor that you've known. It's strengths come from the things that it is able to do beyond simply editing text. | ||
+ | When you're ready to save your file and quit simply go to command mode, type : to get the extended commands then type wq to save and quit. Remember that you can append an ! to a command to force its execution. This doesn' | ||
+ | ===Search and Replace=== | ||
+ | Now let's assume you've written this lovely little bit of source code but woe is you! Upon trying to compile the code you realize that you have misspelled one of your variables and it's causing compiler errors. What do you do? Well, you //could// simply reopen your code then scroll through it looking for the offending variable in every instance of it and fix it. But there' | ||
+ | Now that's quite a bit of information in one dose. Basically what it means is that if you want to replace a certain pattern within your file all you have to do is use the s option. How you use it is to specify an interval of the file, usually either the whole file, or a group of lines. Then the letter s followed by / and the pattern you're searching for. If anyone reading this is unfamiliar with regex' | ||
+ | |||
+ | In the most basic sense its use corresponds to the usage of the sed command in bash. | ||
+ | ===The Configuration File=== | ||
+ | Vim uses a configuration file on the home directory of the user running it called .vimrc. This file states the options that should be set by default whenever vim is started. There are many options that can be specified here. As with most things all of the things that can be set here can be done from within vim via the :set command. | ||
+ | The following table shows some of the more commonly used commands to be put in the .vimrc file. If no entry is given for the Modes portion then that particular command is on by issuing :set < | ||
+ | ^ Command ^ Modes ^ Description ^ | ||
+ | | bg | bg=light, bg=dark | Sets the colors of syntax highlighting for either dark or light color schemes (For better viewing based on terminal colors) | | ||
+ | | syn | syn=on, syn=off | Sets syntax highlighting based on the type of file being edited. (C, C++, HTML, PHP, Perl, etc) | | ||
+ | | tabstop | tabstop=# | Sets the number of characters a striking of the TAB key stops at (Ex: A tabstop of 4 means a tab occupies 4 characters of space) | | ||
+ | | shiftwidth | shiftwidth=# | ||
+ | | textwidth | textwidth=# | Automatically creates a new line after # characters. Useful if the file you change things within long lines as it breaks lines up into manageable portions. | | ||
+ | | ignorecase | Ignores case when searching || | ||
+ | | incsearch | Automatically jump to first instance of searched term as it's typed (search still must be finished with Enter) || | ||
+ | | hlsearch | Highlight search terms || | ||
+ | | smartindent | Automatically indents blocks of code (A block is denoted by { and } in C/C++, also after certain words in bash scripts like for, while, if, etc) || | ||
+ | | autoindent | Preserves indents from previous lines (Useful when editing a block of code.) || | ||
+ | |||
+ | Now, Vim can also be set up to have per file type configurations as well. By putting an entry with autocmd for the file type in question and specifying a .vim file in the ~/.vim directory it will use a secondary configuration along with the contents of the .vimrc file if the file being edited matches the file type in question. | ||
+ | |||
+ | For example. If you wanted to have a separate configuration for C source code, then in your .vimrc file you would type | ||
+ | autocmd FileType c source ~/ | ||
+ | where ~/ | ||
+ | |||
+ | Vim can also check for filetype not only by extension but by the content of the file as well. For example, say you want to have a set of vim settings for when you are editing bash scripts but said scripts don't always have the .sh extension for whatever reason. Well here's what you do: Create a new file in your ~/.vim directory called scripts.vim, | ||
+ | < | ||
+ | finish | ||
+ | endif | ||
+ | if getline(1) =~ ' | ||
+ | setfiletype sh | ||
+ | endif</ | ||
+ | Now save the file. Basically what this says is first off if a filetype has already been set either from extension detection or the other rules in the $VIMRUNTIME/ | ||
+ | ===Commands=== | ||
+ | This section will go over the commands that can be used within Vim. Vim's command mode is one of its most powerful assets as a text editor, being able to quickly and easily perform a variety of operations. Almost all of these commands can have a number put before them to have that command execute a certain number of times. | ||
+ | ^ Command ^ Options ^ Function ^ | ||
+ | | d | w, d, %%^%%, $ | Deletes the current word, line, to the beginning of the line, to the end of the line. | | ||
+ | | c | w, %%^%%, $ | Delete the current word, or to the beginning or end of the line and go to insert mode. | | ||
+ | | r | any character | Replace a single character with the next character typed. | | ||
+ | | u | Undo the last change made. || | ||
+ | | x | Delete the character under the cursor || | ||
+ | | y | w, y | Yank (copy) the current word or line to the buffer. | | ||
+ | | p, P | Paste the contents of the buffer after the cursor, paste the contents of the buffer before the cursor. || | ||
+ | | ~ | Change the case of the character under the cursor. || | ||
+ | | >> | Shift the current line right (indent) || | ||
+ | | << | Shift the current line left (de-indent) || |