I have this mapping:
nnoremap <silent> <leader><space> :Files<CR>
This works fine, but when I have a changed file already open, I get an error when I switch to a new file.
Is it possible to save the file before starting fzf?
I tried something like this, but this doesn't work if I don't have a file open. (start vi without file and start fzf)
nnoremap <silent> <leader><space> :w<CR>:Files<CR>
First, use :update instead of :write. This will only write if there are indeed unpersisted changes.
You can check for an empty buffer with empty(bufname('')), but as there are other corner cases, I would rather just silence the error:
nnoremap <silent> <leader><space> :execute 'silent! update'<Bar>Files<CR>
Alternative
Alternatively, you can look into the 'hidden' option, as #romainl mentioned. With this, Vim won't complain if a buffer that has unsaved changes is not shown in a window any longer, and only confront you on quitting Vim. Many power users have :set hidden.
Related
I'm trying to write a function that allows me to toggle TPope's Vim Fugitive git-log viewer.
The plugin allows you to view the history of your project, by opening up git objects as buffers that begin with fugitive://
I'm trying to write a function in vimscript that will allow me to close all of these buffers at once, when they're open. The function currently looks like this, and is quite close to working:
function! ToggleGLog()
if buflisted(bufname('fugitive'))
:cclose
:bd fugitive*<C-a><cr>
else
Glog
endif
endfunction
command! ToggleGLog :silent :call ToggleGLog()
nnoremap <silent> <leader>gl :ToggleGLog<CR>
The problem that I'm encountering is that the <C-a><cr> portion of the function doesn't work. Normally, the <C-a> would expand the * selector to match all the buffers that start with "fugitive."
How can I write this so that the names of those buffers are automatically expanded and the :bd command will close them all?
How about the following?
execute "normal! :bdelete fugitive*\<C-a>\<CR>"
normal lets Vim run the command to its right as if you typed it, including keys like <C-a>. But as the latter is somewhat special, it needs to be interpreted; this is what execute is doing here. (See the end part of :help normal.)
I started using Vim recently, just installed NERDTree (a plugin to navigate files).
The command to access that plugin is :NERDTree so I though it's a good idea to start learning mappings by assigning one to that command.
So I added to my .vimrc file the following line: map :nt :NERDTree - but when I type :nt in a vim file (even after restarting) I receive the following error message: not an editor command: nt
I also tried to add the mapping directly while editing a file by typing :map :nt :NERDTree but it returned the same error when I tried to use the command.
I checked that answer:What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?, so it seems to me that :map (opposed to noremap etc.) is the good command for that.
The plugin works fine when typing the original command.
What am I doing wrong? (sorry for the noob question)
:NERDTree is a command, not a mapping, so there's no reason for creating a recursive mapping, here.
:map is too overreaching. You should use :<mode>map (for recursive mappings) or :<mode>noremap (for nn-recursive mappings).
You are missing a <CR> at the end of your mapping to tell Vim to actually execute the :NERDTree command.
In this specific case, the right mapping would be:
nnoremap :tn :NERDTree<CR>
But mapping something to :<anything> is not a good idea because it will introduce a timeout whenever you try to execute an Ex command. This means that you need to find another combo. Why not <Space>n?
nnoremap <Space>n :NERDTree<CR>
With the mapping that you have, it will be require multiple keystroke. Will it be okay for you to use a single key like F2?
nnoremap <F2> :NERDTreeToggle<CR>
This will toggle open/close NERDTree upon pressing F2 and save you some key stroke.
Here
you can figure out, how vim's mapping work and look like ;). Don't forget to source your new .vimrc before using.
There is this cool tip in Vim wikia to work with buffers:
nnoremap <F5> :buffers<CR>:buffer<Space>
How can I turn it into a toggle? That is, first press of <F5> shows the list of buffers, press it again, and it closes the list of buffers (for me it's more intuitive than pressing ESC) Also, I would like to do the same for :Hex with, say <F2>, that is, press <F2>, opens file explorer in a split, repress <F2>, closes file explorer.
I do not know what you mean by toggling buffer list. You can dismiss the command line via <esc>. I guess you could do cnoremap <f5> <esc>. This works because pressing <f5> in normal mode brings up the buffer list and keeps you in command-line mode. You map <f5> in command-line mode to <esc> to dismiss the prompt.
:Hex opens a split an netrw explorer buffer. You can simply close the buffer any number of ways: <c-w>c, :q, :close, and so on. To make an <f2> mapping that closes a netrw buffer you can add the following to you ~/.vimrc file:
augroup ToggleNetrw
autocmd!
autocmd FileType netrw nnoremap <buffer> <f2> :<c-u>close<cr>
augroup END
Your <f2> mapping does :Hex and we map a buffer specific <f2> mapping for netrw filetypes to close the buffer via :close.
:Lexplore will toggle the explorer in the current buffer's directory.
You can map to something like:
nnoremap <F2> :Lex<CR>
alternatively you can give a size for the opening netrw:
nnoremap <F2> :20Lex<CR>
In the above example, 20 is the percentage drawn from the current buffer size.
More information can be found by the vim command: :h Lexplore among other resources.
------- EDIT about toggling buffer list.
You can toggle the buffer list after executing the mapping you have by pressing the tab key, if that's what you mean.
I'm trying to add a mapping to vimrc so if I press \r the current buffer will close without saving changes. I think :enew! is what I'd like to map but the way I've done it is wrong:
" Use Leader-r for closing the buffer without saving changes, also in Insert mode
nnoremap <Leader>r :<C-U>enew!<CR>
vnoremap <Leader>r :<C-U>enew!<CR>gv
cnoremap <Leader>r <C-C>:enew!<CR>
inoremap <Leader>r <C-O>:enew!<CR>
How do I amend the above?
Edit: As pointed out by glts what I'd actually like to do is discard the current buffer without saving changes
As the help states, :enew! discards changes made to the current buffer. Instead, use :hide enew, or, as many users prefer, set hidden in your vimrc and then just use :enew.
Or maybe what you actually meant was how to delete the current buffer without saving the changes? In that case, use :bd! instead of :hide enew.
nnoremap <Leader>r :<C-U>hide enew<CR>
xnoremap <Leader>r :<C-U>hide enew<CR>
cnoremap <Leader>r <C-C>:hide enew<CR>
inoremap <Leader>r <Esc>:hide enew<CR>
Personally I would advise against making such "universal" mappings. A normal mode mapping should be enough since a single Esc will take you there anyway.
An advanced solution suggested by #ZyX is to squeeze these four lines into one with this bit of <C-\><C-N> magic:
for a in ['n','x','c','i'] | exe a.'noremap <Leader>r <C-\><C-N>:hide enew<CR>' | endfor
I want to remap <PageUp> to <C-u> and PageDown to <C-d> per the Vim scrolling documentation.
As it stands right now, my /etc/vim/vimrc looks like this:
nnoremap <PageUp> <C-u>
nnoremap <PageDown> <C-d>
I've tried a lot of different combinations and nothing I've done has worked.
My goal is to make the cursor move to the Start Of File or EOF when holding down PageUp/PageDown. As it is right now, the cursor stops before it gets all the way to the top (and PageDown scrolls past the EOF). Just annoyances I'm trying to fix.
EDIT: The above settings work fine. I was placing my mappings too early in the file.
What about the following mappings?
nnoremap <PageUp> gg
nnoremap <PageDown> G
Or simply using gg and G?
Instead of placing the mappings into the system-wide /etc/vim/vimrc, you should put user customizations into the ~/.vimrc file. Nonetheless, the global configuration (if that's what you want) should work, too. That it doesn't means that the mappings get cleared or redefined. You can check with
:verbose nmap <PageDown>
If it didn't get redefined, you have to hunt for :nunmap commands in all loaded scripts (:scriptnames), or capture a log with vim -V20vimlog.
You can do this with
map <silent> <PageUp> 1000<C-U>
map <silent> <PageDown> 1000<C-D>
imap <silent> <PageUp> <C-O>1000<C-U>
imap <silent> <PageDown> <C-O>1000<C-D>
from fixing-pageup-and-pagedown