2022 Developer Survey is open! Take survey.
48 votes
Accepted

What are the differences between :wq<cr> :x<cr> and ZZ when exiting vim?

From Vim's documentation, :x and ZZ are equivalent; they only save the file if it has been modified, then quit Vim: Write current file, if modified, and quit (same as ":x"). (Note: If there are ...
user avatar
  • 5,756
41 votes
Accepted

How to diff and merge two directories?

There is DirDiff.vim plugin to diff and merge two directories recursively. It performs a recursive diff on two directories and generate a diff "window". Based on that window you can perform ...
user avatar
  • 16.5k
25 votes
Accepted

Applying settings to a directory tree only

There are a few lightweight ways to do this. Check for a file of given name and source it if filereadable(".vimscript_file") so .vimscript_file endif The file is hidden in the example but that'...
user avatar
  • 2,278
25 votes
Accepted

How do I copy a file in netrw?

You cannot use y, p etc., to copy files in netrw - it comes with its own commands. See :h netrw-mc: MARKED FILES: COPYING netrw-mc {{{2 (See ...
user avatar
  • 23.7k
23 votes

What are the differences between :wq<cr> :x<cr> and ZZ when exiting vim?

As akshay pointed out, Vim's documentation explains, that :x and ZZ are equivalent and only save a file if the associated buffer has been changed. Whereas :wq saves the buffer to the corresponding ...
user avatar
  • 361
21 votes
Accepted

How can I make Vim autoread a file while it doesn't have focus?

:help 'autoread' doesn't have a lot of information on how it works, but Vim checks if an open file is modified in certain events (it always does this), and will reload it automatically if autoread is ...
user avatar
18 votes
Accepted

Can I search through 'oldfiles' with a specific name or extension?

Assuming you have Vim 8 you can use :filter :browse filter /pattern/ oldfiles For more help see: :h :browse :h :filter :h oldfiles :h new-items-8
user avatar
15 votes

Applying settings to a directory tree only

I use localvimrc for this purpose. Put a .lvimrc with your project settings inside your project and these settings will override settings in .vimrc. By default, you will be asked if you want to ...
user avatar
  • 3,226
12 votes
Accepted

How do I get filename completion to ignore a leading `=`?

The list of valid filename characters is given by the isfname variable. From :he 'isfname': 'isfname' 'isf' string (default for MS-DOS, Win32 and OS/2: "@,48-57,/,\,.,-,...
user avatar
  • 23.7k
12 votes
Accepted

How to do :Sex but with a vertical split?

What you're looking for is :Vexplore. From your question, it sounds like you started off knowing only about :Sexplore and understandably thought that that was the basic command for bringing up the ...
user avatar
  • 258
10 votes

How do I copy a file in netrw?

I would do this from the command line mode: Select the file you need in netrw Open command the line and type - :!mv <C-R><C-F> ../<C-R><C-F> Here <C-R><C-F> ...
user avatar
10 votes

How to pass current file name with a different path to the command line

Special characters (see :h cmdline-special) and filename modifiers (see :h filename-modifiers) are expanded on the command line, so: :diffsplit Path/to/other/directory/%:t
user avatar
  • 3,839
10 votes
Accepted

Is there an elegant way to determine if a file exists in/under runtimepath?

You're looking for globpath(). If it returns a non empty string, then there is at least a match. if !empty((globpath(&rtp, '/autoload/FooBarBaz.vim')) You may also want to check filereadable() ...
user avatar
9 votes
Accepted

Open a file at the given line using the colon notation

You can use the relatively popular file-line plugin. Note that this plugin seems relatively unmaintained, and there exists several forks. I have a personal fork where I've made some minor updates and ...
user avatar
9 votes
Accepted

Read file from vimscript variable

This is because vim does not evaluate variable names or functions with most commands. In your case, vim thinks you literally want to read the contents of a file called g:filePath into the buffer. ...
user avatar
8 votes
Accepted

Can I use some file-tree selector which exists on graphical IDEs?

Vim comes with a built-in file explorer called netrw. If you have a reasonaby recent Vim, the following command will give you the kind of feature you are missing: :Lexplore See :help netrw.
user avatar
  • 32.6k
8 votes

How can I go into a directory in filename completion?

I prefer to type / because extra/////slashes///are//ignored. Also, the arrow keys are poison. :)
user avatar
  • 631
8 votes
Accepted

How can I go into a directory in filename completion?

Simply press the right arrow key.
user avatar
  • 13.6k
8 votes

How to insert a file path with tab- or autocompletion relative to the current file's dir?

You can use vim's file name completion feature for this. To use it, press CTRL-xCTRL-f in insert mode. For more on this, see :help compl-filename. Do note that it isn't recursive, so you must ...
user avatar
8 votes

How to write a value or variable into a file using only vimscript?

writefile() is the ticket, but has some quirks you have to account for, such as splitting on newlines. An example: call writefile(split(varname, "\n", 1), glob('/path/to/file'), 'b')
user avatar
  • 2,081
8 votes

Find project root relative to the active buffer

After following dedowsdi's instructions, it turns out you just need to do: let dir = finddir('.git/..', expand('%:p:h').';') expand(%) expands into the path of the current file (excluding the ...
user avatar
  • 243
7 votes

Make :edit {file} use current directory during :Explore

I set the following two options to ensure that Vim's current working directory is always the same as the current buffer's. set autochdir " Changes the cwd to the directory of the ...
user avatar
7 votes

Make :edit {file} use current directory during :Explore

Yes, vim has a :cd command, which either prints the current directory or changes the current directory. In the help is this - note the last lines: :cd[!] {path} Change the current ...
user avatar
  • 323
7 votes

How can I make ctrlp do better filename matching?

A similar question was just asked on the Vim reddit here. I started using ctrlp-cmatcher myself quite awhile ago which I find to be much more intelligent at matching the right file. Note that it does ...
user avatar
7 votes

How do I open file under cursor, when the filename has a trailing dot?

From :help gf: Trailing punctuation characters ".,:;!" are ignored. So I suppose the dot at the end is not considered as part of the filename but as punctuation. I don't know if it can be done with ...
user avatar
  • 6,416
7 votes
Accepted

Is there a quick way to open a file anywhere within a directory hierarchy?

You can do this by setting the 'path' option to include **, and then using the :find command: :set path=.,,** :find filename.cpp The section on File Searching in the documentation describes how the *...
user avatar
6 votes
Accepted

How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?

Vim's 'path' option allows you to specify directories which commands like gf and :find will search for a file. If you only want this functionality to trigger for a specific set of files, then you ...
user avatar
  • 10.3k
6 votes

Filename Completion for Remote Files

Yes, but currently(2015) not out-of-the-box, because the built-in file-manager of vim doesn't natively support remote browsing. . Update Q3@2016: There are several work-arounds, with varying degrees ...
user avatar
6 votes

Read file from vimscript variable

The solution to this type of problem is the :execute command: :execute '0read' g:filepath See :help :execute
user avatar
  • 5,854
6 votes

Read file from vimscript variable

Commands like read don't evaluate your variables, it takes the argument to be a literal (a file named g:filePath). The execute command, however, takes string arguments, concatenates them and executes ...
user avatar
  • 3,119

Only top scored, non community-wiki answers of a minimum length are eligible