LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-07-2010, 11:30 AM   #1
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
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 ??
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-07-2010, 11:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 04-07-2010 at 11:44 AM.
 
Old 04-07-2010, 11:55 AM   #3
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
@druuna

Would u please suggest me a link or some pdfs that will help me to understand above code ?
 
Old 04-07-2010, 12:06 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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
 
Old 04-07-2010, 12:24 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
3 members found this post helpful.
  


Reply

Tags
framework, function, vim


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript:Combobox creation taking data from DB On button click Manashi Programming 5 03-12-2015 02:38 AM
changing default file permissions upon file creation ceci2 Linux - Newbie 7 10-01-2009 07:27 AM
how to find time of file creation of a given file??? raklo Linux - General 4 08-13-2007 05:28 AM
mysql - data validation at the table-creation-time prabhatsoni Linux - Software 2 03-24-2006 12:13 AM
RPM Spec file creation: %file section question davidas Linux - Newbie 0 03-16-2004 10:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration