LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   data in file while creation (https://www.linuxquestions.org/questions/linux-newbie-8/data-in-file-while-creation-800611/)

lipun4u 04-07-2010 11:30 AM

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 ??

druuna 04-07-2010 11:43 AM

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:

#!/bin/bash
# -------------------------------------------------------------------------- #
# Syntax : PROGNAME <options>
# -------------------------------------------------------------------------- #
#set -xv
set -u
umask 026

# -------------------------------------------------------------------------- #
# Variables
# ------------------------------------------------------------------ #

# -------------------------------------------------------------------------- #
# Functions
# ------------------------------------------------------------------ #

# -------------------------------------------------------------------------- #
# Main
# ------------------------------------------------------------------ #



exit 0;

# -------------------------------------------------------------------------- #
# End
I saved this file as _simple_bash_frame.vim

To be able to load this file once vim is started (as in vi example01), I put the following in my .vimrc file:
Quote:

" -----------------------------------------------------------------------------
" shift f8 -> create simple bash scripting framework
map <S-F8> :call Sim_Bash_Frame()<CR>
fun! Sim_Bash_Frame()
: 0 r ~/_Kast/_simple_bash_frame.vim
sil exe "%s/PROGNAME/" .
\ expand("%")
exe "$d"
:set syntax=sh
exe ":20"
endfun
Once vi is started this makes it possible to:

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:

#!/bin/bash
# -------------------------------------------------------------------------- #
# Syntax : example01 <options>
# -------------------------------------------------------------------------- #
#set -xv
set -u
umask 026

# -------------------------------------------------------------------------- #
# Variables
# ------------------------------------------------------------------ #

# -------------------------------------------------------------------------- #
# Functions
# ------------------------------------------------------------------ #

# -------------------------------------------------------------------------- #
# Main
# ------------------------------------------------------------------ #
X


exit 0;

# -------------------------------------------------------------------------- #
# End
Hope this helps.

lipun4u 04-07-2010 11:55 AM

@druuna

Would u please suggest me a link or some pdfs that will help me to understand above code ?

unSpawn 04-07-2010 12:06 PM

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()
    :call setline(1, "#!/bin/sh")
endfunction
au BufEnter *.sh if getline(1) == "" | call s:ShellHeader() | endif


druuna 04-07-2010 12:24 PM

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.