sponsor Vim development Vim logo Vim Book Ad

PushPop.vim : Pushd/Popd implementation for vim command-line

 script karma  Rating 32/13, Downloaded by 5030  Comments, bugs, improvements  Vim wiki

created by
Hari Krishna Dara
script type

- The script provides a pushd/popd functionality for Vim taking Bash as a
  reference. It defines new commands called Pushd, Popd and Cd and Dirs.
  It also defines abbreviations pushd and popd (the lower case versions)
  to make it easier to type and get used to, but if it causes any
  trouble for your regular command line typing (as the abbreviations are
  expanded anywhere you type), just set the g:pushpopNoAbbrev variable in
  your .vimrc, or install cmdalias.vim also along with PushPop.vim (in
  which case additional abbreviations are defined for dirs and cdp as
  well). The cmdalias.vim plugin provides support for defining
  abbreviations that expand only at the start of the command-line, which
  make the abbreviations behave just like bash aliases, or like the Vim
  built-in commands (read the plugin's description for more information).
- Most of the Bash pushd/popd syntax is supported.
- The plugin integrates seamlessly with the vim features such as 'cdpath'.
  It handles the protected command and space chars correctly as expected,
  however, unprotected spaces are not treated as separators (which is the
  case with 'cdpath' for the sake of backward compatibility with age old
  vim 3.0).
- Vim provides a "cd -" command to quickly jump back to the previous
  directory on the lines of bash, to make it complete, the script
  sets the g:OLDPWD variable to mimic the $OLDPWD variable of bash.
- The Cdp command will push the directory of the current file on to the
  stack and cd into it.
- It provides commands to make it easy to manipulate the "cdpath" and save
  and restore it (unless the persistence feature of genutils.vim is
  disabled), without needing to manually edit the vimrc. The "PPAddDir"
  command will allow you to add the specified directory or the current
  directory (with no arguments) to 'cdpath'. The "PPRemoveDir" command can
  be used to remove a directory by its name or index, or just the current
  directory (whith no arguments). Both commands accept "-l" as an option
  to specify the directory argument literally (the directory name is then
  not modified). The "-l" option with no arguments can also be used to
  add/remove "empty" directory entry (represented as ^, or ,, or ,$ in the
  'cdpath'). If you need to add entried that have wild-cards in them (such
  as "src/**", the above commands can't be used as Vim will try to expand
  the wildcards before the plugin can see them, so in this case use
  "PPAddPat" and "PPRemovePat" commands. Pass "-l" option to avoid
- To view the list of directories in your 'cdpath', use the regular "set
  cdpath?" command or just "Cdpath". In addition, the "Cd" command accepts
  "-i" option to cd directly into one of the directories in 'cdpath'. With
  no arguments, you will be prompted with the list of directories to
  select from, and with an index as the argument (starting from 0), you
  can directly cd into the directory at that index.
- If g:pushpopCdable_vars is set, the plugin emulates the bash
  'cdable_vars' feature. When the built-in ":cd" fails, the argument is
  assumed to be the name of a global variable whose value is the directory
  to change to. Normally, you don't necessarily need to use the :Cd
  command instead of :cd command, but in this particular case, the feature
  will not work unless you use the :Cd command. Consider aliasing :cd to
  :Cd as described in Deprecations section below.
- The main functions are also exposed as global functions, which can be used
  by the script writers to recursively traverse directories and finally
  return back to the original directory.

Please see the script header for more information such as deprecations etc.

Please send in your comments/feedback/suggestions/contributions for improving the script.

You can refer to the bash manual page for more information on how to use Pushd, Popd and Dirs commands. In case you don't have access to it, here is an extract from bash man page (on cygwin):

       pushd [-n] [dir]
       pushd [-n] [+n] [-n]
              Adds a directory to the top of the directory stack,
              or  rotates  the  stack,  making the new top of the
              stack the current working directory.  With no argu-
              ments,   exchanges  the  top  two  directories  and
              returns 0, unless the  directory  stack  is  empty.
              Arguments,  if  supplied,  have the following mean-
              +n     Rotates the stack so that the nth  directory
                     (counting from the left of the list shown by
                     dirs, starting with zero) is at the top.
              -n     Rotates the stack so that the nth  directory
                     (counting  from  the right of the list shown
                     by dirs, starting with zero) is at the  top.
              -n     Suppresses  the  normal  change of directory
                     when adding directories  to  the  stack,  so
                     that only the stack is manipulated.
              dir    Adds  dir to the directory stack at the top,
                     making it the new current working directory.

              If  the pushd command is successful, a dirs is per-
              formed as well.  If the first form is  used,  pushd
              returns  0  unless  the  cd to dir fails.  With the
              second form, pushd returns 0 unless  the  directory
       popd [-n] [+n] [-n]
              Removes  entries from the directory stack.  With no
              arguments,  removes  the  top  directory  from  the
              stack,  and performs a cd to the new top directory.
              Arguments, if supplied, have  the  following  mean-
              +n     Removes the nth entry counting from the left
                     of the list shown  by  dirs,  starting  with
                     zero.   For example: ``popd +0'' removes the
                     first directory, ``popd +1'' the second.
              -n     Removes the  nth  entry  counting  from  the
                     right  of  the  list shown by dirs, starting
                     with zero.  For example: ``popd -0'' removes
                     the  last directory, ``popd -1'' the next to
              -n     Suppresses the normal  change  of  directory
                     when removing directories from the stack, so
                     that only the stack is manipulated.

              If the popd command is successful, a dirs  is  per-
              formed  as  well, and the return status is 0.  popd
              returns false if an invalid option is  encountered,
              the directory stack is empty, a non-existent direc-
              stack is empty, a non-existent directory stack ele-
              ment is specified, or the directory change  to  the
              specified new current directory fails.
              tory stack entry is  specified,  or  the  directory
              change fails.
       dirs [-clpv] [+n] [-n]
              Without  options,  displays  the  list of currently
              remembered directories.  The default display is  on
              a  single  line  with  directory names separated by
              spaces.  Directories are added to the list with the
              pushd  command;  the  popd  command removes entries
              from the list.
              +n     Displays the nth  entry  counting  from  the
                     left  of the list shown by dirs when invoked
                     without options, starting with zero.
              -n     Displays the nth  entry  counting  from  the
                     right of the list shown by dirs when invoked
                     without options, starting with zero.
              -c     Clears the directory stack by  deleting  all
                     of the entries.
              -l     Produces a longer listing; the default list-
                     ing format uses a tilde to denote  the  home
              -p     Print the directory stack with one entry per
              -v     Print the directory stack with one entry per
                     line, prefixing each entry with its index in
                     the stack.

              The return value is 0 unless an invalid  option  is
              supplied  or n indexes beyond the end of the direc-
              tory stack.

Search_key_words: push pop directory cd change bash csh dirs pushpop genutils Hari Krishna Dara
install details
Just drop it in your plugin directory.

genutils.vim (2.0): vimscript #197
Depends on (Not required):
cmdalias.vim (1.0): vimscript #745

Please note that prior to 4.0 version, the plugin depended on the older versions of genutils and multvals
genutils.vim (1.1): vimscript #197
multvals.vim (3.0): vimscript #171

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
PushPop.vim 4.0 2007-02-02 7.0 Hari Krishna Dara Made to work with Vim7. Also, it uses the Vim7 Lists instead of
multvals, so it no longer depends on multvals and it is a lot cleaner.
PushPop.vim 2.4 2003-08-28 6.0 Hari Krishna Dara - Refined the handling of 'cdpath'. It works better now.
- Subtle improvements in the Pushd/Popd command usage.
- Delayed syncing is used to sync the top level directory in the stack with the current directory. You don't have to use the custom Cd command anymore, which means one less abbreviation to deal with. This also means that it will work seamlessly with other plugins that change directory as well (like fileexplorer).
- The script now takes advantage of cmdalias.vim when available for creating aliases to the capitalized commands. This is better than the plain cabbr's.
- There is a new 'cdable_vars' feature on the lines of bash.
- Fixed some misc. minor issues.

Please read the complete header in the script for changes and new usage.
PushPop.vim 2.3 2002-12-26 6.0 Hari Krishna Dara - Fixed bugs.
- Respect protected commas in cdpath and directory names.
- Minor improvements: Pushd also now takes -i argument. AddDir and RemoveDir take -l argument. Use g:pushpopPersistCdpath to control the persistnce of cdpath.
- Requires the new version of multvals.vim
PushPop.vim 2.2 2002-08-14 6.0 Hari Krishna Dara I missed to do an integration, so ended up with a wrong file here. I apologize, please update your version with this.
PushPop.vim 2.1 2002-08-14 6.0 Hari Krishna Dara I have totally redesigned it to work seamlessly with vim's cd and cdpath features. You can now pushd into a diretory that is in the cdpath. Also added new commands to manage the cdpath itself, without needing to change the .vimrc. Removed unnecessary command-line abbreviations. Removed Cdp command as it is a duplicate of "cd -" command and renamed Cdc command as Cdp. The script now requires genutils.vim for storing and retrieving cdpath.
PushPop.vim 2.0.5 2002-03-19 6.0 Hari Krishna Dara Fixed pushd with no-arg to switch between the top two dirs, instead of rotating (like with an index). Fixed cd to not to do a dirs. Added new commands cdp to quickly go back to the previous directory and cdc to pushd in to the directory of the currently open file.
PushPop.vim 2.0.2 2002-02-10 6.0 Hari Krishna Dara I finally made the script complete and plugin compatible. It now supports most of the bash pushd/popd syntax and is very compliant with it in the functionality and messages. Give it a shot, I am sure you will like it.

It also provided an commandline abbreviation for cd command to adjust the top most directory. You can set the global variable pushpopNoAbbrev to disable the commandline abbreviations such as cd, dirs, pushd etc., if they interfere with your regular usage.
PushPop.vim 1.0beta5 2001-10-22 5.7 Hari Krishna Dara Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.