From the command line, if you type "vim some_directory/", vim will open a sort of file browser, where you can navigate through your directory structure by moving up or down with "j" and "k", and selecting with "enter" until you get to and open a file.
I'd like to write a function for vim in VimL that generates a (for now) arbitrary list of file names in your sub-directories and lists them in that file browser-like form mentioned above, where you can navigate to a file in the list by pressing "j" or "k" and selecting it (opening it, to edit) by pressing "enter".
Is that possible with VimL? If so, how? Bear in mind I'm interested in implementing the file-browser functionality, I've already figured out getting a list of file names.
Thanks in advance!
In Vim, this is implemented by scratch buffers; i.e. you open a new buffer with :new, populate it with Vimscript (:normal i... or :call setline(1, "foo")), and :setlocal nomodifiable.
Usually, the following settings are also applied:
:setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
To implement actions, use :map <buffer> to define special mappings just for that scratch buffer, e.g.
:nnoremap <buffer> <CR> :echo "you selected: " getline('.')
Voila!
Related
In vim, I select a text in visual mode, and type :w! ~/saved.txt, actually vim will execute :'<,'>w! ~/saved.txt. like this:
The command will save the text into ~/saved.txt. In another vim instance, I can use :r ~/saved.txt to read from ~/saved.txt to the other vim.
This implement a global copy-paste on the same machine. I want to make this a vim-command/key-mapping, while I don't know how to implement it.
A simple key-mapping:
vnoremap ,w :w! ~/saved.txt<cr>
nnoremap ,r :r ~/saved.txt<cr>
Replace ,w, ,r with your favour.
If I open vim with vim . netrw provides me with a nice list of the files and directories in the current directory.
If I open a file using v the file opens in a very narrow split down the left hand side of the screen and the directory listing remains open in a wide split on the right hand side of the screen.
Ideally I'd like it to have the opposite effect. ie. Show the directory listing in a narrow split on the left hand side of the screen and show the file in a wide split on the right hand side of the screen.
Any help much appreciated.
Netrw v153 and later (May 28, 2014) gives you the :Lexplore command, which, by default, opens a directory listing on the left hand side and opens files to the right (by pressing <cr>).
Whilst Jonathan.Brink's answer works perfectly well, simply adding
let g:netrw_altv=1
to .vimrc also seems to do the trick...
See https://superuser.com/questions/1056929/open-file-in-vertical-split-in-vim-netrw/1062063#1062063 for more info.
I'm sure this could be improved upon you can write a custom mapping that target's the netrw filetype.
Stick this in your .vimrc:
" open file vertically to the right
augroup netrw_mappings
autocmd!
autocmd filetype netrw call Netrw_mappings()
augroup END
function! OpenToRight()
:rightbelow vnew
:wincmd p
:normal P
endfunction
function! Netrw_mappings()
noremap V :call OpenToRight()<cr>
endfunction
The only thing is that you need to use V rather than v. For some reason I was unable to override netrw's v command, but using the capital version seems better anyway since it's not overriding a default.
I have noticed that when I use :cn :cp directly, vim always expands folds for me. But when I set my custom mapping to call :cn like :map <leader>n :cn<cr>, vim doesn't expand foldings when I use <leader>n in this case, I have to move horizontally after using that hotkey to unfold. I could script moving horizontally as well, but I still would like to understand why vim doesn't do that for me.
My foldopen setting contains quickfix.
Thanks.
According to this vim email archive.
This is the correct behavior and that you are supposed to open folds manually if you put :cn or :cp into mappings. The solution was to add zv to the end of the mappings.
So your maps should look like this
noremap <leader>n :cn<CR>zv
Note: I change map to noremap because noremap stops recursive mappings whereas map allows for it.
I opened a files in vim using
gf
command.which opened a file that the current cursor position.
I used
ctrl + ^
is used to toggle between the last two opened files.
But,
How can i go forward and backward opened the files?
:bn & :bp to cycle buffers
To cycle windows I use ctrl+w+w
To cycle opened file :next & :prev
To list opened buffers: :ls
from good old vi :e# reopens previous file ^^ (not using it since ages... well since VIM comes to my systems ^^)
EDIT: a quick test on gvim in windows (set nocp)
map <C-tab> ^[:bn^M
in order to enter escape (^[) and enter (^M) you have to press ctrl+v and then the special char
EDIT 2: a cleaner way to do this is putting this in .vimrc
map <C-tab> <esc>:bn<cr>
but just remember that using mnemonics for the keys depends on some other options (I cannot remember which one).
The best way is IMHO to write a VIM function saving the actual mode, performing switch and then restoring it, then mapping it to in every mode (:[xxx]map commands)
:next to next file
:prev to previous file
Alternatively, you might want to check out any one of the plugins trying to make buffer navigation easier
I myself have reduced it to <leader>bx, x being the number of the buffer.
Selecting the buffer is done via a nice popup list at the bottom.
To get this behavior, you would first install LustyJuggler ( https://github.com/vim-scripts/LustyJuggler ), and then set up the shortcut like so in your vimrc file:
nmap <leader>b :LustyJuggler<CR>
Note that this mapping only works when you are in normal mode.
You can also map the TAB key to quickly cycle through open buffers.
Add the following to your vimrc file:
:nnoremap <Tab> :bnext<CR>
:nnoremap <S-Tab> :bprevious<CR>
In the past, I have used Vim as a note taking platform by creating an index.txt file with a list of tags using the Vim help file format and then creating a bunch of text files that have the normal *Help_Tag* link syntax so that using CTRL-] on a tag in the index.txt file will jump to the respective tag in an arbitrary notes text file. You need to do :helptags dir to generate the tags for the directory (where dir is the path to the notes directory).
What I am looking for is a simple way to be on the left split window and open the tag under the cursor in the right split window. Something like CTRL-W v but for tag jumping and using the already open vertical split window.
The problem is if you do CTRL-] it will open the tag in the left pane and if you do CTRL-W CTRL-] it creates a horizontally split window in the left pane.
There must be a way to do this that I'm overlooking.
map <A-]> :vsp<CR>:exec("tag ".expand("<cword>")) " Open the definition in a new vsplit
Probably the easiset would be an autocommand local mapping
au FileType index.txt nnoremap <buffer> <cr>
\ :vert belowright split
\ |tag <c-r><c-w>
\ |vert resize 130<cr>
Note I use return
Try this mapping:
nmap <buffer> <C-]> :let word=expand("<cword>")<CR><C-W>l:exe "tag" word<CR>
It stores the word under the cursor in the variable word, then swaps the window and goes to the tag with that name. The <buffer> bit makes this mapping only apply to the current buffer.
If you're using headings as note titles, you could use Vim VOoM, which automatically outlines based on sytax and allows for easy navigation with the standard navigation keys.
Vim VOoM also works with foldmarkers, like in the example screenshot.