ebook img

Vi and Vim: Questions and Answers PDF

538 Pages·2016·6.41 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Vi and Vim: Questions and Answers

Table of Contents About this book Key Bindings (42 questions) Cursor Movement (26 questions) VimScript (24 questions) Command Line (22 questions) VIMRC (21 questions) Search (16 questions) Cut Copy Paste (15 questions) Substitute (15 questions) Insert Mode (15 questions) Save (14 questions) Autocompletion (14 questions) Regular Expression (14 questions) Indentation (13 questions) Plugin System (13 questions) Cursor Motions (13 questions) Split (11 questions) Ide (10 questions) Invocation (10 questions) Normal Mode (10 questions) Macro (8 questions) Buffers (8 questions) External Command (8 questions) Vim Windows (8 questions) Folding (7 questions) Filesystem (7 questions) Filetype (7 questions) REGISTER (7 questions) Highlight (7 questions) Terminal (7 questions) Autocmd (7 questions) Undo Redo (7 questions) GVim (7 questions) Ex Mode (7 questions) Line Numbers (6 questions) Syntax Highlighting (6 questions) Neovim (6 questions) Whitespace (6 questions) Line Breaks (6 questions) Startup (6 questions) Text Generation (5 questions) Visual Mode (5 questions) Multiple Files (5 questions) Performance (5 questions) Command History (5 questions) Word Processing (5 questions) Colorscheme (5 questions) Load (5 questions) Formatting (5 questions) Help System (4 questions) Spell Checking (4 questions) Status Line (4 questions) Alignment (4 questions) Microsoft Windows (4 questions) Repeated Commands (4 questions) History Of (4 questions) Original Vi (4 questions) Global Command (4 questions) Scrolling (3 questions) Tmux (3 questions) Tabbed User Interface (3 questions) Security (3 questions) Vimscript Python (3 questions) Filetype Html (3 questions) Filetype Tex (3 questions) Git (3 questions) Crash Recovery (2 questions) Large Documents (2 questions) Plugin Managers (2 questions) Letter Case (2 questions) Path (2 questions) Plugin You Complete Me (1 question) Vim Development (1 question) Copyright About this book This book has been divided into categories where each question belongs to one or more categories. The categories are listed based on how many questions they have; the question appears in the most popular category. Everything is linked internally, so when browsing a category you can easily flip through the questions contained within it. Where possible links within questions and answers link to appropriate places within in the book. If a link doesn’t link to within the book, then it gets a special icon, like this . Key Bindings Questions Q: Figure out which plugin is responsible for a key binding Tags: key-bindings (Next Q), plugin-system (Next Q) I was about to answer a question but realized that my answer depends on a key binding provided by a plugin I have installed. Even worse, I don’t know which plugin provides it. The only way I know to solve this problem would be to “binary search” my installed plugins by disabling half and enabling the other half. I use Vundle to manage my plugins so each iteration would only require me to comment out part of a plugin list but that’s still a clumsy procedure. Is there a better way to determine which plugin is responsible for a given key binding? Is there a way to determine if a given key binding is instead provided by my vimrc file directly? I realize Vim doesn’t have a builtin notion of plugins, so maybe the question is more precisely phrased as “How can I determine which file containing vimscript is responsible for a given key binding?”. Tags: key-bindings (Next Q), plugin-system (Next Q) User: praxeolitic Answer by badger You can use the following: :verbose map <c-p> replacing <c-p> with the key bind you’re looking for. Prints something like this: Last set from ~/.vim/bundle/ctrlp.vim/plugin/ctrlp.vim Tags: key-bindings (Next Q), plugin-system (Next Q) Q: How can I find out what <Leader> is set to? And is it possible to remap <Leader>? Tags: key-bindings (Prev Q) (Next Q) How can I figure out which key is set as my <Leader>, and how do I remap it? Tags: key-bindings (Prev Q) (Next Q) User: krampstudio Answer by orangetux By default your <leader> is \, backslash. You can check it with: :echo mapleader If this gives you an E121: Undefined variable: mapleader, it means it’s set to the default of \. If it gives you something else, then it’s that :-) You can easily remap it. I mapped it to the space-bar: :let mapleader = "\<Space>" Note that the value of mapleader is used at the moment the mapping is defined. So this example: let mapleader = "," nnoremap <Leader>a :echo "Hey there ,"<CR> let mapleader = "\<Space>" nnoremap <Leader>a :echo "Hey there space"<CR> Will produce two mappings: ,a and <Space>a. This means that the current value of mapleader is not necessarily the value that was used to define your mappings! In addition, there’s the maplocalleader, which is the same as mapleader, except that it’s used by <LocalLeader> and that it’s local to the current buffer. More information about <Leader> can be found in Vim’s help with :help mapleader . Answer by badger You can display the current leader key like this :let mapleader Tags: key-bindings (Prev Q) (Next Q) Q: Is there a <C-w> for WORDs? Tags: key-bindings (Prev Q) (Next Q), insert-mode (Next Q), word-processing (Next Q) Pressing Ctrl-w in Insert mode will delete the previous word. It is equivalent to Ctrl- Backspace in Windows, and Opt-Delete in OS X, and really speeds up typing. Vim has the separate concepts of word and WORD (see :help word). This is invaluable for text navigation: it is the difference between w and W, b and B, and so on. Ctrl-w operates on word. Is there a similar instruction for WORD? Tags: key-bindings (Prev Q) (Next Q), insert-mode (Next Q), word-processing (Next Q) User: david-lord Answer by chad I don’t believe there is one built in, but you can map one yourself in your vimrc: inoremap <c-b> <esc>vBda Tags: key-bindings (Prev Q) (Next Q), insert-mode (Next Q), word-processing (Next Q) Q: Run shell commands on current file based on file extension Tags: key-bindings (Prev Q) (Next Q), external-command (Next Q), filetype (Next Q) I’m currently doing a lot of work with both Ruby and JavaScript. I know that I can run my Ruby files with :! ruby % and likewise my JavaScript with :! node %, and that I can bind either of those to, eg, ,b. How can I set things up so that I can just bind a single command that will check the extension of the file I’m editing and run the appropriate command? Tags: key-bindings (Prev Q) (Next Q), external-command (Next Q), filetype (Next Q) User: tom Answer by carpetsmoker You could use :make for this; you can set makeprg (short for make program) to any command. Some examples: au FileType ruby set makeprg=ruby\ % au FileType javascript set makeprg=node\ % au FileType python set makeprg=python\ % au FileType coffeescript set makeprg=coffee\ -c\ % noremap ,b :make<CR> :make was originally intended to run the make tool to build the project, but languages such as Ruby or Javascript don’t need compiling, to setting it to something that will run the code makes sense. Tags: key-bindings (Prev Q) (Next Q), external-command (Next Q), filetype (Next Q) Q: Map a sequence of letters in insert mode Tags: key-bindings (Prev Q) (Next Q), insert-mode (Prev Q) (Next Q) I can already map a key combination in insert mode: :inoremap abc <do stuff> However, this has a few drawbacks: When typing a partial version of the sequence, such as ab, the characters aren’t actually displayed for a short amount of time, since it’s “waiting” to see whether you’re going to type the full command. This is undesirable; I want to map a chain of letters without pausing if a partial substring of the chain is entered. If I type ab, then pause (and wait until the characters actually appear as mentioned in the above point), then type the c, the command is not executed. I want the command to be executed no matter how long I wait. I would use an iab, but that doesn’t work because: 1. It requires entering a space after the string 2. I can’t execute arbitrary commands with it, only insert a string of text. Is there any way to execute a command or keystrokes if a certain string is entered in insert mode, without using a map (which will annoyingly pause every time I type an a and not work if I wait too long)? Tags: key-bindings (Prev Q) (Next Q), insert-mode (Prev Q) (Next Q) User: doorknob Answer by steve-vermeulen Here’s one way to do that: Skip code block let s:inputBuffer = '' augroup _silentInsertTrigger autocmd! autocmd InsertCharPre * call <sid>OnPreEnterChar() autocmd InsertLeave * call <sid>OnInsertLeave() augroup END function! s:OnPreEnterChar() let s:inputBuffer .= v:char if s:inputBuffer[-3:] ==# 'abc' echo '<do stuff>' endif return v:char endfunction function! s:OnInsertLeave() let s:inputBuffer = '' endfunction Tags: key-bindings (Prev Q) (Next Q), insert-mode (Prev Q) (Next Q) Q: Quickly switch between fonts at runtime Tags: key-bindings (Prev Q) (Next Q), gvim (Next Q) While fonts can be manually selected in GVim from the menu, I have a few preferred fonts I would like to switch between depending on the task at hand (small bitmap, larger OTF… etc). Is there a way I can setup key-binding to cycle over a list of fonts predefined in my vimrc? Tags: key-bindings (Prev Q) (Next Q), gvim (Next Q) User: ideasman42 Answer by toro2k The basic idea could be something like: Skip code block " Define a list of the fonts you want to use, and the index in the " list of the default font. See :help Lists let g:fc_list = [ \ "DejaVu Sans Mono 9", \ "Source Code Pro 12", \ "GohuFont 11" \ ] let g:fc_current = 0 " Set default font let &guifont = g:fc_list[g:fc_current] function! FontCycle() " Increment circular list. See :help expr-% let g:fc_current = (g:fc_current + 1) % len(g:fc_list) let &guifont = g:fc_list[g:fc_current] endfunction noremap <leader>fc :call FontCycle()<cr> Answer by quincy-bowers I have the following defined in my .vimrc file. set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 10 So you can set that up as a mapping like this… nmap <Leader>f :set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 10<CR> Add additional mappings for other fonts. Tags: key-bindings (Prev Q) (Next Q), gvim (Next Q) Q: How can I redefine plugin key mappings? Tags: key-bindings (Prev Q) (Next Q), vimrc (Next Q), plugin-system (Prev Q) (Next Q) I have installed a plugin that doesn’t provide a mechanism to disable the key mappings it defines. I want to redefine one of the mappings (<leader>cc) defined by the plugin for my own pursope, I’ve tried to put the following line in my .vimrc: noremap <leader>cc echo "my purpose" But this doesn’t work, <leader>cc is still executing the plugin command. How can I define my mapping so that it overrides those defined by the plugin? Tags: key-bindings (Prev Q) (Next Q), vimrc (Next Q), plugin-system (Prev Q) (Next Q) User: toro2k Answer by romainl Plugins are sourced after your vimrc so there’s no way to override a plugin mapping in your vimrc if the plugin doesn’t provide a way to do so. Placing your custom mapping in ~/.vim/after/plugin/mystuff.vim (the name of the file doesn’t matter) should allow you to override the plugin mapping. Answer by john-o’m. As mentioned in other answers, the plugins are sourced after the vimrc is done. If you want to keep your overrides in your vimrc instead of doing an after plugin, you can use this “trick” anywhere in your vimrc file: autocmd VimEnter * noremap <leader>cc echo "my purpose" From :help VimEnter : VimEnter: After doing all the startup stuff, including loading .vimrc files, executing the “-c cmd” arguments, creating all windows and loading the buffers in them. So, anything you put in a VimEnter auto command gets run after Vim is ready. Using VimEnter this way allows you to keep all your mappings with your other settings where you are used to keeping them: vimrc. Tags: key-bindings (Prev Q) (Next Q), vimrc (Next Q), plugin-system (Prev Q) (Next Q) Q: What is <Leader>? Tags: key-bindings (Prev Q) (Next Q) I see <Leader> quite often in other people’s vimrc files. Like this one. What is it? What does it do? Tags: key-bindings (Prev Q) (Next Q) User: oxinabox Answer by alexander-myshov Vim is full of various commands, which are assigned to almost all keys on the keyboard. But this causes a problem: Which commands can we use for our own commands, without interfering with existing ones? And at this moment, the <Leader> key comes into play. Think about <Leader>-key like a namespace for any user-defined commands. You can assign any command to a mapping with a leading <Leader> and you can be fully confident that your mapping won’t break anything. Default key for <Leader> is backslash. Answer by john-o’m. To quote :help <Leader> :

Description:
If you have a question about Vi and Vim this is the book with the answers. Vi and Vim: Questions and Answers takes some of the best questions and answers asked on the vi.stackexchange.com website. You can use this book to look up commonly asked questions, browse questions on a particular topic, com
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.