I want to configure Vim such that when the [No Name] buffer is open, the syntax is set to markdown. Is this possible? I couldn't see such kind of hook in Vim's help.
I'm using Vim 7.3, compiled with --with-features=huge.
This is mentioned on the vim tips wiki:
" default filetype
let g:do_filetype = 0
au GUIEnter,BufAdd * if expand('<afile>') == "" | let g:do_filetype = 1 | endif
au BufEnter * if g:do_filetype | setf markdown | let g:do_filetype = 0 | endif
Why the need to use a global variable, and not set the filetype immediately, is to my understanding, because the buffer hasn't been fully created when the autocmd is triggered.
Related
I'd like the netrw explorer to automatically close after opening a file.
For example:
:Lex to open file explorer
Open file
Upon open, the file explorer automatically closes.
I've tried setting:
autocmd FileType netrw setl bufhidden=wipe
But it doesn't work.
Rest of relevant .vimrc settings:
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_altv = 1
let g:netrw_winsize = 25
noremap <C-x> :Lex<CR>
autocmd FileType netrw setl bufhidden=wipe
I took this snippet from another Stack Overflow answer (linked below) and added it to the BufWinEnter event to solve this problem.
" Close after opening a file (which gets opened in another window):
let g:netrw_fastbrowse = 0
autocmd FileType netrw setl bufhidden=wipe
function! CloseNetrw() abort
for bufn in range(1, bufnr('$'))
if bufexists(bufn) && getbufvar(bufn, '&filetype') ==# 'netrw'
silent! execute 'bwipeout ' . bufn
if getline(2) =~# '^" Netrw '
silent! bwipeout
endif
return
endif
endfor
endfunction
augroup closeOnOpen
autocmd!
autocmd BufWinEnter * if getbufvar(winbufnr(winnr()), "&filetype") != "netrw"|call CloseNetrw()|endif
aug END
Original answer here:
https://vi.stackexchange.com/questions/14622/how-can-i-close-the-netrw-buffer
This one in particular https://vi.stackexchange.com/a/29004
Try using :Explore, :Sexplore, :Hexplore, or :Vexplore instead of :Lexplore. The primary reason for :Lexplore is to have a persistent explorer on one side or the other; the others already go away when you select a file with <cr>.
I usually have long lived vim session with dozens of files open. Once in a while, I need to restart vim, say to install a new plugin or apply some new config.
How can I reopen all the previous files after restart?
You can use builtin sessions, perhaps with vim-session plugin.
A Vim session contains all the information about what you are editing.
This includes things such as the file list, window layout, global variables,
options and other information. (Exactly what is remembered is controlled by
the 'sessionoptions' option.)
The command
:mksession mysession.vim
saves the current session into the named file. Next time you start vim you can load the session:
vim -S mysession.vim
or inside vim
:source mysession.vim
You can automate saving sessions on VimLeave autocommand and reload a session on VimEnter but be careful abut restoring a session when vim is called, e.g., from git.
Here is a basic setup of automatic session save / load extracted from my config:
let s:session_loaded = 1
augroup autosession
" load last session on start
" Note: without 'nested' filetypes are not restored.
autocmd VimEnter * nested call s:session_vim_enter()
autocmd VimLeavePre * call s:session_vim_leave()
augroup END
function! s:session_vim_enter()
if bufnr('$') == 1 && bufname('%') == '' && !&mod && getline(1, '$') == ['']
execute 'silent source ~/.vim/lastsession.vim'
else
let s:session_loaded = 0
endif
endfunction
function! s:session_vim_leave()
if s:session_loaded == 1
let sessionoptions = &sessionoptions
try
set sessionoptions-=options
set sessionoptions+=tabpages
execute 'mksession! ~/.vim/lastsession.vim'
finally
let &sessionoptions = sessionoptions
endtry
endif
endfunction
It will only restore the session if you run vim without arguments, so if you do vim somefile.txt to do the quick edit, it will not touch the last session.
Bonus (this will restore cursor position inside files too):
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event
" handler.
autocmd MyAutoCmd BufReadPost *
\ if line("'\"") > 0 |
\ if line("'\"") <= line("$") |
\ exe("norm '\"") |
\ else |
\ exe "norm $" |
\ endif|
\ endif
I would like to exit on :bd if i'm on the last buffer. I am not very familiar with vimscript, but I assume something must be bound to the autocmd BufDelete.
From this answer:
:au BufDelete * if len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1 | quit | endif
Add the above snippet to your ~/.vimrc.
I set up an auto run script in my vimrc to condense any block of 3 or more empty newlines down to 3 newlines. I set a mark so after the script executes, I retain my cursor position but I'm getting an E20 Mark not set error when the cursor is within an area that is being removed.
How can I fix this issue/silence the error when this happens?
" .vimrc file:
autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | norm''
You could replace your marks with winsaveview() and winrestview().
autocmd BufWrite * let w:winview = winsaveview() | ... | if exists('w:winview') | call winrestview(w:winview) | endif
Also silence the normal command:
autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | silent! exe "norm! ''"
I have a problem in vim:
If I modify the .vimrc file and add this lines:
map ;bb A78
it just works in normal mode.
If I got it, it should work in insert mode too, shouldn't it?
While editing, I've verified that everything was read properly (command ":map"):
i ;bb A78
If I do the same thing with "imap", I got the same problem: command ":imap" shows it's configured, but if I go in insert mode, and type ";bb" or ";bb" or ";bb" nothing is changed, I don't get the A78
What am I missing?
(And the marvellous codeSnippet plugin works only in normal mode too, which is a big problem to me)
If forgot to precise: I have only the plugin Tabularize, it's vim version 7.3 under cygwin, but I get the same problem in SSH / Linux Debian / vim version 7.0
If I try to do exactly what written here (to give another try, if it may help), that doesn't work either: "To use the abbreviation, switch to Insert mode and type th, followed by any whitespace above (space, tab, or carriage return)." doesn't work at all. This drives me nuts.
Here follows my .vimrc file, maybe there's something wrong here I didn't see:
set nocompatible
filetype plugin on
syntax enable
set ignorecase
set paste
set ruler
set modeline
set showcmd
set expandtab
set tabstop=2
set autoindent
set smartindent
set number
colorscheme desert
set vb t_vb=
set backup
set backupdir=~/.vim/backup
set directory=~/.vim/tmp
set fileencodings=utf-8,ucs-bom,default,latin1
set scrolloff=5
set undolevels=1000
nmap ;bw :. w! ~/.vimxfer<CR>
nmap ;br :r ~/.vimxfer<CR>
nmap ;ba :. w! >>~/.vimxfer<CR>
" Tell vim to remember certain things when we exit
" '10 : marks will be remembered for up to 10 previously edited files
" "100 : will save up to 100 lines for each register
" :20 : up to 20 lines of command-line history will be remembered
" % : saves and restores the buffer list
" n... : where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo
" when we reload, tell vim to restore the cursor to the saved position
augroup JumpCursorOnEdit
au!
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ let JumpCursorOnEdit_foo = line("'\"") |
\ let b:doopenfold = 1 |
\ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
\ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
\ let b:doopenfold = 2 |
\ endif |
\ exe JumpCursorOnEdit_foo |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ exe "normal zv" |
\ if(b:doopenfold > 1) |
\ exe "+".1 |
\ endif |
\ unlet b:doopenfold |
\ endif
augroup END
set backspace=2
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
:autocmd BufNewFile * silent! 0r ~/.vim/templates/%:e.tpl
:autocmd BufNewFile *.php call search('w', '', line("w$"))
Thanks a lot!
You need to make sure that vim is not in "paste" mode.
Try
:set nopaste
map doesn't make the mapping work in insert mode: for ALL modes, you want map!. See :help :map! for more information on this.
However, imap should work, so you're probably having issues either with timeouts or the 'paste' setting. The way a mapping works in insert mode is that it gives you a certain amount of time to enter the mapping (I think the default is 1 second) and if you type it slower than that it assumes you mean the individual characters. So if you do:
:map! ;bb A78
And then type:
;<pause>bb
(where <pause> is just a pause, not something you type)
You'll get ;bb, but if you type:
;bb
really quickly, you'll get A78.
To find out more about timeouts, have a look at these help pages:
:help 'timeout'
:help 'ttimeout'
:help 'timeoutlen'
:help 'ttimeoutlen'
The 'paste' option also has an effect: it disables mappings in insert mode and abbreviations. Try :set paste? to find out if you have this set and :set nopaste to disable it.
See:
:help 'paste'