data in file while creation
I want to add some starting information in a file while creation.
like if I type vi test.sh then #!/bin/bash will be added automatically. Somebosy suggest me how to do this ?? |
Hi,
I solved this the following way: I created a template that holds the basic framework of a specific type (a very simple bash framework in this case). It looks like this: Quote:
To be able to load this file once vim is started (as in vi example01), I put the following in my .vimrc file: Quote:
a) load the framework (: 0 r ~/_Kast/_simple_bash_frame.vim) b) change PROGNAME in the framework file to the file vi was started with (example01 in this example) c) set syntax highlighting to sh d) set the cursor on line 20 This is how the file looks after running vi example01 and pressing shift-F8 (X marks the cursor position): Quote:
|
@druuna
Would u please suggest me a link or some pdfs that will help me to understand above code ? |
Adding these lines to your ~/.vimrc should add "#!/bin/sh" each time the first line is empty when you execute 'vi /path/some/filename.sh':
Code:
function! <SID>ShellHeader() |
Hi again,
Well, I got most of the information from the help pages that come with vim (:help map -> shows info about the map function). I do remember using this page as well (no explanations, but this one gave me the idea and the file to start with): Map function keys to compile and run your code (Vim Wikia) I'll give you a breakdown of the commands used in my .vimrc example in the previous post: map <S-F8> :call Sim_Bash_Frame()<CR> This line assigns the SHIFT-F8 keycombo to a function call, the function name is Sim_Bash_Frame(). The carriage return (<CR>) must be added. fun! Sim_Bash_Frame() and endfun This is the declaration of the function (the one called Sim_Bash_Frame that is run when SHIFT-F8 is pressed. All in between these tags is "exectued" when the keycombo is pressed. : 0 r ~/_Kast/_simple_bash_frame.vim this loads the framework file into the current vim session. sil exe "%s/PROGNAME/" . \ expand("%") sil tells vim to execute the next command silently (no extra messages are created, if it fails it will not tell you anything). exe tells vim to executes the string that results from the evaluation of {expr1} as an Ex command. I.e: "%s/PROGNAME/" . \ expand("%") is seen as en ex statement (in this case a substitution). expand("%") this expands all between the double quotes, % is special, it means the current filename (example01 in this example). exe "$d" this deletes the last line (an extra empty line is added while loading the framework file). :set syntax=sh this sets syntax highlighting for shell files exe ":20" this put the cursor on line 20. Feed your search engine with these words: vim map execute function. There are enough examples (and some explanations) out there to get you going. And don't forget vim's help files. Hope this clears things up a bit. |
All times are GMT -5. The time now is 11:27 PM. |