0

I want to have a vim register that pastes :

#include <iostream>
#include <string>
#define 1e9 + 7
typedef long long ll;
using namespace std;
int main(int argc, char ** argv){

return 0;}

But when I write this in .vimrc ahead of

let @a = '...

I have write it all in a single line. How do I specify the newlines after each include directive.

1

This isn't exactly what you asked for, but I think it may be a "better" solution.

nnoremap cct :-1read $HOME/.vim/cct.c<CR>6ji<Tab>

Typing in cct in Normal mode will paste in the contents from $HOME/.vim/cct.c, which would contain the template you want. Then, 6ji<Tab> moves you to inside the main function, puts you in Insert mode and adds an indent to start you off.

You can of course change the shortcut to your leader key plus something or anything else really. I think this is probably better and more easily updated than putting the whole template directly in your .vimrc.

0

I'm presuming (because you used the [macro] tag) that you want to set the "a register to be a macro, that, when played back, inserts the provided lines.

One way to do that is the following:

let @a = "o"
      \. "#include <iostream>\r"
      \. "#include <string>\r"
      \. "#define 1e9 + 7\r"
      \. "typedef long long ll;\r"
      \. "using namespace std;\r"
      \. "int main(int argc, char ** argv){\r"
      \. "\r"
      \. "return 0;}\<Esc>"

The \ line continuations and . concatenations aren't strictly necessary. You could include the whole lot in a single string, but this will look neater in your .vimrc.

I don't think I'd actually use this myself, though. It would be cleaner to set up a mapping or command to do this, to use Kevin's solution, or to look into the several snippet plugins that provide this feature in a more flexible way.

If you actually just want the register to contain the text so that you can put it with commands such as "ap, you need to alter the code slightly:

let @a = "#include <iostream>\n"
      \. "#include <string>\n"
      \. "#define 1e9 + 7\n"
      \. "typedef long long ll;\n"
      \. "using namespace std;\n"
      \. "int main(int argc, char ** argv){\n"
      \. "\n"
      \. "return 0;}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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