Results 1 to 10 of 16
Hi All,
I want to modify the vi working like - whenever i create a new file it has to open
with predefined lines(with some text line in it). Please ...
- 05-30-2011 #1Just Joined!
- Join Date
- May 2010
- Posts
- 7
working with vim editor
Hi All,
I want to modify the vi working like - whenever i create a new file it has to open
with predefined lines(with some text line in it). Please help me out...

Thank you for your time..
- 05-31-2011 #2Just Joined!
- Join Date
- Nov 2007
- Posts
- 26
Can't you start vi (or vim) and open a file with the predefined lines like this:
vi linesIwant
then use "save as" so as not to overwrite your linesIwant file.
- 05-31-2011 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 31
The command form you want is:
vim -s scriptfile filetoedit
where scriptfile is a set of vi commands and filetoedit is the one you want to edit. When all commands in scriptfile are done, input reverts to the keyboard. Input in scriptfile works exactly like the keyboard input, which means if you need a colon to get into command mode, then you have to put it in the scriptfile. Hope this helps.
- 05-31-2011 #4Just Joined!
- Join Date
- May 2010
- Posts
- 7
---------------------------------------------------------
i putted echo '#!/usr/bin/perl' in the 'scriptfile' and ENTERD vim -s scriptfile filetoedit.
in 'filetoedit' its coming as "o '#!/usr/bin/perl'" highlighted one.
i need some environment variable kind of thing, where one time i should be able to set it and unset when its not required.
- 05-31-2011 #5Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
You need to edit (or create) the "~/.vimrc" file, (thats the ".vimrc" file in your home directory). The commands in this file are executed as soon as you start vim. For example, if you always want line numbering, set "number". If you don't want vim to behave like old-fashioned "vi", set "nocompatible". Your "~/.vimrc" file would look like this:
If you want to define different commands for different kinds of files, like setting tabs to be 4 spaces for Python programs, but tabs are 8 spaces in C/C++ files, you need to create a directory in your home folder called ".vim/ftplugin". To set vim variables for a kind of file, like to set parameters automatically every time you edit a Python program, you need to change the parameters in your "~/.vim/ftplugin/python.vim" file (you may need to create this file). Vim will execute this file after executing the global ftplugin file located at "/usr/share/vim/vimcurrent/ftplugin/python.vim", so you get the global vim Python syntax highlighting, but you also get to override these defaults with the ftplugin file in your home directory.Code:set number set nocompatible
For more information, of course, consult the Vim documentation, by executing ":help ftplugin" or ":help vimrc" from within the Vim program.
- 05-31-2011 #6Just Joined!
- Join Date
- May 2010
- Posts
- 7
my requirement
Hi All,
my requirement very simple, is like whenever i create a new file then it has to come with some predefined lines else it has to work with the normal behaviour of vi/vim.
pls post your comments
- 05-31-2011 #7Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
You are talking about a "skeleton" or "template" file.
What you are talking about is called a "skeleton file" or a "template file".
If you type in either ":help skeleton" or ":help template" in your vim editor, it will tell you exactly how to do what you want. But for your convenience, I will tell you right now.
First, place your skeleton file in a known location, for example, in "~/.vim/other/skeleton.txt". Then, you must edit your "~/.vimrc" file (you may need to create it) and add the following lines to the end of the file:The "*" is a glob-pattern, which matches all files. This tells vim to read the skeleton file for any file you open. You can change it to anything, for example "*.sh" if you only want your skeleton file to be read for new files created with the ".sh" file extension, or "Example*.java" for only java files that begin with the name "Example".Code:" ~/.vimrc function MyLoadInitText() 0 read ~/.vim/other/skeleton.txt " You can enter other commands here as well. endfunction autocmd BufNewFile,BufEnter * call MyLoadInitText()
- 05-31-2011 #8Just Joined!
- Join Date
- May 2010
- Posts
- 7
thank you ramin,
the code you provided is working fine. but i need 2 checks.
note: i replaced * with *.pl
1) suppose i am opening a file as - "vim 1.pl" (newfile), here text form 'skeleton.txt' is coming twice(i need only once).
2) if i open already existing '.pl' file, at this moment it has to keep quiet(no need to insert the text mentioned in the 'skeleton.txt' file).
- 05-31-2011 #9Just Joined!
- Join Date
- Jul 2008
- Posts
- 31
The line you entered above are appropriate for the beginning of a PERL script, as you know. But they are not VI commands, and the form I told you is for VI commands ONLY. So if, for example, you wanted to insert the line you entered above as the first line of every script, you would use the scriptfile
You are trying to use ECHO to set the line in the file, but it is not a VI command, so it will not work like that. What you are getting is three letters, E, C, and H that are ignored and the O, which opens input mode.Code:i#!/usr/bin/perl
You have to remember that every time you start VI with a scriptfile, it will reapply whatever commands you entered, which means it will reinsert the line above each time and that of course is redundant.
- 05-31-2011 #10Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
Sorry, I misunderstood you! I thought you wanted to include it for all types of files. It is much easier if you only want to include your skeleton file when editing only specific types of files (for example Perl programs).
Instead of writing the autocmd in the "~/.vimrc" file, you should define your own "~/.vim/ftplugin/perl.vim" file. This file is executed when Vim's default file-type detection mechanism recognizes you have opened a new Perl file. It is much better than editing "~/.vimrc", because it is only executed for Perl files, not all files. If you would like to do this, you should edit (or create) the file "~/.vim/ftplugin/perl.vim" and include this code:Where of course you must define the "~/.vim/other/perlskel" file to be a file containing the line "#!/usr/bin/perl", or whatever text you wish. The autocmd ensures that the skeleton file will be read only when a new file is created, and not when an existing file is opened.Code:" ~/.vim/ftplugin/perl.vim autocmd BufNewFile * 0 read ~/.vim/other/perlskel
Notice, you can do this for any programming language. For example, for C files, you can edit "~/.vim/ftplugin/c.vim", and include a different skeleton file. It is best practice to have a different "ftplugin/*.vim" file for each programming language you commonly use.


Reply With Quote
