SlideShare a Scribd company logo
1 of 22
VIM and Python - Tips and Tricks




                          http://xkcd.com/378/
Getting Around

h/j/k/l
gg and G – start/end of buffer
^ - first non-blank character in current line
$ - last character on current line
CTRL-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 [] {} ()
Marks

m followed by a letter to set a mark
` followed by the letter – jump to that mark
:marks will list current marks
Vim has some special marks (a few examples):
  `. - jump to position where the last change occured
  `` - jump back to position where you jumped from
lots more found in :help `
Making Changes

y/d/c – yank/delete/change
Y/D/C – same as above, but specifies line*
p/P – paste before/after
combine with movements:
  dw – delete word
  cw – change word
  dG – delete from cursor to end of file
  c`a – change from cursor to mark a
Making Changes
y/d/c – yank/delete/change
Y/D/C – same as above, but specifies line*
p/P – paste after/before
x/r – delete/replace single character
combine with movements:
  dw – delete word
  cw – change word
  dG – delete from cursor to end of file
  c`a – change from cursor to mark a
use the . character to repeat last command
Visual Mode

v – enter visual mode
V – enter visual mode, but operate on a line
CTRL-v -enter visual mode, operate on a column
  Use I to change all columns at once
combine with y/d/c for even more fun
Text Objects

used after an operator or visual mode
i – 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
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>
Undos

u – undo, CTRL-R – redo
see also: undo-branches
time travel
  :earlier Ns, m, h
  :later Ns,m,h
  Can't jump to the future when the code is already
   done, sorry. Hoping for that feature in Vim 8
Splitting Windows

:sp -horizontal split
:vs – vertical split
:new
CTRL-w + movement to move between windows
I remap spacebar for easier commands
Configuration

.vimrc – global settings
.vim directory - plugins, color schemes, syntax
  definitions, etc
browse github for ideas, or just fork one you like
:set – see your current settings
don't be afraid to consult :help to see what things
 do
Config Examples

filtype plugin on – recoginize .py as python
filtype indent on – proper indentation (more on
   this later)
set nu – turn line numbers on
set ruler – see where you are in the file
set t_Co=256 – 256 colors
cmap w!! %!sudo tee > /dev/null %
  made changes to a read-only file? This mapping
   allows you to use :w!! to save using sudo
More Config Examples

au BufReadPost * if line("'"") > 0 && line("'"") <=
 line("$")|execute("normal `"")|endif
  jumps cursor to last edited position on file open
set pastetoggle=<F10>
  easy way to shift to paste mode to not screw up
   indentation
nmap <silent> <F6> :set number!<CR>
  turn line numbers on/off
set iskeyword-=-
  use dash as a word seperator
Indentation

My biggest hang-up when I started with python
Found a python indent file that works pretty well
  Found in .vim/after/indent/
  Autoindents/unindents around keywords
Various other indent definitions available
 (djangohtml is another I use)
= command will re-indent
use > and < to indent keys
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
   haven't had much luck
  pydiction plugin – keyword/modules
tags

I map F4 to rebuild tags file using ctags
CTRL-] - jump to tag under cursor
CTRL-T – return to where you came from
:tag <tagname> - jump to a tag
If there are multiple matches, you select from a
   list
taglist is another useful plugin to view the tags in
  the current file
NERDtree

Popular file explorer
Opens in a new window, which can be re-sized
 and customised
Supports bookmarks
I prefer to just use :e + <tab>
SelectBuf


Similar to a file explorer, but limited to buffers
  already open
A must-have if you normally operate many
 buffers at once
flake-8

wrapper for flake8 (must install seperately)
  PyFlakes
  PEP8
  complexity checker
Can configure to ignore errors
I map to F5, as I also use makeprg for other
  languages using the same key
Doxygen

Generate comment skeletons
  :DoxLic - License
  :DoxAuth - Author
  :Dox - function/class block headers
  Works well out of the box, but config options available
Other Plugins to Check Out

vimpdb
commandT
project
vcscommand
OScan
snippets
netrw
scratch
Resources

http://www.tuxfiles.org/linuxhelp/vimcheat.html
http://www.vim.org/scripts/index.php
http://www.pixelbeat.org/vim.tips.html
http://www.catswhocode.com/blog/100-vim-command
http://www.reddit.com/r/vim/

More Related Content

What's hot

Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
Lin Yo-An
 

What's hot (20)

The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
Writing multi-language documentation using Sphinx
Writing multi-language documentation using SphinxWriting multi-language documentation using Sphinx
Writing multi-language documentation using Sphinx
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
A Python Tutorial
A Python TutorialA Python Tutorial
A Python Tutorial
 
Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
 
Introduction to Clime
Introduction to ClimeIntroduction to Clime
Introduction to Clime
 
Dive into Pinkoi 2013
Dive into Pinkoi 2013Dive into Pinkoi 2013
Dive into Pinkoi 2013
 
Learning Python from Data
Learning Python from DataLearning Python from Data
Learning Python from Data
 
A Look at Command Line Swift
A Look at Command Line SwiftA Look at Command Line Swift
A Look at Command Line Swift
 
Practicing Python 3
Practicing Python 3Practicing Python 3
Practicing Python 3
 
Fluentd Hacking Guide at RubyKaigi 2014
Fluentd Hacking Guide at RubyKaigi 2014Fluentd Hacking Guide at RubyKaigi 2014
Fluentd Hacking Guide at RubyKaigi 2014
 
System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
Debugging concurrency programs in go
Debugging concurrency programs in goDebugging concurrency programs in go
Debugging concurrency programs in go
 
Learning Git with Workflows
Learning Git with WorkflowsLearning Git with Workflows
Learning Git with Workflows
 
Practical Example of grep command in unix
Practical Example of grep command in unixPractical Example of grep command in unix
Practical Example of grep command in unix
 
Python debugging techniques
Python debugging techniquesPython debugging techniques
Python debugging techniques
 
Building robust and friendly command line applications in go
Building robust and friendly command line applications in goBuilding robust and friendly command line applications in go
Building robust and friendly command line applications in go
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
 

Viewers also liked (17)

06 chapter 1
06 chapter 106 chapter 1
06 chapter 1
 
Evaluación de centros educativos
Evaluación de centros educativosEvaluación de centros educativos
Evaluación de centros educativos
 
الكتاب المقدس العهد الجديد - رسالة بطرس الرسول الاولى
الكتاب المقدس   العهد الجديد - رسالة بطرس الرسول الاولىالكتاب المقدس   العهد الجديد - رسالة بطرس الرسول الاولى
الكتاب المقدس العهد الجديد - رسالة بطرس الرسول الاولى
 
Los espiritus del bosque
Los espiritus del bosqueLos espiritus del bosque
Los espiritus del bosque
 
Slides day two
Slides   day twoSlides   day two
Slides day two
 
Facebook
FacebookFacebook
Facebook
 
The sacrament of penance
The sacrament of penanceThe sacrament of penance
The sacrament of penance
 
SCCE Processors and GDPR
SCCE Processors and GDPRSCCE Processors and GDPR
SCCE Processors and GDPR
 
Financial & mgt. accounting
Financial & mgt. accountingFinancial & mgt. accounting
Financial & mgt. accounting
 
Ack prez mlrng
Ack prez mlrngAck prez mlrng
Ack prez mlrng
 
Taiwan (2016)
Taiwan (2016)Taiwan (2016)
Taiwan (2016)
 
Inv pres q22014_-_final
Inv pres q22014_-_finalInv pres q22014_-_final
Inv pres q22014_-_final
 
M2M Framework December 2012
M2M Framework December 2012M2M Framework December 2012
M2M Framework December 2012
 
Ciri-ciri Ikan Hiasan
Ciri-ciri Ikan HiasanCiri-ciri Ikan Hiasan
Ciri-ciri Ikan Hiasan
 
Ag 1 source-FWAA
Ag 1 source-FWAAAg 1 source-FWAA
Ag 1 source-FWAA
 
Later People of the Fertile Crescent
Later People of the Fertile CrescentLater People of the Fertile Crescent
Later People of the Fertile Crescent
 
iEnglish workshop 4
 iEnglish workshop 4 iEnglish workshop 4
iEnglish workshop 4
 

Similar to Vim and Python

DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
brian_dailey
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
root_fibo
 
List command linux fidora
List command linux fidoraList command linux fidora
List command linux fidora
Jinyuan Loh
 

Similar to Vim and Python (20)

DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
 
58518522 study-aix
58518522 study-aix58518522 study-aix
58518522 study-aix
 
List command linux fidora
List command linux fidoraList command linux fidora
List command linux fidora
 
Terminal linux commands_ Fedora based
Terminal  linux commands_ Fedora basedTerminal  linux commands_ Fedora based
Terminal linux commands_ Fedora based
 
Rhel2
Rhel2Rhel2
Rhel2
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Unix Trainning Doc.pptx
Unix Trainning Doc.pptxUnix Trainning Doc.pptx
Unix Trainning Doc.pptx
 
Introduction to Command Line & Batch files
Introduction to Command Line& Batch filesIntroduction to Command Line& Batch files
Introduction to Command Line & Batch files
 
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
AARAV NAYAN OPERATING SYSTEM LABORATORY PCAAARAV NAYAN OPERATING SYSTEM LABORATORY PCA
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
 
VIM for (PHP) Programmers
VIM for (PHP) ProgrammersVIM for (PHP) Programmers
VIM for (PHP) Programmers
 
Linuxppt
LinuxpptLinuxppt
Linuxppt
 
workshop_1.ppt
workshop_1.pptworkshop_1.ppt
workshop_1.ppt
 
Ultimate Unix Meetup Presentation
Ultimate Unix Meetup PresentationUltimate Unix Meetup Presentation
Ultimate Unix Meetup Presentation
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Vim and Python

  • 1. VIM and Python - Tips and Tricks http://xkcd.com/378/
  • 2. Getting Around h/j/k/l gg and G – start/end of buffer ^ - first non-blank character in current line $ - last character on current line CTRL-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. Marks m followed by a letter to set a mark ` followed by the letter – jump to that mark :marks will list current marks Vim has some special marks (a few examples): `. - jump to position where the last change occured `` - jump back to position where you jumped from lots more found in :help `
  • 4. Making Changes y/d/c – yank/delete/change Y/D/C – same as above, but specifies line* p/P – paste before/after combine 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. Making Changes y/d/c – yank/delete/change Y/D/C – same as above, but specifies line* p/P – paste after/before x/r – delete/replace single character combine with movements: dw – delete word cw – change word dG – delete from cursor to end of file c`a – change from cursor to mark a use the . character to repeat last command
  • 6. Visual Mode v – enter visual mode V – enter visual mode, but operate on a line CTRL-v -enter visual mode, operate on a column Use I to change all columns at once combine with y/d/c for even more fun
  • 7. Text Objects used after an operator or visual mode i – 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. 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. Undos u – undo, CTRL-R – redo see also: undo-branches time travel :earlier Ns, m, h :later Ns,m,h Can't jump to the future when the code is already done, sorry. Hoping for that feature in Vim 8
  • 10. Splitting Windows :sp -horizontal split :vs – vertical split :new CTRL-w + movement to move between windows I remap spacebar for easier commands
  • 11. Configuration .vimrc – global settings .vim directory - plugins, color schemes, syntax definitions, etc browse github for ideas, or just fork one you like :set – see your current settings don't be afraid to consult :help to see what things do
  • 12. Config Examples filtype plugin on – recoginize .py as python filtype indent on – proper indentation (more on this later) set nu – turn line numbers on set ruler – see where you are in the file set t_Co=256 – 256 colors cmap w!! %!sudo tee > /dev/null % made changes to a read-only file? This mapping allows you to use :w!! to save using sudo
  • 13. More Config Examples au BufReadPost * if line("'"") > 0 && line("'"") <= line("$")|execute("normal `"")|endif jumps cursor to last edited position on file open set pastetoggle=<F10> easy way to shift to paste mode to not screw up indentation nmap <silent> <F6> :set number!<CR> turn line numbers on/off set iskeyword-=- use dash as a word seperator
  • 14. Indentation My biggest hang-up when I started with python Found a python indent file that works pretty well Found in .vim/after/indent/ Autoindents/unindents around keywords Various other indent definitions available (djangohtml is another I use) = command will re-indent use > and < to indent keys
  • 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 haven't had much luck pydiction plugin – keyword/modules
  • 16. tags I map F4 to rebuild tags file using ctags CTRL-] - jump to tag under cursor CTRL-T – return to where you came from :tag <tagname> - jump to a tag If there are multiple matches, you select from a list taglist is another useful plugin to view the tags in the current file
  • 17. NERDtree Popular file explorer Opens in a new window, which can be re-sized and customised Supports bookmarks I prefer to just use :e + <tab>
  • 18. SelectBuf Similar to a file explorer, but limited to buffers already open A must-have if you normally operate many buffers at once
  • 19. flake-8 wrapper for flake8 (must install seperately) PyFlakes PEP8 complexity checker Can configure to ignore errors I map to F5, as I also use makeprg for other languages using the same key
  • 20. Doxygen Generate comment skeletons :DoxLic - License :DoxAuth - Author :Dox - function/class block headers Works well out of the box, but config options available
  • 21. Other Plugins to Check Out vimpdb commandT project vcscommand OScan snippets netrw scratch