I am trying to use vim substitute commands to transform type specific C-code into macro Code. This example snippet
#define LIST_GEN_FUNCS(TYPE)
struct list_##TYPE* list_generate() {
struct list_##TYPE* ret = malloc(sizeof(struct list));
ret->len = 0; ret->first = 0;
return ret;
}
needs to look like that:
#define LIST_GEN_FUNCS(TYPE)
struct list_##TYPE* list_generate() { \
struct list_##TYPE* ret = malloc(sizeof(struct list)); \
ret->len = 0; ret->first = 0; \
return ret; \
} \
To do this, i need a way to fill up all columns with whitespaces until a fixed line length, then add ‘\’ after each line ending.
%s/$/\= submatch(0) . " " . repeat(" ", 78 - LENGTH_OF_LINE)
Now, here is my problem. I was not able to find out how to get the length of a matched line. Could anyone tell me, what to insert as LENGTH_OF_LINE?
One solution is to add spaces to the end of the line and then delete to a specific column. For example:
The
<C-v><Esc>inserts a literal^[character and the<cr>ends the command.If you want to find the length of a line in VimL you can use
strlen(getline(6))for example if you wanted to get the length of line 6. If you wanted to implement that into your existing solution you could doAs Zyx says in the comments, a better way is