Skip to main content
Share Your Experience: Take the 2024 Developer Survey
30 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 ...
m4110c's user avatar
  • 451
20 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
Peter Rincker's user avatar
14 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 ...
Mike Holt's user avatar
  • 278
11 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() ...
Luc Hermitte's user avatar
  • 17.4k
11 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 ...
Foobar's user avatar
  • 273
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
Sato Katsura's user avatar
  • 4,009
8 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 *...
Greg Hewgill's 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')
Steven Lu's user avatar
  • 2,271
7 votes

How to diff and merge two directories?

If you just want to just use vimdiff without installing anything extra, the following command will open all the differing files in succession letting you look at the changes with vimdiff: for ...
Jongab's user avatar
  • 71
6 votes
Accepted

Is it possible to open a new file that starts with '+'?

From vim --help: Arguments: -- Only file names after this ... So this works on the command line: vim -- +models/File.m From inside Vim you need to escape the +: :e \+models/...
Sato Katsura's user avatar
  • 4,009
6 votes

Creating a list of files contained in a buffer

You can take advantage of the way vim execute command when applied over a range. Select your lines and do: :'<,'>call system('mkdir -p '. fnamemodify(fnameescape(getline('.')),':h:p').' &&...
nobe4's user avatar
  • 16.1k
6 votes

What workflow to use when editing large multifile projects

I used vinegar for a long time, and thanks to "the tpope way" taught me the native netrw shortcuts (which has a tree view by the way, press i 3 times). All I miss on a bare Vim is the - mapping to go ...
LEI's user avatar
  • 1,636
6 votes
Accepted

What is the correct way to escape UNC paths?

The problem you are encountering is, that the commandline subsystem on Windows does not support UNC paths. (Try running cmd.com and then cd \\server\path\to\dir and notice the error message CMD does ...
Christian Brabandt's user avatar
5 votes
Accepted

Can I iterate through files with a certain extension from the vim command line?

What you describe is exactly how Vim behaves. :e ~/Documents/myProj/*.txt<tab><tab><tab><tab>... You may want to turn on 'wildmenu' in your vimrc so you can see the list or ...
Peter Rincker's user avatar
5 votes

How do I copy a file in netrw?

Set cursor on ./ (or directory in which you want to copy) , press mt Set cursor on file you want copy, press mf, then mc, type a name of new file, enter
itsnikolay's user avatar
5 votes

How to `cd` / explore the directory containing the current file?

I there is a mean but it use a plugin, to solve that simply. First approach If it's only to visualize the current directory, you can use the :term command: :term ls %:p:h (filtering result %=current ...
Fabrice Hategekimana's user avatar
4 votes

Creating a list of files contained in a buffer

If you must do it with Vim: function! Touch() range abort for fn in getline(a:firstline, a:lastline) if !filereadable(fn) let base = fnamemodify(fn, ':h'...
Sato Katsura's user avatar
  • 4,009
4 votes

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

Another option is to enclose the filename in quotes: foo="/bar/baz" Then file completion (<C-x> <C-f>) works after the first ".
pete-may's user avatar
4 votes

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

from this answer (refering to an answer by PhanHaiQuang and a comment by flukus) One can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are ...
MacMartin's user avatar
  • 251
4 votes
Accepted

A side pane file explorer that can list files by modification dates?

If you use the netrw plugin (which is included by default in recent versions), you can sort by date. Start netrw with the command :Ex (or :Vex to make it open as vertical split or "side pane" as you ...
Tumbler41's user avatar
  • 7,756
4 votes
Accepted

Reading external file data at the end of the buffer

Are you looking for :$r file? -- which will move the cursor. Don't forget to play with marks I often use mx to mark and 'x to come back. You can also use: :call append('$', readfile('file')) -- which ...
Luc Hermitte's user avatar
  • 17.4k
4 votes
Accepted

How do I open the last n files I was working on in vim?

The files listed by :oldfiles are also available as a list in v:oldfiles, so something like: for f in v:oldfiles[:10] exec "badd" f endfor Using an appropriate value instead of 10, of course.
muru's user avatar
  • 24.9k
4 votes
Accepted

How to find a executable file inside a folder?

I don't fully understand your question, i know two ways to find all executable files inside a folder: readdir( require patch 8.1.1120) echo readdir('dir', {v-> executable('dir/' . v) && !...
dedowsdi's user avatar
  • 6,268
4 votes

Easily add/open a file in the same folder as the current file/buffer

:Ex<CR> followed by % will prompt you for a new filename in that directory, which can be a pretty smooth process. Note that there's also set autochdir if that fits your patterns/plugins better (...
Rich Churcher's user avatar
3 votes
Accepted

gf: can I get next file that matches?

From :h gf: gf E446 E447 [count]gf Edit the file whose name is under or after the cursor. ... If a [count]...
muru's user avatar
  • 24.9k
3 votes

A side pane file explorer that can list files by modification dates?

You could use netrw's :Lexplore command (usually shortened to :Lex) to get a side pane. As Tumbler41 said, you can change the sorting order with s; you can also have netrw start up with that sorting ...
user21497's user avatar
  • 811
3 votes

Is it possible to open a new file that starts with '+'?

More generally, you can start by specifying a directory name, so that the + (plus sign) appears in the middle of the file specification instead of at the beginning. Because all *nix operating systems,...
Eli Skolas's user avatar
3 votes

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

I made a little plugin vim-autoread that uses tail -f to update the buffer in the background asynchronously. This needs obviously the job feature of Vim Version 8. Use :AutoRead to turn it on and :...
Christian Brabandt's user avatar
3 votes

Copying absolute filepath of selected file in Netrw

You can select the file under cursor through visual block mode, like Ctrl+V, then press $ and then a j and then a y. Now, in command line mode, Type :echo @%.@" This will show the full path of the ...
SibiCoder's user avatar
  • 3,402
3 votes

How can I pass extra flags to FZF/ripgrep commands with the leader key?

Please note that I changed your mapping to suit my keyboard. However this is what I got in terms of functionality: nnoremap <C-P> :MyFind0<CR> nnoremap <Leader><C-P> :MyFind1&...
igorepst's user avatar
  • 143

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