21 vim 2007-11-28 23:19:00 2009-06-06 23:53:00 tools text_editor programming vim programmers editor Some vim tips and a little vim reference. This document is here to duplicate the useful ':help' information. Just a nice quick shortcut reference. ///Table of Contents Goes Here/// # Introduction Some notes about `vi` & `vim`. Sure be read the `vimtutor` help. These notes are also part of these document and of the `:help` command in `vim` and some additional. But first of all, read the informations in the `vimtutor` if you want to be have a good quick start. Just run `vimtutor` in a shell if you have `vim` installed. It take a 20 minutes to finish that `vimtutor`. But it's really a must. If you want to make use of `vim`, you should need to read it. It also show you near the end, how you can copy the `.vimrc` configuration file to your home directory. So that you can enable the code highlighting when using `vim`. Somethings you really need. Like with many good stuff, default the config file suck a bit and there's a need to tweak it to fit it to us needs. But once tweaked, it will rock! Once you got the basic working of `vim`, after having read the `vimtutor` introduction, come back to this article. This article is more as a reference, quick shortcut reference... `vim` is a really good and a powerful text editor that you may help you to write things. `vim` is very useful for programmers or people that want to do more than using a simply and restricted text editor in the `cli`. You can use `vim` as an `ide`. A thing that i can suggest to new users of `vim`, is to use the `gvim`. So that you runs the graphical version of `vim`. In this case for basic things, like open a file, save a file, ... You can do it be clicking with the mouse and view the associated shortcuts for it in the menu. So you can quickly learn the commands and finally use the basic features of the text edit be pressing on items. Also use the help menu to get more information of how this application works. The tutorial is very useful. You should also need to tweak your `.vimrc` config file. Default it's been working right, but you should been need to tweak it to have all the nice features activated and working to your needs. A good tip, keep always a good working version of your config file. Once you have tweaked to your needs you will be really happy because some features are very useful to work with `vim`. Note that it take many time to tweak the `.vimrc` config file. There's a need to make many tests when you tweak your `.vimrc` config file. If you plan to tryout `vim`, you need to tryout the `vimtutor`. It is a little `vim` tutorial that will help you much to get started. Read it! Type `vimtutor` in your shell. # Modes While you use `vim`, you need to remember that there are 3 essential modes: * Normal mode * Insert mode * Visual mode Remember, to go in, normal mode, press escape, it's better to press several times escape when you not know in which mode you are. The normal mode is the default mode. From the normal mode, you can easy go to the other 2 modes. To go in `insert` mode press the `i` key or the `insert` key. Then you usually enter all your text like your used to do with all other text editors. To go in visual mode, press the 'v' key. Remember to first press esc twice. To go out of the previous mode. Once in visual mode; you can use the arrow keys to move and select some text. The visual mode is useful for selecting block, parts of text and perform somethings on it. Say re-formate the paragraph, copy and pasting some stuff, delete the selected text ... The most frustrating thing for the new user are these modes. Because each action is depending on the mode. You will see below too, there are some keyboard shortcuts noted. But these depend highly on the mode you are in. Read been the additional comments. Some needs to be are executed in visual mode and not in normal mode. When you see somethings like `:w foo`. This should be executed in normal mode. There are some more modes to. Need to write more about it. # Navigation keys In normal mode press these keys to navigate into the text. To go in navigation mode or be sure that you are in these mode press the `ESC` key. * k : up * j : down * h : left * l : right Sure, you can use the arrow keys of the keyboard to navigate. # Some keys need to know There are some keys you need to know. Like exiting a file without saving the modification. Probably you may mess up some things while experimenting. ## In normal mode :w write (save) :w FILENAME write to FILENAME :wall write all files :q quit :q! quit without saving :qall! quit all without saving :wq write & quit :wqall Write and quit all :e! Restore to last save state :E Open the build in explorer so you can browse and open whatever file :Explorer idem :e . idem ZZ Write file and quit ctrl-c To go out of inserte mode and go back to normal mode 2k move 2 lines up 4h move 4 characters to the left :verbose command show you all commands and which script defined them :command shows you command names :verbose command E Will list the source of all commands starting with E x Delete a character at the cursor position i Insert text (Go in insert mode) I Insert at the begin of the line. A Append text at the end of the line. a!!! Append the text '!!!' to the end of the line a Append text after the cursor possition. gl o Open a line below the current cursor position. O Open a line above the current cursor position. o This is append Append 'This is append' d delete d2 delete 2 chars. Can use any decimal number dw Delete word. daw Delete word. das Delete a sentence. dap Delete a paragraphe. d$ Delete from cursor position to the end of the line dd Delete whole line (Can also '2dd' to delete 2 lines). de Delete to the end of the current word, INCLUDING the last character. p put or the last deleted line yy Copy the whole line. 7yy Copy 7 lines. p Paste current buffer. J Joining 2 lines 16G Jump to line 16 works on any other integer # Moving e Move to the end of the word, next end of word if already on the end w Move to the next word 2w Move 2 words forwards 3e Move the cursor to the end of the third word forward. 0 (zero) Move to the begin of the line C-e Scrolling down with leaving the cursor in position C-y Scrolling up with leaving the cursor in position # Undo/redo u undo previous action U undo all the changes on a line CTRL+R redo (undo the undo) CTRL+r idem # Folding zi (N) Enable/disable folds zo Open one fold of N if provided zc (N) Close one fold of N if provided zO Open all folds. zC Close all folds zv View Cursor Line zMzX View Cursor Line only zm Close more folds zM Close all folds zr Open more folds zR Unfold all zd Delete Fold zD Delete All Folds :set fdm=marker Set type of fold marker :set foldmethod=indent For python :set foldenable :help :folding For help about folding. # Change the case ~ Toggle the case gU Upercase the selection gu Lowercase the selection # Spell :set spell Activate spell :set nospell Desactivate spell :set spl=en spell Set english spell checking ]s Next error [s Previous error z= Suggest Corrections :spellrepall Repeat correction zg To put word under cursor to the own word list zw To put the word in the bad list, if it was in the good word list zW Like zw, but put it in the internal word list # Identing text :> Indent to right. >> Indent to right. :< Indent to left. << Indent to left. # Window manipulating :split Horizontal split :vsplit Vertical split ctrl+w + Grow window ctrl+w - Shrink window ctrl+w _ Shink the window to the maximum ctrl+w w Jump to other window :close Close the current window # Patching :set patchmode=.orig # Not classified stuff :tabedit thatfile # Need to execute the commands in normal mode :make Make :cl List Errors :cl! List Messages :cn Next Error :cp Previous Error :cold Older List :cnew Newer List r Replace characters cw Change word c$ Change to the end of the line :CTRL-g Show location in the file and status :shift-G Go to the bottom of the file :x schift-G where x is a number to go :!COMMAND to execute an external command. ex. :!ls #,# w FILENAME To save a part of a file where the first # is the line number of the top and the second # is the bottom number. CTRL-g to view line number :r FILENAME To insert the content of a extern file. R To replace more than one word, go Replace Mode. ~ Change case of the letter if lower set upper and versa. :set To view the active parameters. :set all To view all the parameters. Verry usefull !!! :synatax on or off to enable/disable syntaxhighlighting. :help w To find help about w. :help c_ # In Visual mode d Delete selected text y Yank (copy) selected text ~ Reverse case of selected text gq Reformat the text to the textwith variable # Searching :/KEYWORD To search a keyword press 'n' to go next press 'shift-n' to go back % To find the next ),] or } usefull for debugging you must set the cursor on the first ex. (,[,{ :set ic To ignore case (no case sensitive while search) :set hls is hlsearch and incsearch (highlighting and incasesensitif) :nohlsearch Remove highlighting. # To highlight all words matching the word the cursor is on, press the # key (altgr+#) # Patern matching :s/old/new/ Change the first word 'old' by 'new' of the line. :s/old/new/g Change all words 'old' by 'new' on the line. :s/\./!/ Replace the dot with an exclamation mark. And example how to escape some special character. :s/^- //g Replace all lines starting with '- ' with '' (empty). :s/foo/bar/gc Replace all words foo by bar on the whole line and confirm :%s/word1/word2/gc Replace all words 'word1' with word2, and ask each time a confirmation (% for all lines, g for all occurences, and c for confirmation. :g/^#/d Delete all line that start with # :g/^$/d Delete all empty line # Setting the width of text vim: set ft=text80: # Change case of text gUw Change the word to uppercase g~w Switch the case of the word. guw Change the word to lowercase. gUw Change the word to uppercase ~ Change the case. # Copy somethings Y Copy the current line to the temp buffer. yy Same as 'Y'. ye Copy a word to the temporary buffer. yw Same as 'ye'. y$ Copy from cursor position to the end of the line. J Join the previous line to the current line. gJ Same as 'J' but without a space(vim). :j! Same as 'J'. :!command To run a command. ex ':!ls' :!! Repeat the lastest system command (usefull to repeat...) # Paste somethings p Paste the deleted or yanked text. gp Same as 'p' but leave the cursor position (vim) ]p Same as 'p' but match the current indention. (vim) [p P gP # Commenting a line # With commenting, i mean putting a "#" in begin of a line :m,ns/^/# Where m is the starting line and n the end # Or in visual block mode (ctrl+v), select some line and then: I# # Folding text An example, put The following in a new file, save and exit. Reopen it. ## Automatic folding # This should be folded {{{ This is some hidden stuff in the fold. #}}} # vim:foldmethod=marker expandtab And you should get somethings like this when you reopen the file: +-- 5 lines: This should be folded -------------------------------------- # vim:foldmethod=marker expandtab # Increasing numbers If you have some number, say `23`, set the cursor position on it and press twice the `esc` button, to be sure your out of any mode and be are in command mode. Then press `ctrl+a` and it will the number. (hold `ctrl` pressed and press `a`). # Command line ranges If you want to delete all the lines after `102` to the end of the file: :102,$d For more info see: :help cmdline-ranges or better: :he 10.3 # Other gl o Open a line below the current cursor position. O Open a line above the current cursor position. Commands that work in 'Insert Mode': Ctrl+W Delete previous word (vim). Ctrl-A Repeat the lastest insertion (vim). Ctrl-U Delete the current line. Ctrl-[ Terminate the 'Insert Mode' and go in 'Visual Mode'. The previous given commands are set by 'ssty', so it's possible that some command differ. Look at the settings of 'ssty'. cw cc Change the line. c$ Change the text form the current position to the end of a line. C Same as 'c$'. dd Delete the current line. d$ Delete the remainder og the line. D Same as 'd$' 4dd Delete 4 lines, replace the number with you're choice. dw Delete a whole word. d} Delete the current paragraph. d{ Delete the previous paragraph. d^ Delete all dL dG Delete all from the possition to the EOF. 2dw Delete the next 2 words # Nice plug-ins ## minibufferexplorer See ### Setup Copy the plug-in in your `~/.vim/plugin/` and restart `vim`. Open now some files and you see the buffer browser on top op the `vim`. ### Usage # There are 3 different keybindins for the same result # To change of buffer, then we can press tab or move # with up, down, left, right ctrl+W ctrl+w k ctrl+w w # Or with the mouse enabled, we can double click on a buffer ## pydiction Python auto completion feature Really good! You can easy add some more syntax highlighting. See ### Usage #Note: We use it in insert mode! ctrl+n complete next ctrl+p complete previous ## Taglist See . ### Setup Download the compressed file. Extract the compressed file. Copy `taglist.vim` to `~/.vim/plugin/` then copy doc to `~/.vim/doc/`. Then the first time we use taglist, `cd $HOME/.vim/doc`; and start vim. then `:helptags .`. ### Usage :TlistToggle Open/close the taglist :help taglist For more help ## tagexplorer See the website for more informations. List files in a new screen. ### Setup Download the stuff, place the file in `~/.vim/plugin/`. Restart vim. ### Usage :TagExplorer Open/close the tag explorer # In the TagExplorer o on a selected file show/hidden all functions/classes + on a selected file show all funtions/classes - To collapse c To collapse s Sort functions/classes u Update the list of tags g To select to jump to which directory ? To get all shortcute keys the options We can select a funtion/classe in the TagExplore when we have make use of the 'o' option and and show it on on that possition in the file. Very useful! When selecting a function/classe in the TagExplorer, we can press the space bar to get the prototype of the tag (the arguments options of the funtion/classe). ## pydoc Easy get informations of pydoc in vim. See website for more info. ### Setup Get the stuff, and place 'pydoc.vim' in `~/.vim/plugin/`. ### Usage :Pydoc foo.bar.baz Split screen an show pydoc info about foo.bar.baz :Pydoc math Split screen and show pydoc math :PydocSearch math Search all stuff concerning math ## vimtips Show tips each time you start vim. See for more info. ### Setup Get the files. Extract the stuff. Move the content of the doc to `~/.vim/doc/` and the plugin to `~/.vim/plugin/`. Download the tips of and place it in `~/.vim/plugin/vimtips/`. ### Usage :TipOfTheDay Split screen an show the tip of the day. ## Other interesting plugins * [surrounding] (http://www.vim.org/scripts/script.php?script_id=1697) # Resources * [Some config files] (http://www.webdragon.net/htmlcss.php#vim) * [A little reference] (http://satz.free.fr/blog/index.php?2004/08/21/38-vim-tips) * * * [vim reference card] (http://tnerual.eriogerg.free.fr/vim.html) *