Open CtrlP on VimEnter Only If Opening a Directory - vim

I'd like to open CtrlP if I am opening a directory with vim, but not a file. I like to have it automatically open in I just open a directory for convenience. However, it is slightly inconvenient if I know exactly which file I want to open because of the added loading time.
Currently I just have this in my .vimrc:
autocmd vimenter * CtrlP
Thanks in advance for any responses!

You can write a function to test the args to see if a single directory was passed in and if it was, execute CtrlP. Here is a very rudimentary solution:
function! MaybeCtrlP()
if argc() == 1 && isdirectory(argv()[0])
" Uncomment this to remove the Netrw buffer (optional)
" execute "bdelete"
execute "CtrlP"
endif
endfunction
autocmd VimEnter * :call MaybeCtrlP()

In .bash_profile, create an alias:
alias vimCtrlP="vim +CtrlP"
Then every time you need this, use vimCtrlP as you would issue vim in shell, followed by the directory.

Related

Autoload netrw when starting vim

I want netrw to autoload when I launch vim using the terminal. Completely new to linux/ubuntu. Is there any way of doing that?
Adding the following to your .vimrc (Vim's configuration file, located in the root of your home directory) will cause Vim to automatically load Netrw after starting up.
" Open Netrw after Vim starts up
augroup InitNetrw
autocmd!
autocmd VimEnter * :silent! Explore
augroup END
A problem with the preceding approach, as implemented, is that Netrw will also load when you use Vim with an argument to open a specific file. A workaround is to use the following modification, based on the suggested approach in Netrw's documentation (:help netrw-activate).
" Checks if there is a file open after Vim starts up,
" and if not, open the current working directory in Netrw.
augroup InitNetrw
autocmd!
autocmd VimEnter * if expand("%") == "" | edit . | endif
augroup END
The following pages have more details on autocommands and the .vimrc configuration file.
https://learnvimscriptthehardway.stevelosh.com/chapters/12.html
https://learnvimscriptthehardway.stevelosh.com/chapters/14.html
https://learnvimscriptthehardway.stevelosh.com/chapters/07.html
And the following code block in your vimrc:
set autochdir
let g:netrw_browse_split=4
augroup InitNetrw
autocmd!
autocmd VimEnter * if argc() == 0 | Lexplore! | endif
augroupend
Kind of does what #dannyadam suggested. But opens the netrw pane as a side bar on the right. If you want to be on the right use Lexplore without the bang(!).

Golang: running `go build` on save using vim (macvim) and vim-go

I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.
Within my sublime setup, it runs go build anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build provides.
I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.
In short I want to run :GoBuild upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?
yes, use vim autocommand to run :GoBuild:
You can specify commands to be executed automatically when reading or
writing a file, when entering or leaving a buffer or window, and when
exiting Vim. The usual place to put autocommands is in your .vimrc or
.exrc file.
Run the following command:
:autocmd BufWritePre *.go :GoBuild
Now each time you save your Go file with :w it will run :GoBuild too.
The event type is BufWritePre, which means the event will be checked just before you write *.go file.
BufWritePre starting to write the whole buffer to a file
BufWritePost after writing the whole buffer to a file
and:
When your .vimrc file is sourced twice, the autocommands will appear
twice. To avoid this, put this command in your .vimrc file, before
defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a
variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
like this (put this at the end of your vim startup file):
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: autocmd BufWritePost *.go :GoBuild
:endif
ref:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
Create ~/.vim/after/ftplugin/go.vim with:
autocmd BufWritePre *.go :GoBuild

How can I see the file or directory named passed to Vim from the command line?

I'd like to set up my .vimrc so that NERDTree is open whenever vim (or MacVim) is called from the command line with a directory argument, but closed when vim is called with a file argument. I looked through the list of vim's built-in variables but couldn't find anything that looked promising.
Looked up arglist per Alligator's advice. Ended up with the following:
if argc() > 0 && isdirectory(argv(0))
autocmd VimEnter * silent NERDTree
endif

How can I make NERDTree to open on the same drive that the file that I'm editing?

NERDTree shows in viewport disk c: regardless from which disk do I open the file.
When I use gvim in windows I open files using:
gvim.exe --remote-tab-silent [FILE]
I'm loading NERDTree with this line in _vimrc:
au VimEnter * NERDTree
Can NERDTree automaticaly change drive to correct drive somehow?
Actually, my last answer does not work because once the NERDTree have been opened, it does not open again in the new buffer dir. It must work similarly to NERDTreeFind but it does not have a Toggle feature.
I made a function and mapped it to my key and now it works perfectly even opening the Ruby project if you have the vim-rails plugin.
Add this to your vimrc:
function! NTFinderP()
"" Check if NERDTree is open
if exists("t:NERDTreeBufName")
let s:ntree = bufwinnr(t:NERDTreeBufName)
else
let s:ntree = -1
endif
if (s:ntree != -1)
"" If NERDTree is open, close it.
:NERDTreeClose
else
"" Try to open a :Rtree for the rails project
if exists(":Rtree")
"" Open Rtree (using rails plugin, it opens in project dir)
:Rtree
else
"" Open NERDTree in the file path
:NERDTreeFind
endif
endif
endfunction
"" Toggles NERDTree
map <silent> <F1> :call NTFinderP()<CR>
It should work now.
Previous answer below:
You could map the key you use to open
NERDTree like this(in .vimrc):
map <silent> <F1> :NERDTreeToggle %:p:h<CR>
This maps my F1 key to
toggle(open/close) NERDTree using the
path of the currently active buffer.
If no buffer is open, it opens in the
currently launched Macvim directory.
NERDTree provides several Ex commands to manipulate its buffer (see
:help NERDTreeGlobalCommands). Among them there is the :NERDTreeFind
command which behaves the same way as the :NERDTree command except it opens
the NERDTree buffer in the directory containing currently opened file.
So, in order to achieve the desired effect described in the question, you can
simply change the auto-command to read
:autocmd VimEnter * NERDTreeFind
I use mapping for NERDTree and in this way when I open it always opens in current dir
" NERDTree mappings
nnoremap <silent> <F9> :NERDTreeToggle <cr>
inoremap <silent> <F9> <Esc>:NERDTreeToggle <cr>
But if you open a file like gvim ~/other/dir/file NERDTree will open current dir from where gvim was called. So this is not a real solution to your problem.
Perhaps if you cd in working dir before calling gvim will solve your problem. In this case even your au VimEnter * NERDTree in _vimrc must work as you espect .
About changing directory and setting working dir set autochdir read here
Add
au VimEnter,BufWinEnter * NERDTreeFind
to your .vimrc.
VimEnter part makes it work on load.
BufWinEnter makes it happen you open a new file.
* tells it to do this with all files
NERDTreeFind is the command to run
srcs:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html

How to add NERDTree to your .vimrc

How do I add NERDTree to my .vimrc?
Okay, the previous version was a bit terse, but the answer you're looking for is to add the line below into your ~/.vimrc file. It tells Vim that you want to setup a command to run when Vim starts, but since it depends on various plugins to be loaded, you don't want to run it until all initialization is finished:
autocmd VimEnter * NERDTree
If, however, you're annoyed by the fact that the cursor always starts in the NERDTree window, you can add a second autocommand that will move the cursor into the main window:
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
I like to see NERDTree only when I start vim without file arguments, so I added this to my .vimrc:
autocmd VimEnter * if !argc() | NERDTree | endif
Are you on a Windows or unix-y system?
If you're on a unix-y system you put plugins in ~/.vim/plugin. Here's what my plugin directory looks like:
$ ls ~/.vim/plugin
NERD_tree.vim scratch.vim scratchfind.vim
After that it starts working right away. Try running vim like this:
$ vim .
It should open the current directory in the NERD tree view.
If you're on Windows you put plugins here: C:\Program Files\Vim\vim70\plugin
To get NERDTree to load automatically when you start up vim, run it like this from the command line:
$ vim -c "NERDTree" some_file.txt
You can set an alias for this in your .bashrc:
alias vimt='vim -c "NERDTree" $1'
Now whenever you run vimt (instead of vim) you'll also open up NERDTree on the left side of the window.
You could also add a shortcut key to start NERDTree in your .vimrc this way:
function OpenNERDTree()
execute ":NERDTree"
endfunction
command -nargs=0 OpenNERDTree :call OpenNERDTree()
nmap <ESC>t :OpenNERDTree<CR>
Now when you hit Esc then t it will pop open NERDTree.
Per the NERDTree instructions you can just use pathogen.vim. Install it with:
mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
Add this to your .vimrc:
execute pathogen#infect()
then install NERDTree:
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
And if you want to open a NERDTree automatically when Vim starts up, add the following to your .vimrc:
autocmd vimenter * NERDTree
The answers here have a minor problem.
If you call vim --noplugin or use a script that uses --noplugin mode such as vimpager, it will cause this error:
Error detected while processing VimEnter Auto commands for "*":
E492: Not an editor command: NERDTree
To avoid this, put the command in ~/.vim/after/plugin/NERD_tree.vim instead:
autocmd VimEnter * NERDTree
And it might also be a good idea to test that NERDtree is available as well, i.e.:
if exists("loaded_nerd_tree")
autocmd VimEnter * NERDTree
endif
" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
I like to open NERDTree on startup, but with two requirements:
Open NERDTree only when there are no arguments (file arguments)
Display only the NERDTree window (I don't want to display the main window)
I use this command:
autocmd VimEnter * if !argc() | NERDTree | wincmd p | q | endif
Update (9 Jan 2022)
I've found a more performant way to meet the two requirements I've specified above.
Remove the autocmd, which I've mentioned above, from the .vimrc file. Instead, create an alias like this:
vim() {
if [ $# -eq 0 ]; then
/usr/bin/vim ./
else
/usr/bin/vim "$#"
fi
}

Resources