Opening a file via Vim on GIT - vim

I'm following a tutorial on Uniswap forking (just to learn how this works) and I'm stuck a particular step.
How does one go from:
vim migrations/2_deploy_contracts.js
to
I'm only able to see:
How do I see the folders and directories ?

The user in that particular video is using the NERDTree plugin for Vim. Vim is very powerful and extensible, and it's possible to load a variety of extensions written in Vimscript to customize the interface, add editor features (such as LSP support), or various other functionality.
They're also using a custom colorscheme which is probably based on the Solarized palette. You can also load a custom colorscheme with the :colorscheme ex command.

There is another thing to notice, besides mentioned NerdTree plugin.
I think you are running your command from the wrong place.
vim migrations/2_deploy_contracts.js
This command tries to open the file set by relative path, or creates a new file, if that does not exist. As we see from your screenshots - the file exists in the tutorial, but it does not exist on your machine (the [+] mark after the filename on the second screenshot shows that).
My guess you need to cd to the right directory first (tutorial project root) and then only run your vim command to open the file.
As for your question about seeing the files and directories, you can do it without NerdTree plugin, using built-in netrw. Just type :Ex in vim normal mode.

Related

How to execute a file everytime before opening Vim

I have recently started using vim and I really like it. I have added a few easy mappings in my vimrc file.
But the problem is I get to use a lot of remote machines a lot of time and I can't copy my vimrc on to them but most of the times I won't have enough permissions to do that.
So, I was wondering if there is any way I can put all my vim mappings in a file and tell vim to run it every time it loads, just like a vimrc?
The action that is "parsing" the .vimrc is called source.
In runtime, you can reapply/reparse your .vimrc by using
:source ~/.vimrc
So if you can somehow copy your .vimrc, even if not in your home, but a folder like /tmp you should be able to source it from there, with
:source /tmp/.vimrc
This question has more details and solutions.
One option would be to specify an alternative .vimrc file while launching the program.
The vim man pages has this to say about specifying a vimrc file:
-u {vimrc}
Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this
to edit a special kind of files. It can also be used to skip all
initializations by giving the name "NONE".
See ":help initialization" within vim for more details.
Note that this option overrides the default vimrc file, so you'll have to specify all of your settings/options in this file.
As was mentioned in another answer, you can place your custom vimrc file anywhere you want (or have access to) and then specify the the -u option with the path to your vimrc file.
You could even combine this method with managing your custom vimrc file in an online version control system (like github) - this way you will be able to wget your file from the web instead of having to manually copy it from machine to machine.

How to create my custom vim snippets?

I want to create some snippets when writting c++.
for example:
create a file, cpp.snippets.
priority -1
snippet exam
This is an example!
endsnippet
and put it in ~/.vim/my-snippets/snippets/.
then, add following statement in ~/.vimrc:
set runtimepath+=~/.vim/my-snippets/
let g:UltiSnipsSnippetsDir='~/.vim/my-snippets/'
let g:UltiSnipsSnippetDirectories=["snippets"]
But it not work, how can i fix it ?
UltiSnips plugin includes detailed documentation. Have you read the following help page?
:help UltiSnips-snippet-search-path
Update:
One of the things that was obvious when I read that help section was that in UltiSnips the name "snippets" can't be used in g:UltiSnipsSnippetDirectories because it is reserved for snipMate compatible snippets. This does not happen in the link shared in the comment below, where the name "my-snippets" is used instead.
I do not use UltiSnips, but from the documentation I would suggest the following approach:
Do not set g:UltiSnipsSnippetsDir nor g:UltiSnipsSnippetDirectories.
Keep the runtimepath+= configuration.
Create the following directory: ~/.vim/my-snippets/UltiSnips.
Place the personal snippets under this new directory.
Reasoning:
By default UltiSnips searches for all UltiSnips directories under your runtime paths, so no configuration is required if this name is used.
Although the runtime setting is required for personal snippets, this configuration is automatically maintained if a plugin manager is used.
The last point allows the installation of vim plugins that contain snippets. For example, this plugin contains various snippets for both snipMate and UltiSnips, including C++.
If you are using the sirver/ultisnips plugin (UltiSnips) the correct way to do this is simply run the :UltiSnipsEdit command which opens up a custom snippets file for the current language / filetype.
I had so much grief with this. Here is an answer for future reference for those of you who do not want to suffer headaches.
I have a shared .vimrc served on a samba share. Both Windows gViM and ViM use this file.
Relevant Part of .vimrc for Windows
I have a samba share mounted under L:. Note that I actually had to use POSIX for the path, not Windows backslashes \ despite being a path for Windows.
if has('win32') || has('win64') "If gVim under Windows"
let g:UltiSnipsSnippetDirectories=["L:/.vim/custom_snippets"]
endif
Relevant Part of .vimrc for Unix
My terminal opens xterm-256color for more colors, but you could exchange that with xterm. Here the path can expand ~ correctly, since this is the real home directory where my ``.vim` lives.
if $TERM == "xterm-256color"
let g:UltiSnipsSnippetDirectories=["~/.vim/custom_snippets"]
endif
Finishing Touches to Load custom_snippets
You don't need any! The following changes are NOT necessary:
"let g:UltiSnipsSnippetDirectories=["custom_snippets"]
"let g:UltiSnipsSnippetsDir="~/.vim/snippets_custom/"
However, Putty does not pass the tab key or control key properly it seems, despite all paths working fine. I tested the paths with :UltiSnipsEdit while in a file type environment set ft=tex and it took me to ~/.vim/snippets_custom/tex.snippets as it should (both in gvim on Windows and from my unix console).
Perhaps Useful for enabling in Putty
Patch: Creating a ctrl+tab keybinding in PuTTY
How to solve the collision of TAB key mapping of `UltiSnips` plugin in the Vim
https://unix.stackexchange.com/questions/53581/sending-function-keys-f1-f12-over-ssh

VIM: Overwriting system vimrc

I work on multiple MAC OS X systems, which do not save changes after log out. As you know VIM is on every new mac, just type in vim in the terminal. I always bring my vimrc file with me, and the problem is that every single time I start vim i have to load it with :so command.
I want to use the fact that vim is available on every unix, but I also want to take advantage of the nifty integration with the terminal for doing quick tests, I just switch back to the terminal, but for that I need to close vim. When I reopen it, I again have to load the vimrc. And I am a newb, I don't even have plugins yet...
I want to keep the integration with the terminal and only develop a super quick way of introducing my changes to vim. Think of the conditions as if though you are sitting on newly installed operating system.
Thanks !
Not directly to your question, but you can always invoke vim with -u, which will let you specify your vimrc file instead of launching vim and then running :so.
The default location for your .vimrc file is ~/.vimrc (on the mac, ~ is /Users/_you_, eg /Users/matt). If you can write your file there, it will be loaded when vim starts up every time.
The system vimrc file on the mac is at /usr/share/vim/vimrc, but it sounds like these systems are not under your control, so you won't be able to write that file. Have a look at: How can I override ~/.vim and ~/.vimrc paths (but no others) in vim?, which uses the -u option to change the path vim looks for plugins under. But, in all cases, you must either be able to write your .vimrc into your home directory (which it sounds like your system does not permit) or specify the path to it at runtime (as with the command-line option I mention above, or with the normal command :so which you're currently using).
Depending on the kind of testing you need to do, you can always run shell commands within vim, by using the ! in normal mode. For instance, I frequently make changes to a python file in a buffer, and then (in normal mode) run !nosetests within vim—that writes terminal output into a temporary buffer at the bottom, and doesn't require me to leave or suspend vim. I can review the output, and any key-press takes me back to my buffer.
I don't have any experience with Mac-Os terminal. However I think if you could cp your .vimrc file to your home directory. everytime you start vim, vim will load the .vimrc file from your home directory.
If you want to swtich back to terminal from vim to do some testing/execute some commands and back to vim. you could consider to:
open multiple terminal windows
try something like screen or tmux. personaly I am using tmux, and it's very nice.
try Conque Shell plugin: http://www.vim.org/scripts/script.php?script_id=2771 I have this plugin installed too.
type Ctrl-z in vim to back to terminal
If you want to sync your .vimrc on different machines, you could put your .vimrc file in
a scm repository like gitHub, bitbucket... (I perfer this option, since you could have different branches for different settings)
dropbox
I hope this helps.
How is it possible that your changes are not saved after you log out? What would be the point of such a machine? An internet kiosk in an airport? Do you log as a user without a "home" directory?
If you have a "home" directory, just create a blank ~/.vimrc and put your settings there.
If you don't have a "home" directory but you are able to write somewhere else, create a blank vimrc file where you can, write your settings there and learn this command by heart:
$ vim -u /path/to/your/vimrc
If you don't have a "home" directory and you are really sure that you can't save anything on these machines, put your settings in a file somewhere online, preferably a place under your control, and learn this command by heart:
$ vim -u http://domain.name/yourvimrc
If you are lucky, the command you use will be remembered by your shell for you and it will be easy to issue it again without much typing.
For running your tests, you can either:
Hit <C-z> to suspend Vim. You are back at the prompt from where you started Vim and you can do your thing. Type $ fg to go back to Vim.
Type :sh to launch a new shell from the current directory. To go back to Vim, type $ exit.

Any default settings possible for VIM editor?

I use ":set ai sw=4" and ":set number" (and some other cmd) commands each time when I open any file in VIM.
For every new instance of VIM I have to enter set of commands. Is there any way where I can put these commands and VIM will execute it every time while opening any file like default settings.
Just paste those lines on the file ~/.vimrc and they'll be executed everytime VIM is openned. If it does not exist, create one.
Two recommendations:
Use a plugin that will auto-load .vimrc when you save it. Being confortable with editting .vimrc is very important. Building it up with your preferences and key-mappings as you learn VIM is one of the reasons it is so powerful.
Google now for some ready .vimrc setups as they override some not-so-optimal VIM configurations. For example, you probably won't want that annoying bell beep, among many other things.
You need to create a .vimrc file.
You should create it in your home directory.

How to navigate in large project in VIM

How do you manage big projects (hundreds of files) using only VIM?
I personally start having problems in any larger than small project.
is there any way to quickly 'go to file', preferably with name completition?
same for 'go to class definition', when it is in another file
I kinda know all the VIM basics, so I don't have problem using it for writing scripts or quick editing some source code. But it gets really messy for me when I have to navigate between files.
VIM has excellent support for tags. Once you have created a tags file for your project, you can jump to a definition or declaration of a method, class, etc., including jumping across files, all inside the same editing session.
Try
:help tags
To generate a tags file for C/C++, go to your shell prompt (I'm assuming your system is *nix/Cygwin) and type
info ctags
or
ctags --help
I like simple solutions, my favorite way to navigate at the moment is:
Add to ~/.vimrc.local
set path=$PWD/**
Then type this in the editor to find the file anywhere in the current working directory (pwd)
:find user_spec.rb
You can use tab-completion on the filenames to find multiple choices as well, making this TextMate convert very happy.
I use a combination of NERDTree (directory sidebar), FuzzyFinder Textmate (go-to-file like TextMate's CMD+T), and Sessions (:h sessions) to help me deal with large projects.
I would suggest using some sessions helper plugin. I would mention what I use, but I'm not satisfied with it yet. Just Google "vim sessions".
One thing to note with getting FuzzyFinder Textmate to work is that it depends on an old version the FuzzyFinder plugin, specifically v2.16. Anything higher and you'll get errors. But it's definitely worth the trouble. While it doesn't have name completion, its search is smart so if I search for fro/time/actionsphp it will pull up the file apps/(fro)ntend/modules/(time)_tracking/actions/(actions).class.(php) (parenthesis denote what it's matching). It makes it very easy to pick out files that are only unique by their folder name.
As well as the invaluable ctags and the various associated commands. I also couldn't live without the project plugin, which allows you to have the files of interest associated with a project in a separate pane. I can't remember how much of my setup is customised, but if I want to open a source file called Debug.c, I hit:
<F12> " Opens the project pane
/De " Searches for "De", which is likely to be enough to find Debug.c or possibly Debug.h
<ENTER> " Opens the selected file and closes the project pane
I often then do:
:vsp " Vertically split the window
<F12> " Reopen project pane
# " Search back to find previous entry with the same name (to find
Debug.h if I was on Debug.c: my headers are in Headers/ and
my source is in Source/, so the headers come earlier in the list
than the source). I use * to go the other way, obviously.
<ENTER> " Open the header file in the other window and close the project window.
With this relatively short sequence, I can open any file and it's header in a vertical split. Since the project plugin window is just a text file, completion is achieved by using Vim's searching capability.
Starting in Vim 7.3, the :find command has tab-completion of filenames.
So if you set your 'path' option to contain your entire project (probably using the ** wildcard to allow recursively searching subdirectories), then you can use the :find, :sfind, :tabfind, etc. commands with completion to get to any file in your project. This also allows jumping to files directly with gf and friends if the file name is in your text, for example in an include directive.
With this method, no external tools or plugins are needed for navigating to specific files. Although, it may admittedly not be as fast or easy to use, and doesn't address the need for jumping to definitions. For definitions, I use ctags as other answers suggest.
If you are using ctags as other posters have recommended, then make sure you look at the taglist plugin.
Make sure you take the time to read the docs and learn the key bindings. Here are a few to get you started (from the TList window):
o - open file in new window
t - open file in new tab
[[ or backspace - previous file in list
]] or tab - next file in list
Exuberant ctags.
Use Ctrl-] to jump to the tag under the cursor.
Opening vim from root of your source file and extending path option to include all sub-directories therein.
For example set path+=/usr/include/c++/** for C++ headers and set path+=** for your source directory.
This ,then, opens a plethora of following possibilities.
1) Opening file by name or parts of it
:find file_name
You can use auto-completion and wildcard expansion with :find reliably. You type the name, it will locate the name. This works language agnostic.I am sure you will like it.
2) Navigating to files under cusror:
if you want to go a file path like #include "project/path/classA.h.
gf or gF - go to file under cursor.
Ctrl-6 - to come back to last cursor position after gf or gF
3) API lookup and navigating to the API location
[i or [I can be used to look up your function signature for word under cursor without leaving your workspace. [<Tab> to actually go to declaration. Use Ctrl-6 to come back to last location.
Without extending path, you can start navigating files by :Ex command and navigate and open your file. I prefer NerdTree over this though.
I use FindFile. If you open vim at the root of your project and run :FC . the plugin will cache all the filenames beneath your cwd. You can then do :FF to open a completion menu and type the name of the file you want (or rather, the first few letters).
Although I'm kinda hoping someone will point out a better solution so I can learn something, NERDTree has been good to me for getting to specific files with name completion as long as I have the tree expanded. The command when I need to get to a file is something like:
,d/foo.pyo (where foo.py is a file name)
,d to open the tree, / to enter search mode, the name (or partial name, or regex, or whatever) of the file, and then o to open.
Of course you may have to hit 'n' a few times if you didn't type enough of the filename or there are duplicates.
I admit it feels like a bit of a hack using NERDTree like this although it has gotten so far into my muscle memory by now that I don't even think about it.
Of course I use ctags too but those are only useful when you have a function near the cursor and need to get to its definition in another file or something. A lot of times I say "OK, I need to work on feature x now" and need to navigate to another file without any references nearby that ctags would really help with.
I'm using two plugins of mine:
searchInRuntime that completes filenames on command line. It is somehow similar to fuzzyfinder and lookupfile,
lh-tags which is completely experimental and undocumented. It offers two features: automatic and quick update of the tagfile on file save(ing?), and a tag selector plugged to <c-w><m-down> by default. You may want to check the renowned taglist plugin instead.
Both require my viml library lh-vim-lib.
Try SourceCodeObedinece. This one I developed to handle C++ 1Gb source files project.
I use it in pair with 0scan.
These two plugins are wrappers around the most popular Vim browsing tools: ctags and cscope.

Resources