Home > linux > vim command that will do :buff or :e if the file is not already in the buffer?

vim command that will do :buff or :e if the file is not already in the buffer?

March 14Hits:1

basically my workflow is as follows I have a bunch of windows open using splits in a terminal in regular vim. Sometimes I have a file open sometimes I don't I usually switch between :buf and :tag to get the file. Ideally I would like something that would work like :buf but if the file is not in the buffer currently then it would preform a :e.

Therefore to simplify:

I type :buf and type part of a filename and hit <tab> it gives me the match for the files and matches as much of the file as it can if the file I want is not in the buffer already I would like to still be able to type that file name and have it load the file. Usually now I have to go back and change the command from a :buf [file] to a :e [file].

Is there a way to create a command that would work like a :buf but open the file if it doesn't already exist? The reason I use :buf to begin with is it limits the scope to files in certain directories and will match them when hitting tab. I hope I explained it correctly :).


:buf FooX[tab] 

shows 4 files and has

:buf [very long path]/FooX 

Now if the Exact file with prefix FooX is not shown I still want to be able to type FooXFileNotShown.cpp and have it load it like a :e would.

Thanks in advance for your help.


:b[uffer] is only used to access opened buffers. Its completion's "scope" is not restricted to "certain directories" but to the buffers in your buffer-list/argument-list. You can't use that command to open a file that wasn't opened already.

:e[dit] is used to edit a file and its completion is "scoped" to the files and directories directly under Vim's working directory. You can limit the completion list with the wildignore option (:help 'wildignore') or using the awesome ** wildcard (:help starstar).

When you use :e to edit a file that is already in the buffer-list/argument-list, Vim doesn't create a new buffer, it dimply switches to the existing buffer, effectively acting like :b.

So… It looks like you are a little confused about the behavior of :b and that you are simply looking for :e file<Tab>.

So you want a command with a completion of existing buffers, that then either opens the buffer (if existing), or else edits the passed file instead of giving you the E94: No matching buffer error.

How about this:

:command! -nargs=1 -complete=buffer Buf try | execute 'buffer' <q-args> | catch /^Vim\%((\a\+)\)\=:E94/ | execute 'edit' <q-args> | endtry

Custom commands must start with an uppercase letter, but you use the cmdalias.vim - Create aliases for Vim commands plugin to overload the existing :buf command.

Tags:linux, vim, buffer

Related Articles

  • vim command that will do :buff or :e if the file is not already in the buffer?March 14

    basically my workflow is as follows I have a bunch of windows open using splits in a terminal in regular vim. Sometimes I have a file open sometimes I don't I usually switch between :buf and :tag to get the file. Ideally I would like something that w

  • How to paste yanked text to vim command-line?September 6

    Hi is there any way I can paste the yanked text from a vim buffer to a vim command line, as I need to search for some file names which appear in current open text and I have to manually type the file name after :e --------------Solutions-------------

  • How do you run a vim command that is the text inside a buffer?December 28

    For example lets say this text is inside buffer %s/a/b/gI How do I run that as a vim command, without typing it inside the vim command line. Regards --------------Solutions------------- In command mode: You yank the text, say if it was on its own lin

  • Simple VIM commands you wish you'd known earlier August 14

    I'm learning new commands in VIM all the time, but I'm sure everyone learns something new once in a while. I just recently learned about this: zz, zt, zb - position cursor at middle, top, or bottom of screen What are some other useful or elegant comm

  • vim command to restructure/force text to 80 columnsJune 13

    I know there are ways to automatically set the width of text in vim using set textwidth (like Vim 80 column layout concerns). What I am looking for is something similar to = (the indent line command) but to wrap to 80. The use case is sometimes you e

  • Bash Scripting VIM commandsFebruary 18

    I have a few files that I have an annoyingly repetitive group of VIM commands to apply to. They are basic VIM commands just to delete some lines which are not supported by the process I need the files for. Say I have a file called postgresdb.out On t

  • How do I get vi-style line editing in Vim command mode?November 3

    When typing a command with ":" in Vim, I often find myself hitting escape and/or trying to navigate with h,j,k and l or trying other vi-style key combinations to edit the command I am writing. I have configured readline to use vi-style line edit

  • Can I use perl regular expressions in the vim command line?October 6

    I want to use perl regular expressions on the vim command line. For example, to capitalize the words on the current line, you could type: :s/(\w+)/\u$1/g --------------Solutions------------- You can filter any line or range of lines through an extern

  • Search exact word in vim command modeOctober 29

    I want to search a exact word in vim in command mode(using Vim command /wordtosearch , NOT the search of the current word at the cursor using * or # commands) e.g. I would like to search an exact word say CIF but I do not want to find CIF_handle or C

  • How do I automate a set of VIM commands that I use all the time?April 4

    I'm running some text files written in Markdown through Pandoc to get html, but it renders the tags strangely with weird line breaks and white space in tags, so I've been running a few sed commands in VIM. Should I be looking at Vimscripts or at macr

  • Are cygwin vim commands different than normal vim?April 20

    I am unable to edit text files using vim in cygwin. I have to press i many times to insert text. Sometimes it works and sometimes doesn't. Whenever I move cursor up down I have to press I many times. What could be the problem? Does backspace work in

  • Vim motion commands in the Vim command-lineOctober 11

    Sometimes in Vim, I'm typing a command and realise that I could really use Vim's motion commands for navigating the command-line: :s/somthing/else/ realise that I misspelled something. Fmae and be done with it. Of course, trying this would just put F

  • Create a Vim command to add a php debug statementOctober 21

    I'm trying to create a vim command to easily insert a debug statement on the variable name under the cursor. I would at least like to handle some common cases so it doesn't have to be perfect but the following should all work: $test_var = array($test

  • What is your favorite vi or vim command trick?

    What is your favorite vi or vim command trick?April 20

    I just saw a similar question about Emacs/AUCTeX so this one was coming. I use nvi for all my editing including TeX and it would not change it for anything. However, even after almost 20 years of use I never stop to be amazed by clever trick people s

  • How to run Vim command from the shell?July 21

    This might be a dumb question, but bear with me. I'm automating some of the usual stuff I do when setting up a new work environment, and would like to automate the Vim command :BundleInstall (for installing all my Vim plugins). Is it possible to run

  • vim: command not foundSeptember 5

    I'm trying to install openflow in Ubuntu, but I encounter a problem when installing regression suite. I follow the instruction from openflow website with the below lines To update the OF_ROOT environment variable for your setup: cd ~/ cp <openflow-di

  • How can you view source and keep Vim commands enabled in Vimium?April 9

    When I view source using Vimium, it disables the Vim commands. Ideally would like to keep the commands active so that I can search the source code using regex and avoid having to switch back to the mouse to close the tab or revert back to chrome comm

  • Vim command mapping helpJune 6

    I'm a relatively recent convert to Vim, so I don't know a whole lot. My question is about mapping a multi-part command to a single key combo. In my .vimrc, I've got the following line: nnoremap <leader>ss :w\|:silent !execute_external_script > ou

  • Hiding vim command-line when it's not being used?July 14

    Is there any way to hide the vim command-line (ideally, automatically) when it's not being used? On small screens it takes up one valuable line. I'm thinking it would be 'normally' hidden, and appear when :, /, etc. are typed. --------------Solutions

  • vim command palette, similar to sublime text

    vim command palette, similar to sublime textNovember 8

    In sublime text, we can press ⌘⇧P to bring up the . Are there any similar tools for vim? I've been trying vim-ctrlp-cmdpalette, and although it works relatively well (there are some small issues), it depends on ctrlp (not a huge problem), and it look

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 1.598 s.