When calling CtrlP for the first time it cause quite a delay to build the cache. Is there a way to run it in the background so I can continue doing stuff in vim?
If you need to speed up indexing of CtrlP, you can use ag to grep files, exclude some folders and files from indexing, and set cache location.
To do that, put these lines in your .vimrc:
let g:ctrlp_cache_dir = $HOME . '/.cache/ctrlp'
let g:ctrlp_user_command = 'ag %s -i --nocolor --nogroup --hidden
\ --ignore .git
\ --ignore .svn
\ --ignore .hg
\ --ignore .DS_Store
\ --ignore "**/*.pyc"
\ -g ""'
You cannot do that, in fact, vim isn't multi-threaded.
You can look over for NeoVim which is multi-threaded.
Alternativly, you could use the Unite plugin and the file_rec/async command :
Note: with large projects this may cause some performance problems.
Normally it is recommended to use |unite-source-file_rec/async|
source, which requires |vimproc|.
Related
I have fzf setup in vim with barely any customisation:
" fzf and ripgrep settings
set rtp+=/usr/local/opt/fzf
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-i': 'split',
\ 'ctrl-s': 'vsplit'
\ }
In my .bash_profile, I set the environment variable FZF_DEFAULT_COMMAND to show hidden, but ignore node_modules and .git:
export FZF_DEFAULT_COMMAND='rg --files --follow --no-ignore-vcs --hidden -g "!{node_modules/*,.git/*}"'
However, when I use :Files function in vim, it still searches in the folders i want ignored.
Have you tried :GFiles command?
It is one of the best FZF command that is excluding anything that is in .gitignore.
Personally I like to use it as default.
:help fzf-vim-commands
You can ignore the node_modules (and any other folder you want to ignore) by using a custom FZF_DEFAULT_COMMAND. I use something like this:
(place it in your vimrc)
let $FZF_DEFAULT_COMMAND='find . \( -name node_modules -o -name .git \) -prune -o -print'
Silly me. I forgot to source ~/.bash_profile
I suppose 'junegunn/fzf' are we tolking about.
I have success on make fzf and rg work for my need. (I'm a developer)
this is my vim 8.2 configuration:
command! -bang -nargs=* Rg
\ call fzf#vim#grep(
\ "rg -g '!design/' -g '!dist/' -g '!pnpm-lock.yaml' -g '!.git' -g '!node_modules' --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1,
\ fzf#vim#with_preview({'options': '--exact --delimiter : --nth 4..'}), <bang>0)
In this configuration inside last row --exact remove all fuzzy search: how developer I like exatc metch more
to not include folder and file I do not like I use -g option:
rg -g '!design/' -g '!dist/' -g '!pnpm-lock.yaml' -g '!.git' -g '!node_modules'
The ! is to exclude
I'm excluding design, dist folder from my search base path (current directory)
and exclude il file pnpm-lock.yaml anywhare
and excluse .git and node_modules anywhare (file or folder)
hope can help,
regards,
Leonardo
You can use fzf.vim
It allows you to use default $FZF_DEFAULT_COMMAND if defined.
If you are using vim-plug for plugin management, to install fzf.vim:
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
In case it results in:
command failed: rg --files ...
It is because it results from an rg error or you do not have it installed, for that you just have to execute
sudo apt install ripgrep -y
In case you use a script for the installation you can use:
which rg >/dev/null 2>&1 ||
apt install ripgrep -y
Ctrlp currently searches/indexes my entire home directory, how can I tell it to only index ~/code?
You have several options to configure where CtrlP will search for files. You will have to add these to your .vimrc file.
For example, in my case I use ag to perform the search:
let g:ctrlp_user_command = 'ag %s -l --hidden --nocolor -g "" --ignore .git'
Normally CtrlP will look into the directory of the current project. If you always want to search in ~/code like you ask, you could add this custom command (in OSX):
let g:ctrlp_user_command = 'find ~/code -type f ! -path "*.git*"'
You could even add extra directories, like:
let g:ctrlp_user_command = 'find ~/code ~/foo ~/bar -type f ! -path "*.git*"'
However, I advise to not fix the directory where CtrlP will look and leave it to search for the project of the current file (see let g:ctrlp_working_path_mode)
CtrlP searches/indexes your entire home directory because that's what you ask it to do.
You are probably starting Vim in your $HOME and doing :CtrlP from there. Since the working directory is $HOME you shouldn't be surprised that CtrlP scans all of it.
The solution to this non-issue is simply to use your shell and Vim as they are supposed to be used:
$ cd ~/code
$ vim
When using ag on the command line, like so:
$> ag . --ignore="*node_modules/*/node_modules" -l --nocolor -f -U -g ""
I am able to avoid searching through any node_modules directories more than one level deep in my node services, which is the desired behavior.
However, when I use the following in my vimrc, the node_modules directories more than one level deep are not ignored:
" Use The Silver Searcher https://github.com/ggreer/the_silver_searcher
if executable('ag')
" Use Ag over Grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s --ignore="*node_modules/*/node_modules" -l --nocolor -f -U -g ""'
endif
How can I set up ag and ctrlp to correctly ignore those directories?
Not sure if I need to use a different syntax (like regex) or some other gotcha when transplanting to vimrc.
The reason I'm not putting this in the wildignore is that node_modules are ignored in my .gitignore, so I'm using the -U option to ignore any vcs files (thereby allowing ag to search node_modules) -- but this option also seems to bypass the wildignore.
Like you I do use both tools, but the way I ignore folders is different
For Ag I use the .agignore file, it has the same sintax as .gitignore and just like it, it can go in your home folder or project folder.
Not sure if that will solve your problem with Ctrlp, in any case it is quite fast already for me so I use the normal ignore variable like this:
let g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/](doc|tmp|node_modules)',
\ 'file': '\v\.(exe|so|dll)$',
\ }
I'm trying to add Go language syntax highlighting to VIM on ubuntu with resources and direction supplied here http://go-lang.cat-v.org/text-editors/vim/.
Go comes with a go.vim file that contains syntax settings for VIM and the above page offers the following instructions
Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/ and put the following in ~/.vim/ftdetect/go.vim:
au BufRead,BufNewFile *.go set filetype=go
This is more or less the same vein of procedure for customizing vim syntax I've seen elsewhere
(Vim 7.3 on Ubuntu 12.10 doesn't have 'ftplugin' directory anywhere and https://github.com/jnwhiteh/vim-golang/blob/master/readme.txt)
So I think I'm doing the right thing when I create directories:
~/.vim
~/.vim/syntax
~/.vim/ftdetect
and follow the above instructions by adding
go.vim to ~/.vim/syntax/
and creating a file, go.vim, in ~/.vim/ftdetect/ which contains
au BufRead,BufNewFile *.go set filetype=go
Yet syntax highlighting does not seem to occur. Is there something I need to do to force VIM to look at these new settings files?
UPDATE:
Go 1.4 Release Notes
Miscellany
The standard repository's top-level misc directory used to contain Go
support for editors and IDEs: plugins, initialization
scripts and so on. Maintaining these was becoming time-consuming and
needed external help because many of the editors listed were not used
by members of the core team. It also required us to make decisions
about which plugin was best for a given editor, even for editors we do
not use.
The Go community at large is much better suited to managing this information. In Go 1.4, therefore, this support has been removed from
the repository. Instead, there is a curated, informative list of
what's available on a wiki page.
The standard Go distribution includes Go files for Vim in go/misc/vim/. This directory contains a readme.txt file which contains installation instructions.
readme.txt
Vim plugins for Go (http://golang.org)
To use all the Vim plugins, add these lines to your $HOME/.vimrc.
" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
If you want to select fewer plugins, use the instructions in the rest
of this file.
<<..SNIP..>>
On Debian, I suppose it's the same on ubuntu, you just :
sudo apt-get install vim-gocomplete gocode vim-syntax-go
vim-addon-manager install go-syntax
vim-addon-manager install gocode
you can just add these lines to your ~/.vimrc:
set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
EDIT This assumes filetype plugin indent off before these lines (i.e. beginning of .vimrc file) and may cause problems if it's not. See #peterSO's answer below for the safer version.
For the best syntax highlighting try https://github.com/fatih/vim-go
It's a new project that consolidates many vim plugins and adds a lot of features. From the readme:
Improved Syntax highlighting, such as Functions, Operators, Methods..
Auto completion support via gocode
Better gofmt on save, keeps cursor position and doesn't break your undo history
Go to symbol/declaration with godef
Automatically import packages via goimports
Compile and go build your package, install it with go install
go run quickly your current file/files
Run go test and see any errors in quickfix window
Lint your code with golint
Run your code trough go vet to catch static errors.
Advanced source analysis tool with oracle
List all source files and dependencies
Checking with errcheck for unchecked errors.
Integrated and improved snippets. Supports ultisnips or neosnippet
Share your current code to play.golang.org
on 25/Jan/2015
Please see https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins as now all editor & shell support in Go repo is removed (https://codereview.appspot.com/105470043)
I did not find instructions on turning on vim syntax highlighting for CentOS 7. Have tested the ensuing instructions to work on CentOS 7.3.1611. First, create the following directory in your home directory:
$ mkdir ~/.vim/ftdetect/
Then, create a file named, go.vim inside the above directory with the contents:
au BufRead,BufNewFile *.go set filetype=go
Download the syntax definition file for Go: vim.go. Transfer it to the right system-wide directory so that multiple users can share:
$ sudo mv -i go.vim /usr/share/vim/vim74/syntax/
For whatever reason outside of my own decision making, we got Golang installed on our dev VMs by Debian packages. This particular distribution of vim doesn't come with any of the goodies for vim as far as I was able to tell from a search around for it. Anyways, I decided to go the vundle route in order to rapidly deploy the goodies to all these dev VMs. You could probably work this method into puppet or something if you'd like, we didn't do that though. Anyways, here's what I did:
Step 1: Install vundle:
https://github.com/gmarik/vundle
Step 2: put this line in your .vimrc (It's from here, of course: https://github.com/jnwhiteh/vim-golang ), and then run vim from the command line like vim +BundleInstall +qall or from within vim with :BundleInstall
Bundle 'jnwhiteh/vim-golang'
Step 3: Save this little bash script I whipped up as govim.sh or whatever, chmod +x govim.sh, and run it like ./govim.sh
Script as follows:
#!/bin/bash
mkdir $HOME/.vim/ftdetect
mkdir $HOME/.vim/syntax
mkdir $HOME/.vim/autoload
mkdir $HOME/.vim/autoload/go
mkdir $HOME/.vim/ftplugin
mkdir $HOME/.vim/ftplugin/go
mkdir $HOME/.vim/indent
mkdir $HOME/.vim/compiler
mkdir $HOME/.vim/plugin
mkdir $HOME/.vim/plugin/godoc
ln -s $HOME/.vim/bundle/vim-golang/ftdetect/gofiletype.vim $HOME/.vim/ftdetect
ln -s $HOME/.vim/bundle/vim-golang/syntax/go.vim $HOME/.vim/syntax/
ln -s $HOME/.vim/bundle/vim-golang/autoload/go/complete.vim $HOME/.vim/autoload/go/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go.vim $HOME/.vim/ftplugin/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go/*.vim $HOME/.vim/ftplugin/go/
ln -s $HOME/.vim/bundle/vim-golang/indent/go.vim $HOME/.vim/indent/
ln -s $HOME/.vim/bundle/vim-golang/compiler/go.vim $HOME/.vim/compiler/
ln -s $HOME/.vim/bundle/vim-golang/plugin/godoc/godoc.vim $HOME/.vim/plugin/godoc/
ln -s $HOME/.vim/bundle/vim-golang/syntax/godoc.vim $HOME/.vim/syntax/
Kaching! You now have all the goodies installed, and someone correct me if I'm wrong on this but perhaps more than what comes with the official Golang distribution. I don't know about this either having not tried it yet, but I think that the runtimepath/rtp gets clobbered if you use Vundle with the other answers here anyways.
It should be easy as 1, 2, 3 :
Download the file vim.go and place in in ~/.vim/syntax under the name go.vim (create the directory syntax if you don't already have it).
If you don't already have the file ~/.vim/ftdetect/go.vim, create it (and the folder if necessary).
In .vim/ftdetect/go.vim, add the following line : au BufRead,BufNewFile *.go set filetype=go
This page says that:
Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/
and put the following in ~/.vim/ftdetect/go.vim:
au BufRead,BufNewFile *.go set filetype=go
It worked for me, only i did not find the /misc/vim/go.vim directory at first. So i copied the files from another computer that had installed go on /usr/local/go/...
This is what worked for me in MAC
Install vim-go - https://github.com/fatih/vim-go
Set the .vimrc to the following
let g:tagbar_type_go = {
\ 'ctagstype' : 'go',
\ 'kinds' : [
\ 'p:package',
\ 'i:imports:1',
\ 'c:constants',
\ 'v:variables',
\ 't:types',
\ 'n:interfaces',
\ 'w:fields',
\ 'e:embedded',
\ 'm:methods',
\ 'r:constructor',
\ 'f:functions'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 't' : 'ctype',
\ 'n' : 'ntype'
\ },
\ 'scope2kind' : {
\ 'ctype' : 't',
\ 'ntype' : 'n'
\ },
\ 'ctagsbin' : 'gotags',
\ 'ctagsargs' : '-sort -silent'
\ }
let g:go_highlight_structs = 1
let g:go_highlight_methods = 1
let g:go_highlight_functions = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
syntax on
Install gotags - e.g. brew install gotags
Generate ctags - e.g. gorags -R . > ./tags
Open vim from the new shell
Turns out the directions above were slightly ambiguous.
~/.vim/syntax/go.vim should have the same contents as ~/.vim/ftdetect/go.vim
only ~/.vim/ftdetect/go.vim must be appended with au BufRead,BufNewFile *.go set filetype=go.
If taken literally, the directions tell you to create a file ~/.vim/ftdetect/go.vim containing only
au BufRead,BufNewFile *.go set filetype=go
I suppose that's where contextual knowledge should kick in. Only I'd never done this before and had no such context. Thanks all!
I don't like how Vim clutters up my folders with backup files, so I have the following line in my .vimrc file:
set backupdir=~/.vim_backup
However, sometimes this folder doesn't exist because of new machines where I am copying my user files over.
How can I create this folder automatically if it doesn't exist, from within .vimrc? Or is there some better way to deal with this situation?
I think this is operating system independent:
if !isdirectory("/my/directory")
call mkdir("/my/directory", "p")
endif
You can put this into your .vimrc:
silent !mkdir ~/.vim_backup > /dev/null 2>&1
This will attempt to create the ~/.vim_backup each time you start vim. If it already exists, mkdir will signal an error, but you'll never see it.
You could use the mkdir() Vim function. It can create intermediate directories and set proper permissions on the directory as well:
if !isdirectory($HOME . "/.vim/backup")
call mkdir($HOME . "/.vim/backup", "p", 0700)
endif
I much prefer the one-liners already posted. However, I thought I'd share what I've been using:
function! EnsureDirExists (dir)
if !isdirectory(a:dir)
if exists("*mkdir")
call mkdir(a:dir,'p')
echo "Created directory: " . a:dir
else
echo "Please create directory: " . a:dir
endif
endif
endfunction
call EnsureDirExists($HOME . '/.vim_backup')
I wanted a solution to this that works on both Linux and on Windows (with Cygwin binaries on the path.) - my Vim config is shared between the two.
One problem is that 'mkdir' is, I think, built-in to the cmd shell on windows, so it can't be overridden by putting the Cygwin mkdir executable earlier in the path.
I solved this by invoking a new bash shell to perform the action: From there, 'mkdir' always means whatever mkdir executable is on the path:
!bash -c "mkdir -p ~/.vim-tmp"
However, this has the problem that. on Windows at least, it pops up a new window for the cmd shell that '!' invokes. Don't want that every time I start vim. Then I discovered the vim function 'system', which invokes a new cmd shell (the icon appears in the Windows taskbar) but the window is minimised. Hence:
call system("bash -c \"mkdir -p ~/.vim-tmp\"")
Ugly but works.
I have a command in my vimrc that enables me to make a directory corresponding to the buffer presently frontmost:
nmap <silent> ,md :!mkdir -p %:p:h<CR>
This can be used to create a directory any place you care to put one locally or remotely via netrw if you the permissions to do so.
This is built into Vim, so you can simply:
call mkdir($HOME . "/tmp")
You can also specify the p flag to have the command operate in the same way mkdir -p would:
call mkdir($HOME . "/tmp", "p")
For details see :help mkdir or the vim manual online.
This is what I am currently using in my .vimrc file and I think it looks quite clean and simple:
if empty(glob($HOME . '/.vim/undodir'))
call mkdir($HOME . '/.vim/undodir', 'p')
endif