Last active
December 25, 2022 22:07
-
-
Save jamescherti/07aa562b0b263ba414655a5701a3868d to your computer and use it in GitHub Desktop.
Vim: an easy way to switch between the currently edited file and netrw (the local-directory browser), and vice versa (keyboard mapping: <Leader>e).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Language: Vim script | |
" Author: James Cherti | |
" URL: https://gist.github.com/jamescherti/07aa562b0b263ba414655a5701a3868d | |
" License: MIT | |
" Description: NetrwSwitch() will allow you switch between the currently | |
" edited file and netrw (the local-directory browser), | |
" and vice versa (keyboard mapping: '<Leader>e'). | |
function! NetrwSwitch() | |
if &filetype !=# 'netrw' && exists('w:netrw_switch_previous_buffer') | |
unlet w:netrw_switch_previous_buffer | |
endif | |
if exists('w:netrw_switch_previous_buffer') | |
if &filetype ==# 'netrw' && bufexists(w:netrw_switch_previous_buffer) | |
execute 'buffer ' . string(w:netrw_switch_previous_buffer) | |
call winrestview(w:netrw_switch_winsave) | |
if w:netrw_switch_previous_cwd !=# '' | |
execute 'lchdir ' . fnameescape(w:netrw_switch_previous_cwd) | |
endif | |
endif | |
else | |
if &filetype !=# 'netrw' | |
let w:netrw_switch_previous_buffer = bufnr('%') | |
let w:netrw_switch_previous_cwd = '' | |
if haslocaldir(1) | |
let w:netrw_switch_previous_cwd = getcwd(0) | |
endif | |
let w:netrw_switch_winsave = winsaveview() | |
execute 'edit .' | |
endif | |
endif | |
endfunction | |
nnoremap <silent> <Leader>e :call NetrwSwitch()<CR> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment