Wikia

Vim Tips Wiki

Changes: Command line file name completion

Edit

Back to page

(Add longer variant that performs removal only left of the cursor.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=1593
 
|previous=1593
 
|next=1595
 
|next=1595
|created=July 22, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=
 
|author=
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
In the Vim command line (after pressing <tt>:</tt>), you can use file name completion after a command that expects a file name to be entered. For example, if you type <tt>:e my</tt> then press Tab, the "<tt>my</tt>" will be expanded to the next file name beginning with "my". This tip shows how to navigate directories by easily removing components from the file name.
+
In the Vim command line (after pressing <code>:</code>), you can use file name completion after a command that expects a file name to be entered. For example, if you type <code>:e my</code> then press Tab, the "<code>my</code>" will be expanded to the next file name beginning with "my". This tip shows how to navigate directories by easily removing components from the file name.
   
 
==Removing the last path component==
 
==Removing the last path component==
Line 37: Line 37:
 
</pre>
 
</pre>
   
Then you can press Ctrl-T to remove the last path component. It uses '<tt>isfname</tt>', and stops at characters not included, except escaped spaces. At least one character is always removed.
+
Then you can press Ctrl-T to remove the last path component. It uses '<code>isfname</code>', and stops at characters not included, except escaped spaces. At least one character is always removed.
   
 
----
 
----

Latest revision as of 06:37, July 13, 2012

Tip 1594 Printable Monobook Previous Next

created 2008 · complexity basic · version 7.0


In the Vim command line (after pressing :), you can use file name completion after a command that expects a file name to be entered. For example, if you type :e my then press Tab, the "my" will be expanded to the next file name beginning with "my". This tip shows how to navigate directories by easily removing components from the file name.

Removing the last path componentEdit

In the command line, when a file name is expected, you can press Tab or Shift-Tab for the next or previous file name. After starting file name completion, you can press Ctrl-N or Ctrl-P (next and previous) with the same effect. In addition, the standard Ctrl-W is available to delete the last word before the cursor.

It may happen that you browse a directory, and want to browse another directory:

:e dir1/long-file-name1
:e dir1/long-file-name2
...
:e dir1/long-file-name51

Now you want:

:e dir2/

in the command line. You could press Ctrl-N to cycle through the rest of the files, or press Ctrl-P to cycle back. Alternatively, you could press backspace to delete long-file-name51, or press Ctrl-W five times to remove the words "name51", "-", "file", "-" and "long" from the cmdline. A better procedure is to use the following script.

cnoremap <C-t> <C-\>e(<SID>RemoveLastPathComponent())<CR>
function! s:RemoveLastPathComponent()
  return substitute(getcmdline(), '\%(\\ \|[\\/]\@!\f\)\+[\\/]\=$\|.$', '', '')
endfunction

Then you can press Ctrl-T to remove the last path component. It uses 'isfname', and stops at characters not included, except escaped spaces. At least one character is always removed.


" Variant, first removes the extension.
function! s:RemoveLastPathComponent()
  let c = getcmdline()
  let cRoot = fnamemodify(c, ':r')
  return (c != cRoot ? cRoot : substitute(c, '\%(\\ \|[\\/]\@!\f\)\+[\\/]\=$\|.$', '', ''))
endfunction

All of the above always remove from the end of command line, regardless of the current cursor position. (And that is mostly fine, since it's the most common use case.) The following extends the last variant with performing removal only left of the cursor.

function! s:RemoveLastPathComponent()
  let l:cmdlineBeforeCursor = strpart(getcmdline(), 0, getcmdpos() - 1)
  let l:cmdlineAfterCursor = strpart(getcmdline(), getcmdpos() - 1)

  let l:cmdlineRoot = fnamemodify(cmdlineBeforeCursor, ':r')
  let l:result = (l:cmdlineBeforeCursor ==# l:cmdlineRoot ? substitute(l:cmdlineBeforeCursor, '\%(\\ \|[\\/]\@!\f\)\+[\\/]\=$\|.$', '', '') : l:cmdlineRoot)
  call setcmdpos(strlen(l:result) + 1)
  return l:result . l:cmdlineAfterCursor
endfunction
cnoremap <C-BS> <C-\>e(<SID>RemoveLastPathComponent())<CR>

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki