My init.vim code:

autocmd BufNewFile *.cpp 0r C:\Users\User\Desktop\cp\Template.cpp

When i create a .cpp file, Template.cpp code is imported in that cpp file.
upto this point, everything is fine.

but while doing that, vim always leaves a last blank line (which i don't want)

#include <iostream>
using namespace std;

int main(){
    // code goes here
-- unwanted last line --

So can anyone help me fix this problem?

1 Answer 1


:r inserts the contents of a file. It does not overwrite existing contents. Since a new file opened in Vim can't have zero lines* you'll be adding the file's contents to one blank line.

Simplest thing to do is just add an additional command to your autocommand:

autocmd BufNewFile *.cpp 0r C:\Users\User\Desktop\cp\Template.cpp | $d

So we use | to indicate an additional command and :d[elete] with a "range" of $ (last line).

Or, if you'd prefer to start with the cursor at the beginning of the buffer:

autocmd BufNewFile *.cpp r C:\Users\User\Desktop\cp\Template.cpp | 1d

* According to various definitions/specifications (including POSIX) a "text file" must contain at a minimum a terminating line feed character. IOW, totally empty files are not considered text files and Vim is a text file editor...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.