I want to automate certain text manipulation (essentially writing a plugin), but for easiness, I wish to minimize the use of Vim script whenever possible. This is because, indeed it is much harder to look for a documentation for writing something in Vim script, than in a well-established script language in any sort, Python, Lua, and so on.
Thus I am not sure what the best method is, to call an external binary or script that modifies the current buffer. For concreteness, let me say that I want to remove all newlines in the current buffer. Surely this can be done by a simple replacement, but for sake of illustration let me implement it in Python.
#!/usr/bin/env python import os # to get path name import sys # to get CLI arguments srcPath = os.path.realpath(__file__) dirPath = os.path.dirname(srcPath) dataPath = dirPath + "/" + sys.argv myFile=open(dataPath,'r') content = myFile.read() myFile.close() content = content.replace("\n", "") myFile=open(dataPath,'w') myFile.write(content) myFile.close()
And when editing, say,
test.txt, run this line in Vim: (of course I can choose a shorter name)
:cabbrev remove_newline :! ./replace.py %
Now I can simply run
I see several problems in this approach:
The buffer has to be saved in advance, otherwise there is a warning
W12: Warning: File "test.txt" has changed and the buffer was changed in Vim as well See ":help W12" for more info. [O]K, (L)oad File, Load (A)ll, (I)gnore All: "test.txt" "test.txt" [noeol] 1L, 5C
Such warning is understandable, but it is cumbersome to save file every time before I hit my shortcut. Calling an external script this way is not seamlessly integrated with the i/o action of buffer, but considered an alien action.
I want to open a new tab that temporary holds error message, but don't know how to connect the std i/o into a new Vim buffer.
I want to select text and, upon hitting my shortcut, only replace selected text. But I don't know how to specify location selected in Vim, into called script.
Or maybe my intention is not suitable to implement in Vim? I know very little about plugins. It seems Youcompleteme is written in Python, but it remains overwhelming of me to find where Vim "communicates" with Python in the Repo (which is all I have to know for present purpose).