Any default settings possible for VIM editor? - vim

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.

Related

Opening a file via Vim on GIT

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.

Why does Vim cursor jump to selected text when bypassing visual mode?

Vim automatic visual mode can be annoying. It's switching into visual mode when ever you left click and select a text in it. Fortunately there is the possibility to bypass this behaviour by holding shift-key while selecting text in the terminal. This bypass is useful, quickly to use at hand without having to configure vim first.
However I recently noticed that, when the terminal detects something as a link (for instance /var/www/example.com/ directory in apache vhost configs), and I try to select it using mouse+shift-key combination, it doesn't let me copy the selected text but jumps to the next similar line in the text while enabling auto visual mode again.
Why does this happen and how can I bypass that?
PS: I know about :set mouse-=a in vimrc but as I am working on different servers I don't want to have to edit vimrc each time I am on a new server.
The only text-selection vim has is visual mode. So you tell vim with mouse=a that it should use the mouse to select text. What do you expect?
You could always copy the text without your mouse at all ("+yy if you have clipboard support).
I could not reproduce your behavior with the links. Is this also happening without plugins?
But anyway, if you don't want visual mode on your mouse, you have to modify the mouse setting and stop telling vim to use visual mode on your mouse.
If there's a specific setting that you want on your main machine, but not on other servers etc then you can use an if statement in your .vimrc to specify which system you want the setting to be active on. For instance:
"My Linux machine
if readf('/etc/machine-id') == ['your-machine-id']
set mouse=a
endif
"Only on Macs
if system('uname') == "Darwin\n"
set mouse=a
endif
Someone is setting mouse=a. You can find the guilty one by executing :verbose set mouse.
Then you either have to change that or you have to create a .vimrc for your user. As far as I understand that might not be easy in your situation.
Instead of set mouse-=a you could also use set mouse= to disable all mouse stuff. This is what I prefer and it saves two keystrokes :-).
This is how I handle this problem. It might or might not be doable for you.
I have one file called .rks, that I scp to every server I have to login.
The first command after login is always
. ./.rks
This sets up my shell environment (prompt, aliases, vi editing mode etc) and creates a file
called ~/.vimrc.rks (if it doesn't exist) containing my basic Vim setup
(e.g. set mouse=). Finally it exports the variable VIMINIT:
export VIMINIT="source $HOME/.vimrc.rks"
Now Vim sources ~/.vimrc.rks on startup and I get my setup.
This way I
only have to transfer one file to all machines. Other files (like
.vimrc.rks) are created when sourcing that file.
have my personal setup without changing any default files. Others might also log in with the same user name.
I just learned, that I could automate the transfer of my setup file using ssh LocalCommand configuration. See this answer on serverfault.

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.

Changing NERDTree ~/.vimrc options on the fly without restarting VIM

I would like to change my sort order dynamically. At times, I want my sources to be listed alphabetical, at times I want only one extension to be shown.
I can currently get it to work by editing NERDTreeSortOrder in my ~/.vimrc.
But how do I do this without re-launching VIM?
I tried just executing ":let NERDTreeSortOrder = ['new sort order']" but that doesn't really change anything. i add it to my vimrc, quit VIM and restart and it works.
By the way, if someone can tell me how to make NerdTree show only a specific extension, that would work for me too. I know I can make NERDTree not show a particular file type by adding it to ignore, but I want the other way around.
Any ideas?
You'll need to refresh the directory (after entering let NERDTreeSortOrder=[...]).
While in the NERDTree buffer, either :
r to limit the refresh to the selected directory
R to refresh from the start of the root node.
As for whitelisting specific file extensions, have at look at this question.
Have your tried source-ing your .vimrc from within vim?
:source $MYVIMRC or :source ~/.vimrc
:source can be shortened to :so, e.g. :so ~/.vimrc

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.

Resources