Insert external file and modify each line from scriptAbsolute script-file line-numbers in Vim-function errorsRead file from vimscript variableCurrent VimL script path from inside a functionEnter insert to insert mode from functionHow do I script Vim to download a thesaurus file and save it?Help to implement script that toggle single and double quotesHow to fake a script file name inside a monolithic vimrc?What is a working setup to use Vim plugins UndoTree and Goyo together?Trim/clear new line character from batch script at Windows7 with gvimThe difference between a plugin script and a sourced script
When is the phrase "j'ai bon" used?
Difference between sizeof(struct name_of_struct) vs sizeof(name_of_struct)?
How many times to repeat an event with known probability before it has occurred a number of times
What is the difference between state-based effects and effects on the stack?
Converting 3x7 to a 1x7. Is it possible with only existing parts?
Is it possible to install Firefox on Ubuntu with no desktop enviroment?
What does the output current rating from an H-Bridge's datasheet really mean?
Why does MAGMA claim that the automorphism group of an elliptic curve is order 24 when it is order 12?
Are there any rules for identifying what spell an opponent is casting?
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
How to avoid offending original culture when making conculture inspired from original
Using roof rails to set up hammock
How to remove multiple elements from Set/Map AND knowing which ones were removed?
Creating polygon with exact measurements in QGIS 3
Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?
How to search for Android apps without ads?
Leveling up and Getting Items!
For Saintsbury, which English novelists constituted the "great quartet of the mid-eighteenth century"?
How did the European Union reach the figure of 3% as a maximum allowed deficit?
Print the phrase "And she said, 'But that's his.'" using only the alphabet
How can the US president give an order to a civilian?
newcommand with parameter blank or zero
Why can't we feel the Earth's revolution?
Struggling to present results from long papers in short time slots
Insert external file and modify each line from script
Absolute script-file line-numbers in Vim-function errorsRead file from vimscript variableCurrent VimL script path from inside a functionEnter insert to insert mode from functionHow do I script Vim to download a thesaurus file and save it?Help to implement script that toggle single and double quotesHow to fake a script file name inside a monolithic vimrc?What is a working setup to use Vim plugins UndoTree and Goyo together?Trim/clear new line character from batch script at Windows7 with gvimThe difference between a plugin script and a sourced script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:
fun InsertLicense()
if filereadable('LICENSE')
let license = 'LICENSE'
elseif filereadable('LICENSE.txt')
let license = 'LICENSE.txt'
else
let license = ''
endif
if line('$') < 3 && len(license) > 0
echo 'inserting license from ' . license
call append(0, readfile(license))
endif
endfun
It's working fine except one problem: LICENSE
file is a plaintext file, e.g.:
Copyright 2019 OrgName
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
...
but I need to wrap it as Java multiline comment:
/*
* Copyright 2019 OrgName
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
...
*/
Is it possible to insert *
before each line of license
file using vim scripting language?
vimscript
New contributor
add a comment |
I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:
fun InsertLicense()
if filereadable('LICENSE')
let license = 'LICENSE'
elseif filereadable('LICENSE.txt')
let license = 'LICENSE.txt'
else
let license = ''
endif
if line('$') < 3 && len(license) > 0
echo 'inserting license from ' . license
call append(0, readfile(license))
endif
endfun
It's working fine except one problem: LICENSE
file is a plaintext file, e.g.:
Copyright 2019 OrgName
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
...
but I need to wrap it as Java multiline comment:
/*
* Copyright 2019 OrgName
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
...
*/
Is it possible to insert *
before each line of license
file using vim scripting language?
vimscript
New contributor
add a comment |
I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:
fun InsertLicense()
if filereadable('LICENSE')
let license = 'LICENSE'
elseif filereadable('LICENSE.txt')
let license = 'LICENSE.txt'
else
let license = ''
endif
if line('$') < 3 && len(license) > 0
echo 'inserting license from ' . license
call append(0, readfile(license))
endif
endfun
It's working fine except one problem: LICENSE
file is a plaintext file, e.g.:
Copyright 2019 OrgName
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
...
but I need to wrap it as Java multiline comment:
/*
* Copyright 2019 OrgName
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
...
*/
Is it possible to insert *
before each line of license
file using vim scripting language?
vimscript
New contributor
I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:
fun InsertLicense()
if filereadable('LICENSE')
let license = 'LICENSE'
elseif filereadable('LICENSE.txt')
let license = 'LICENSE.txt'
else
let license = ''
endif
if line('$') < 3 && len(license) > 0
echo 'inserting license from ' . license
call append(0, readfile(license))
endif
endfun
It's working fine except one problem: LICENSE
file is a plaintext file, e.g.:
Copyright 2019 OrgName
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
...
but I need to wrap it as Java multiline comment:
/*
* Copyright 2019 OrgName
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
...
*/
Is it possible to insert *
before each line of license
file using vim scripting language?
vimscript
vimscript
New contributor
New contributor
New contributor
asked Jun 7 at 12:06
g4s8g4s8
1183
1183
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
readfile()
returns a list, each line being in a separate list item.
You can therefore make use of map()
to change the list inplace:
let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')
This makes use of lambda expression and wraps a the comment characters /*
and */
around it.
Alternatively, you can do this:
:let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']
which adds only /*
at the start of the file and */
at the end of the file and for each line simply prepends the *
.
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
add a comment |
Christian’s answer is great—for java files. But it requires some care for different filetypes.
One alternative would be to lean on the machinery of tpope’s commentary and do something like
" Go comment a paragraph
normal gcap
After you insert the license text.
Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
g4s8 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f20244%2finsert-external-file-and-modify-each-line-from-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
readfile()
returns a list, each line being in a separate list item.
You can therefore make use of map()
to change the list inplace:
let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')
This makes use of lambda expression and wraps a the comment characters /*
and */
around it.
Alternatively, you can do this:
:let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']
which adds only /*
at the start of the file and */
at the end of the file and for each line simply prepends the *
.
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
add a comment |
readfile()
returns a list, each line being in a separate list item.
You can therefore make use of map()
to change the list inplace:
let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')
This makes use of lambda expression and wraps a the comment characters /*
and */
around it.
Alternatively, you can do this:
:let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']
which adds only /*
at the start of the file and */
at the end of the file and for each line simply prepends the *
.
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
add a comment |
readfile()
returns a list, each line being in a separate list item.
You can therefore make use of map()
to change the list inplace:
let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')
This makes use of lambda expression and wraps a the comment characters /*
and */
around it.
Alternatively, you can do this:
:let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']
which adds only /*
at the start of the file and */
at the end of the file and for each line simply prepends the *
.
readfile()
returns a list, each line being in a separate list item.
You can therefore make use of map()
to change the list inplace:
let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')
This makes use of lambda expression and wraps a the comment characters /*
and */
around it.
Alternatively, you can do this:
:let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']
which adds only /*
at the start of the file and */
at the end of the file and for each line simply prepends the *
.
answered Jun 7 at 12:15
Christian BrabandtChristian Brabandt
16.5k2848
16.5k2848
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
add a comment |
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
1
1
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
Damn, it took me 4 more minutes to come up with a much less elegant solution! :)
– statox♦
Jun 7 at 12:21
1
1
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
Upps, Sorry for that ;)
– Christian Brabandt
Jun 7 at 12:22
add a comment |
Christian’s answer is great—for java files. But it requires some care for different filetypes.
One alternative would be to lean on the machinery of tpope’s commentary and do something like
" Go comment a paragraph
normal gcap
After you insert the license text.
Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.
add a comment |
Christian’s answer is great—for java files. But it requires some care for different filetypes.
One alternative would be to lean on the machinery of tpope’s commentary and do something like
" Go comment a paragraph
normal gcap
After you insert the license text.
Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.
add a comment |
Christian’s answer is great—for java files. But it requires some care for different filetypes.
One alternative would be to lean on the machinery of tpope’s commentary and do something like
" Go comment a paragraph
normal gcap
After you insert the license text.
Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.
Christian’s answer is great—for java files. But it requires some care for different filetypes.
One alternative would be to lean on the machinery of tpope’s commentary and do something like
" Go comment a paragraph
normal gcap
After you insert the license text.
Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.
edited Jun 7 at 16:43
answered Jun 7 at 12:46
D. Ben KnobleD. Ben Knoble
3,2331521
3,2331521
add a comment |
add a comment |
g4s8 is a new contributor. Be nice, and check out our Code of Conduct.
g4s8 is a new contributor. Be nice, and check out our Code of Conduct.
g4s8 is a new contributor. Be nice, and check out our Code of Conduct.
g4s8 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Vi and Vim Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f20244%2finsert-external-file-and-modify-each-line-from-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown