I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins
Related
I kind of new to vim, and when I usually edit things in vim I go something like:
vim .
or
vim /path/to/some/project
And then Ctrl-P to the file I need to edit. The problem with this approach is that plugins like NERDTree or FZF understand that the working directory is the one I started vim from not the directory I gave it as the parameter. Example:
/some/path$ vim /other/path
vim would understand that /some/path is the working directory not /other/path. Because of this I would have to manually :cd :%p:%h each time I start vim. Is there a way to make vim automatically :cd to the first opened directory?
I never thougth about that, but while reading your question i thougth, that could be nice ... or totaly horrible. Anyway interesting problem and I tried it for like 30 seconds and already hate it. The reason is, that your projectfolder almost never directly contains code files which you want to edit.
It should work like this:
autocmd VimEnter * silent! cd %:p:h
I want to install this closetag.vim script:
http://vim.sourceforge.net/scripts/script.php?script_id=13
It says
place this file in your standard vim scripts directory and source it
while editing the file you wish to close tags in.
And this is shown as an example:
:let g:closetag_html_style=1
:source ~/.vim/scripts/closetag.vim
1) What is my standard vim scripts directory on W7?
I have neither .vim nor scripts folder on my system. And if I have\am expected to create one (or ones) where should it (they) be placed? %ProgramFiles%\Vim\vim80 or %ProgramFiles%\Vim\vimfiles or maybe somewhere else?
1.1) Also, this might be a silly thing to ask about, but why do I keep seeing that tilde in path almost every time I read about Vim. Does it mean that Vim is used primarily by Mac/Linux people? Why is that?
2) What does it mean to source the script? Run a command like this let g:closetag_html_style=1 in command mode in Vim?
Btw what does style=1 mean here?
And if I want it to work by default for all html\xhtml\xml files, what do I do? Put this command to _vimrc file?
Thank you so much!
You could find the answers to all your questions just by reading the plugin description carefully. Unfortunately, it is both poorly written and factually incorrect.
What is my standard vim scripts directory on W7?
On Windows, you are supposed to put custom and third-party scripts in various places under:
C:\Users\username\vimfiles\
But that's not what the author means by "standard vim scripts directory". What he is referring to is this:
C:\Users\username\vimfiles\scripts\
which is not standard at all.
Also, this might be a silly thing to ask about, but why do I keep seeing that tilde in path almost every time I read about Vim. Does it mean that Vim is used primarily by Mac/Linux people? Why is that?
Yes, Vim is primarily used by UNIX-like systems users. Because of history.
What does it mean to source the script? Run a command like this let g:closetag_html_style=1 in command mode in Vim?
No. Read your question again.
Btw what does style=1 mean here?
Nothing.
But :let g:closetag_html_style=1 means "set the g:closetag_html_style option to true".
And if I want it to work by default for all html\xhtml\xml files, what do I do? Put this command to _vimrc file?
No. This is explained on the plugin's page:
For greater convenience, load this script in an autocommand:
:au Filetype html,xml,xsl source ~/.vim/scripts/closetag.vim
Which is wrong on many levels.
Here is what you actually have to do to use that script:
Save the closetag.vim script to the following location:
C:\Users\username\vimfiles\scripts\closetag.vim
Create vimfiles\ and/or vimfiles\scripts\ if they don't exist.
Add the lines below to C:\Users\username\_vimrc:
augroup closetag
autocmd!
autocmd Filetype html,xhtml,xml,xsl runtime scripts/closetag.vim
augroup END
let g:closetag_html_style = 1
Reference:
:help startup
:help :source
:help :runtime
:help :let
:help autocommand
I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.
Imagine I'm editing file, and I want to show the list of the files inside the folder who belongs the file I'm editing, to edit one of them.
How can I do that? Is there any way using FuzzyFinder?
Did you even read FuzzyFinder's documentation (:help fuzzyfinder)? Quickly opening nearby files is one of that plugin's main features.
Without installing anything, you can do:
:Ex[plore]
to open the netrw file tree. See :help netrw.
You can also do:
:e <Tab>
Add these lines to your ~/.vimrc to make command line completion even better:
set wildmenu
set wildmode=list:full
and read :help wildmenu and :help commandline-completion.
set autochdir is a useful option to add to your ~/.vimrc, by the way.
change vim current directory to current file's:
:cd %:h
then
FuzzyFinder can do what you want (pick and edit). (:FufFile) I have mapping :
nmap <Leader>ff :FufFile<cr>
NERDTree can do that as well.
Depends on what you mean by showing the file.
To include the list of files in the currently edited files, you can do something like:
:read !ls /path/to/file
(it can be shortened to :cd %:h | read !ls if you don't mind if vim changes it's current directory...)
If you want to pick another file to edit, I'd suggest to take a look at NerdTree plugin (here is a little intro). Or you can simply issue:
:cd %:h | e .
I have multiple plugins in Vim and some of them modify the default behavior of Vim. For example I use Vimacs plugin, which makes Vim behave like emacs in the insert mode alone. Sometime I want to turn off the Vimacs plugin without moving the vimacs.vim out of the plugins directory. Is there a way to do it?
You can do this if you use a plugin manager like Vundle or Pathogen, which will keep the plugin in its own directory underneath the ~/.vim/bundle/ directory.
In that case, just find out the runtimepath of the vimacs plugin with the following command:
set runtimepath?
Let's say it's ~/.vim/bundle/vimacs.
Then, put this command in your .vimrc:
set runtimepath-=~/.vim/bundle/vimacs
To load vimacs, just comment that line out and relaunch Vim (or source your .vimrc).
See which variable vimacs check on start. On the begin of the script file find something Like if exists('g:vimacs_is_loaded").... Then set this variable in your .vimrc or while start vim with vim --cmd "let g:vimacs_is_loaded = 1".
In case you are using pathogen, this post gives a better answer, in my opinion. Since I have frequent need to disable snippets when using latex, also added this in my ~/.config/ranger/rc.conf:
map bs shell vim --cmd "let g:pathogen_blacklist = [ 'ultisnips', 'vim-snipmate' ]" %f
This way, whenever I want to open a file with snippets disabled, it is easy.