Banner of None

How can Python help you edit your files even faster with vim


Category: vim

Date: July 2020
Views: 1.04K


Often times when we are editing our files, we need a faster way to produce hundreds, even thousands of lines with repetitive content or lines that have some variables that change on every line and it would be tedious to write them manually. Here comes Python.

lets say I wanted to tag 29 articles in my website with the same 8 tags; that's 232 instructions. I could go about them one by one and add 8 tags to each one but that would be extremely tiresome and quite frankly boring and it would take me half a day to complete. so instead I'll make text file lets call it mytable.sql and in it I'll put the mysql instructions to add those tags. First we write this line:

	
insert into mytable (id,tagid) values
	

And then in another vim buffer we write in a file lets call it a.py these 3 lines of code:

	
for i in range(1,30):
	for j in range(1,9):
		print("(",i,",",j,"),")
	

All I have to do now is to type ";c" with ";" being my vim leader in my vimrc file and ";c" is a keymap to execute the Python code above to produce the 232 lines I need to add my tags Once Python code was executed I'll copy the output from the buffer (an easy task with vim, just another keymap in my vimrc file) and copy it to my first file. The result will be like this:

	
insert into mytable (id,tagid) values
( 1 , 1 ),
( 1 , 2 ),
( 1 , 3 ),
( 1 , 4 ),
( 1 , 5 ),
( 1 , 6 ),
( 1 , 7 ),
( 1 , 8 ),
( 2 , 1 ),
( 2 , 2 ),
( 2 , 3 ),
( 2 , 4 ),
( 2 , 5 ),
( 2 , 6 ),
( 2 , 7 ),
( 2 , 8 ),
( 3 , 1 ),
( 3 , 2 ),
( 3 , 3 ),
( 3 , 4 ),
( 3 , 5 ),
( 3 , 6 ),
( 3 , 7 ),
( 3 , 8 ),
( 4 , 1 ),
( 4 , 2 ),
( 4 , 3 ),
( 4 , 4 ),
( 4 , 5 ),
( 4 , 6 ),
( 4 , 7 ),
( 4 , 8 ),
( 5 , 1 ),
( 5 , 2 ),
( 5 , 3 ),
( 5 , 4 ),
( 5 , 5 ),
( 5 , 6 ),
( 5 , 7 ),
( 5 , 8 ),
( 6 , 1 ),
( 6 , 2 ),
( 6 , 3 ),
( 6 , 4 ),
( 6 , 5 ),
( 6 , 6 ),
( 6 , 7 ),
( 6 , 8 ),
( 7 , 1 ),
( 7 , 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 ),
( 18 , 8 ),
( 19 , 1 ),
( 19 , 2 ),
( 19 , 3 ),
( 19 , 4 ),
( 19 , 5 ),
( 19 , 6 ),
( 19 , 7 ),
( 19 , 8 ),
( 20 , 1 ),
( 20 , 2 ),
( 20 , 3 ),
( 20 , 4 ),
( 20 , 5 ),
( 20 , 6 ),
( 20 , 7 ),
( 20 , 8 ),
( 21 , 1 ),
( 21 , 2 ),
( 21 , 3 ),
( 21 , 4 ),
( 21 , 5 ),
( 21 , 6 ),
( 21 , 7 ),
( 21 , 8 ),
( 22 , 1 ),
( 22 , 2 ),
( 22 , 3 ),
( 22 , 4 ),
( 22 , 5 ),
( 22 , 6 ),
( 22 , 7 ),
( 22 , 8 ),
( 23 , 1 ),
( 23 , 2 ),
( 23 , 3 ),
( 23 , 4 ),
( 23 , 5 ),
( 23 , 6 ),
( 23 , 7 ),
( 23 , 8 ),
( 24 , 1 ),
( 24 , 2 ),
( 24 , 3 ),
( 24 , 4 ),
( 24 , 5 ),
( 24 , 6 ),
( 24 , 7 ),
( 24 , 8 ),
( 25 , 1 ),
( 25 , 2 ),
( 25 , 3 ),
( 25 , 4 ),
( 25 , 5 ),
( 25 , 6 ),
( 25 , 7 ),
( 25 , 8 ),
( 26 , 1 ),
( 26 , 2 ),
( 26 , 3 ),
( 26 , 4 ),
( 26 , 5 ),
( 26 , 6 ),
( 26 , 7 ),
( 26 , 8 ),
( 27 , 1 ),
( 27 , 2 ),
( 27 , 3 ),
( 27 , 4 ),
( 27 , 5 ),
( 27 , 6 ),
( 27 , 7 ),
( 27 , 8 ),
( 28 , 1 ),
( 28 , 2 ),
( 28 , 3 ),
( 28 , 4 ),
( 28 , 5 ),
( 28 , 6 ),
( 28 , 7 ),
( 28 , 8 ),
( 29 , 1 ),
( 29 , 2 ),
( 29 , 3 ),
( 29 , 4 ),
( 29 , 5 ),
( 29 , 6 ),
( 29 , 7 ),
( 29 , 8 );
	

And now mytable.sql is ready to be executed by mysql server and the tags to be addedd into mytable. I should point out that the numbers 1 to 29 are the unique ids of my articles and 1 to 8 are the unique ids of my tags

Now its time to show you the relevant part of my vimrc file and the vim magic to do this:
we go to the ~/.vim/ftplugin folder or create it if it does not exist.
we create a file: python.vim
and we put this vim code in it:

	
"file : ~/.vim/ftplugin/python.vim

function! RunPython()
	let s:current_file = expand("%")
	enew|silent execute ".!python " . shellescape(s:current_file, 1)
	setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
endfunction

setlocal foldmethod=indent
setlocal foldnestmax=1
setlocal colorcolumn=0
setlocal completeopt-=preview

nnoremap <Leader>c :call RunPython()<CR>
	

And that's it. and you can do the same with all the scripting languages like shell scripting .



1.04K views

Previous Article Next Article

0 Comments, latest

No comments.