Vim and Python


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Vim and Python

  1. 1. VIM and Python - Tips and Tricks
  2. 2. Getting Aroundh/j/k/lgg and G – start/end of buffer^ - first non-blank character in current line$ - last character on current lineCTRL-u / CTRL-d – page up / page down:n – go to line number n.H/M/L – go to first/middle/last line on screen*% - jump to corresponding bracket [] {} ()
  3. 3. Marksm followed by a letter to set a mark` followed by the letter – jump to that mark:marks will list current marksVim has some special marks (a few examples): `. - jump to position where the last change occured `` - jump back to position where you jumped fromlots more found in :help `
  4. 4. Making Changesy/d/c – yank/delete/changeY/D/C – same as above, but specifies line*p/P – paste before/aftercombine with movements: dw – delete word cw – change word dG – delete from cursor to end of file c`a – change from cursor to mark a
  5. 5. Making Changesy/d/c – yank/delete/changeY/D/C – same as above, but specifies line*p/P – paste after/beforex/r – delete/replace single charactercombine with movements: dw – delete word cw – change word dG – delete from cursor to end of file c`a – change from cursor to mark ause the . character to repeat last command
  6. 6. Visual Modev – enter visual modeV – enter visual mode, but operate on a lineCTRL-v -enter visual mode, operate on a column Use I to change all columns at oncecombine with y/d/c for even more fun
  7. 7. Text Objectsused after an operator or visual modei – inner / a – ambient a / i – operate on block of a( / i( - operate of block of () a{ / i{ - operate of block of {} at / it – operate of html or xml tags
  8. 8. Search/ or ? - for a basic search (forward or backward) I recommend :set ignore case, :set smartcase and :set incsearch n to repeat search, or N to repeat in opposite direction*/# - find next/previous of word under cursor[I – list lines with word under cursor set up a map to make this easier: :map <space>f [I:let nr = input("Which one: ")<Bar>exe "normal " . nr ."[t"<CR>
  9. 9. Undosu – undo, CTRL-R – redosee also: undo-branchestime travel :earlier Ns, m, h :later Ns,m,h Cant jump to the future when the code is already done, sorry. Hoping for that feature in Vim 8
  10. 10. Splitting Windows:sp -horizontal split:vs – vertical split:newCTRL-w + movement to move between windowsI remap spacebar for easier commands
  11. 11. Configuration.vimrc – global settings.vim directory - plugins, color schemes, syntax definitions, etcbrowse github for ideas, or just fork one you like:set – see your current settingsdont be afraid to consult :help to see what things do
  12. 12. Config Examplesfiltype plugin on – recoginize .py as pythonfiltype indent on – proper indentation (more on this later)set nu – turn line numbers onset ruler – see where you are in the fileset t_Co=256 – 256 colorscmap w!! %!sudo tee > /dev/null % made changes to a read-only file? This mapping allows you to use :w!! to save using sudo
  13. 13. More Config Examplesau BufReadPost * if line(""") > 0 && line(""") <= line("$")|execute("normal `"")|endif jumps cursor to last edited position on file openset pastetoggle=<F10> easy way to shift to paste mode to not screw up indentationnmap <silent> <F6> :set number!<CR> turn line numbers on/offset iskeyword-=- use dash as a word seperator
  14. 14. IndentationMy biggest hang-up when I started with pythonFound a python indent file that works pretty well Found in .vim/after/indent/ Autoindents/unindents around keywordsVarious other indent definitions available (djangohtml is another I use)= command will re-indentuse > and < to indent keys
  15. 15. Autocomplete<tab> invokes autocomplete Use options wildmode and wildmenu to change how this works (I use list:longest,full)Also work on filenames/directories (:e <tab>)There are other forms of autocomplete: Omnicomplete – Heuristics based completion, I havent had much luck pydiction plugin – keyword/modules
  16. 16. tagsI map F4 to rebuild tags file using ctagsCTRL-] - jump to tag under cursorCTRL-T – return to where you came from:tag <tagname> - jump to a tagIf there are multiple matches, you select from a listtaglist is another useful plugin to view the tags in the current file
  17. 17. NERDtreePopular file explorerOpens in a new window, which can be re-sized and customisedSupports bookmarksI prefer to just use :e + <tab>
  18. 18. SelectBufSimilar to a file explorer, but limited to buffers already openA must-have if you normally operate many buffers at once
  19. 19. flake-8wrapper for flake8 (must install seperately) PyFlakes PEP8 complexity checkerCan configure to ignore errorsI map to F5, as I also use makeprg for other languages using the same key
  20. 20. DoxygenGenerate comment skeletons :DoxLic - License :DoxAuth - Author :Dox - function/class block headers Works well out of the box, but config options available
  21. 21. Other Plugins to Check OutvimpdbcommandTprojectvcscommandOScansnippetsnetrwscratch
  22. 22. Resources