How to create a new file containing an existing file's content in vim - vim

I want to create a file with a block of code in it and then, when I open a new file, this block is already in the file without having to copy paste every time. Something like:
:e newfile.cpp/template.cpp
where I now have a new file named newfile.cpp and it has the contents of template.cpp in it; template.cpp will just sit in my directory and wont be changed unless I open it specifically.

One generic possibility is simply to use this command:
:r template.cpp

Use |(bar) to concate two commands:
:e newfile.cpp | r template.cpp
create a new file
read the template

You can leave your template opened and use:
:saveas newfile.cpp
Or, use one snippets plugin like snipMate or XPTemplate to implement a similar functionality.

If rely on templates a lot, you probably need something more advanced. There are several such plugins on vim.org; one is tSkeleton.

Related

using Vim gd on project with multiple code folders

I am using vim to edit Fortran 90 code. The code is structured in the following manner:
./srcs - contains main code file
./libs - contains some module file
If I open the main file in vim and then find some interesting function and press "gD" vim cannot find this function since it does not know where to search. If I copy all the source files to one folder this option does work.
My questions are:
How do I tell vim where my code is.
Can you suggest for a quick manner on how to work with this kind of folder without repeating the use of solution (1).
In this case you might be better off using exuberant ctags.
After installing this program, run ctags -R in the top level directory of your project. Then use e.g. set tags=tags; in your .vimrc to tell vim where your tag file is.
Once this is set up, you can use tag searching.

Showing NERDTree bookmark relative to project/directory

NERDTree keeps list of all bookmark in $HOME/.NERDTreeBookmarks file .When I hit B it show all entries from that file.So I tried having a local copy of this file with project specific bookmark but this does not work :(
Is there a way to see only project related bookmark in NERDTree ?
Here's what I use for git repo specific NERDTree bookmarks.
if isdirectory(expand(".git"))
let g:NERDTreeBookmarksFile = '.git/.nerdtree-bookmarks'
endif
Since there's no way to tell how a project directory is structured, it seems like the use of a shell environment variable is the easiest solution.
First, add path to your bookmarks:
export NERDTREE_BOOKMARKS="/full/path/.NERDTreeBookmarks"
Then add this to your ~/.vimrc or similar:
if !empty($NERDTREE_BOOKMARKS)
if filereadable($NERDTREE_BOOKMARKS)
let g:NERDTreeBookmarksFile = $NERDTREE_BOOKMARKS
endif
endif
Depending on how you develop, the use of environment variables may or may not be a suitable solution. I.e. if you open/close your shell all the time, or have a multitude of shells open at once.
A simpler alternative could be something like this, but it'll only work if you open vim/a file with vim in the directory the bookmarks are located.
if filereadable(".NERDTreeBookmarks")
let g:NERDTreeBookmarksFile = ".NERDTreeBookmarks"
endif
As timss already pointed out, the key setting is the g:NERDTreeBookmarksFile config variable. You need to find a way to manipulate that (global) setting depending on the project that is currently open. Using external environment variables is one solution; I would rather solve this with one of the local vimrc plugins that are available on vim.org; I use localrc.vim - Enable configuration file of each directory myself.
In each different project dir, you create a .local.vimrc script that sets the above variable to the project's bookmark file. (This only works reliably when you only ever open one project at a time in Vim.)

What is the easiest way to rename the file you're currently editing in Vim?

What would be the most practical way to rename the file you're currently editing in Vim without messing up your current splits configuration?
Generally, one would need to ... save the file under a different name, delete the original one, and re-open the new one without making a mess of the current layout.
Anyone have any idea how to do that in one command (function) or less?
:saveas newname will save the buffer with the new name, make that name the current buffer, and set the alternate buffer to the old file.
:call delete(expand('#')) will then delete the file associated with the alternate buffer.
You can easily turn that into a command with something like
:command! -bang -complete=file -nargs=+ Rename saveas<bang> <args> | call delete(expand('#'))`
The user manual provides a thorough description of how to create user commands. Here's an explanation of the elements I'm using above.
-bang allows the command to called as either Rename or Rename! and <bang> in the constructed command is replaced by either an empty string or !, depending on how it is called. This is used to support the same functionality in the :saveas command.
-complete=file will let you tab-complete the path that will be used for the new file, similar to :e and :saveas do.
-nargs=+ specifies that :Rename requires at least one argument (the filename), but can take more. <args> is replaced with whatever arguments are given to :Rename. This allows you to specify the extra arguments that :saveas accepts, so you could do something like :Rename ++enc=latin1 newfile to rename the file to newfile and change the encoding to latin1.
Tim Pope has a plugin that has a function :Rename that does this: vim-eunuch.
You can also do the following sequence of steps:
:saveas newfile
:bw <buffer_for_the_old_file>
:!rm old_file
of course this is not as nice as renaming the file in the shell.
Call up the explorer with :Explorer or just :E, select your file, and then press r to rename.
Use :Move provided by eunuch.
eunuch also provides other useful file operations, like :Remove, sudoedit.

How to add new snippets to snipMate in VIM

Ive just started using the sniptMate plugin for VIM and love it, however, since my collection of snippets isn't huge, I'd like to be able to add new snippets dynamically rather than opening the snippets file and creating a new snippet as I am in the middle of development.
As I am coding something I realize that some specific piece of code can be saved as a snippet to save me trouble of typing the bloat code again, at this time I want to be able to add a snippet without opening the snippet file for the language I am using at the time.
If you are using pathogen, you can write your own snippets without polluting the original ones. To do this, add your snippets to ~/.vim/bundle/mysnippets/snippets/*.snippets. FYI, mysnippets can be any name.
Moreover, it's a convention to add a _.snippets file in that directory where you add snippets which should be available everywhere irrespective of the filetype.
I'm not sure it's meant to be done like this but you can try calling the MakeSnip function from within file you're currently working on. For example:
:call MakeSnip(&ft, "foo", "<foo>${1}</foo>")
&ft will pass the filetype of the file you're currently editing, "foo" is the trigger and "<foo>${1}</foo>" is the replacement text.
Of course, snippets created like this won't persist. So why not have the snippets file open in another buffer, define new snippets there as necessary, then do:
:call ReloadSnippets(&ft)
and your new snippet will be available. You could even define an autocmd to call the ReloadSnippets function when you write the snippets file.
You can put your own snippets in ~/.vim/after/snippets/ or whatever the equivalent on Windows is... read :h snipMate for the filename syntax.
Just place your own snippets (given you want to use them in all files you edit) here:
~/.vim/after/snippets/_.snippets
For example:
snippet test
{"foo": "${1}", "bar": "${2}"}
Snippets for Snipmate are usually stored in the ".vim" directory in the snippets folder.
You can pretty easily edit the snippet file for the language you are working on.
If you use vundle to manage your plugins on Windows and you have installed
vim-snipMate & vim-snippets
If you want to add your customization, say to 'html' filetype, you just go to this folder:
$HOME/vimfiles/bundle/vim-snippets/snippets/
create a new file and name it in this format:
html_bsmith.snippets
(it must be underscore after html, see :help snipmate)
Then vim will load your file automatically.
If you customization can be used anywhere, just put it in the existing file:
_.snippets

In Vim, what is the "alternate file"?

I just ran :help registers in Vim and noticed that # 'contains the name of the alternate file'.
I have seen an example for renaming files that goes like this:
" Save the current file, foo.txt, as bar.txt
:w bar.txt
" Start editing bar.txt
:e#
So apparently in that case, the file you just saved out is the "alternate file."
Can someone give me a more general definition for the "alternate file" and what else you might use it for?
The alternate file is the file that was last edited in the current window. Actually when you use some command to open a new buffer, if the buffer that was displayed had a filename associated with it, that filename is recorded as alternate file name.
See :help alternate-file.
Very useful for...
Pasting in the name of a file I've just been looking at into the current file.
You can use <C-R># for this in insert mode or "#p in normal mode.
Not that useful for...
Jumping back and forth between two files. It does the job very well, but this is just something I don't generally need to do.
Even in the example given, I'd probably use:saveas bar.txt instead.
An Example:
Say if you're doing a bit of C programming and want to call some function. You can't remember the name of the function, so you place a mark on your current location mA and jump into several different files using tags or grep to find out where the function is declared and what it's actually called.
Ah - found it. You can copy the name and return to the mark yiw'A
Uh-oh - we also need to #include the file! Easy - just use the alternate file name register to paste the file name in... Gi#include"<C-R>#"
Be pleased that you've avoided the distraction of having to go back to the function's declaration and copy out the file name via :let #"=#% or something similar.
What I'd rather do when jumping between files:
When editing two files, it's probably easier to split them, so you can keep both on screen at the same time. If I'm editing 2 files I'll usually be comparing them in some way.
Usually I'm interested in 1-3 files (any more and I get confused). I'll often jump into or directly open many other files. Marking the interesting files, or traversing the jump list is usually the way to get around in this case.
If you're editing C/C++ where you're switching between a file and it's header, use a plugin! It will be much more convenient.
I use it in the buffer context to return to the last buffer that I was editing
vim foo bar
:n
:e#
will take you back to foo in that case
I 've always interpreted the "alternate file" as being the "previous file", so it is an handy way to jump back to the buffer you were editing.

Resources