Repository: ivanceras/rust-vim-setup Branch: master Commit: c01fa411a602 Files: 4 Total size: 18.5 KB Directory structure: gitextract_0dni0wel/ ├── .gitignore ├── LICENSE ├── README.md └── VIM_Notes.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *~ *.swp *.swo ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Use VIM as your RUST IDE ### Install vim first, if its not already ```sh sudo apt-get install vim ``` If `vi` worked in your command line, it is most likely the light version of `vi` which is installed by default in Ubuntu, and it lacks most functionality of the real `vi`. You also need to install * `curl` * `git` * `CMake` * Python Headers * Development Tools I assume most of you have already installed it, if not then install it via ```sh sudo apt-get install curl sudo apt-get install git sudo apt-get install build-essential cmake sudo apt-get install python-dev python3-dev ``` ### Install pathogen plugin [Pathogen](https://github.com/tpope/vim-pathogen) is a vim plugin which allows you to easily install other plugins, by just adding files into the `~/.vim/bundle` (There are other alternatives, but pathogen is most commonly used) ```sh mkdir -p ~/.vim/autoload ~/.vim/bundle curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim ``` * Add this to `~/.vimrc` ```sh execute pathogen#infect() syntax on filetype plugin indent on ``` ### Install syntax highlighting for rust ```sh cd ~/.vim/bundle git clone https://github.com/rust-lang/rust.vim.git ``` ### Install YouCompleteMe [YouCompleteMe](https://github.com/Valloric/YouCompleteMe) - a code-completion engine for Vim with support for C-family languages and [Rust](http://blog.jwilm.io/youcompleteme-rust) ```sh cd ~/.vim/bundle git clone https://github.com/Valloric/YouCompleteMe cd ~/.vim/bundle/YouCompleteMe git submodule update --init --recursive ./install.py --rust-completer ``` * Download source code of the rust version you are using, for example [master branch](https://github.com/rust-lang/rust/) * Extract the zip file and put in your Development folder ie. `~/Developer` * Checkout and build racer ```sh mkdir -p ~/Developer/ cd ~/Developer/ git clone --depth 1 --branch master https://github.com/rust-lang/rust rust-master ``` Add this to your `.vimrc` ```sh let g:ycm_rust_src_path="/home//Developer/rust-master/src/" ``` ### Install Vim number toggle [vim-numbertoggle](https://github.com/jeffkreeftmeijer/vim-numbertoggle) adds line numbers to the code you are editing. Line numbers can either show absolute position or position relative to your cursor location. This will boost your productivity later, when you orchestrate vim commands such as moving 10 lines below the current cursor position: `10j`. ```sh cd ~/.vim/bundle git clone git://github.com/jeffkreeftmeijer/vim-numbertoggle.git ``` ### Install `NERDtree` for displaying files in a tab [NERDTree](https://github.com/scrooloose/nerdtree) is a file manager, that most text editors have, such as `sublime`, `atom`, or `eclipse` where it lists the files in the directory you are editing from. ```sh cd ~/.vim/bundle git clone https://github.com/scrooloose/nerdtree.git ``` * Add this to `~/.vimrc` ``` autocmd VimEnter * NERDTree autocmd BufEnter * NERDTreeMirror "CTRL-t to toggle tree view with CTRL-t nmap :NERDTreeToggle "Set F2 to put the cursor to the nerdtree nmap :NERDTreeFind ``` ## Controls ```sh CTRL-t - Open/Close the files tab CTRL-n - Toggle relative / absolute numbering CTRL-ww - Switch between the files tab and the main window F2 - Focus cursor to files tab - Open the focused files/directory, duh! h,j,k,l - Navigate the cursor left, down, up, right respectively i - Insert mode, you can start typing in your code. - Back to default mode, where you can issue commands in vi :w - Write/save the file, you are editing :wqa - Save the file, then quit the editor closing vi including the files tab ``` ## Some advance commands ```sh :bp - Open previous file/buffer :bn - Open next file/buffer :b - Open the file you are looking for without typing the exact filename :vsp - Vertically split the window :vsp - Open the file in vertical split :sp - Horizontal split :sp - Open the file in horizontal split ``` * You don't really have to quit the editor using `wq` whenever you want to build the project. A convenient way is to open a new tab in a terminal via `--t` and issue your command (`cargo build --release`) from there. That way, you don't lose the state of your editor, i.e. you can undo `u` or redo `-r` your code changes when needed ## Compile without opening another tab Alternatively, you can compile your project without opening another terminal instance by issuing the command directly from `vim` using `:! ` ```sh :! cargo run --release ``` ## Try to `vi`ew some files ```sh vi main.rs ``` * It should look something like this ![](https://raw.githubusercontent.com/ivanceras/rust-vim-setup/master/rust-vim.png) ## Enable mouse support If you want to use the mouse to point and click files and move the cursor around, add this to your `~/.vimrc` ``` "enable mouse support set mouse=a ``` Additionally, you can use arrow keys to move the cursor around. However, if you are really serious about using `vi` and want to maximize your `vi` skills, you should minimize the use of the arrow keys or the mouse. ![Using Mouse](https://raw.githubusercontent.com/ivanceras/rust-vim-setup/master/using-mouse.gif) ## A helpful vim cheatsheet at * http://vim.rtorr.com/ * [My Comprehensive List](https://github.com/ivanceras/rust-vim-setup/blob/master/VIM_Notes.md) ## Automatically reload files when changed Sometimes some of your files might get edited outside of your current `vi` session, such as other editors/code generators, dropbox sync, git pulls. * Add this to your `~/.vimrc` to refresh them automatically ``` " check file change every 4 seconds ('CursorHold') and reload the buffer upon detecting change set autoread au CursorHold * checktime ``` ## Enable the global Clipboard in vim In Linux distros, you have to install `vim-gtk` to gain clipboard functionality. [More information here.](http://stackoverflow.com/questions/3961859/how-to-copy-to-clipboard-using-vim) ```sh sudo apt-get install vim-gtk ``` Then you can Copy to `+` register, which is the global/OS clipboard `"+y` Paste from `+` register `"+p` Pasting is equivalent to `--v` in insert mode. It is equivalent to pasting (`-v`) in terminal. ## One step install for those who have never used vim before or people who want to do it automatically * This will replace your existing `~/.vimrc` and `.vim`, if you have one * Don't hold me responsible for breaking your vim configuration, your laundry machine or anything else that would make you angry. ```sh sudo apt-get install curl curl -sSf https://raw.githubusercontent.com/ivanceras/rustupefy/master/setup.sh | sh ``` ## Update * Update it via (same as setup) ```sh curl -sSf https://raw.githubusercontent.com/ivanceras/rustupefy/master/setup.sh | sh ``` ## Uninstall ```sh curl -sSf https://raw.githubusercontent.com/ivanceras/rustupefy/master/uninstall.sh | sh ``` ================================================ FILE: VIM_Notes.md ================================================ # Basic Controls CTRL-t open or close nerdtree CTRL-n toggle relative / absolute numbering CTRL-w w switch focus on nerdtree and open windows h,j,k,l navigate the cursor left, down, up, right respectively i insert mode, you can start typing in your code go back to normal/default mode, where you can issue vi commands :w write/save the file, you are editing :wqa save the file, then quit the editor closing vi including the files tab set focus on the nerd tree ## Rust specific commands CTRL-x CTRL-o activate auto rust autocompeletion (example: type in `String::` then press CTRL-x CTRL-o then select from the drop down list the appropriate functions) gd go to function/struct/variable declaration of the focused item ( this is equivalent to eclipse' open declaration ) gD go to declaration split open in window tab :bp to go back to the previous edited file (very useful when following a declaration) :b when you have open a lot of files, this comes in handy in showing back the previously open files # NERDTree specific commands o open files, directories and bookmarks go open selected file, but leave cursor in the NERDTree t open selected node/bookmark in a new tab T same as 't' but keep the focus on the current tab i open selected file in a split window gi same as i, but leave the cursor on the NERDTree s open selected file in a new vsplit gs same as s, but leave the cursor on the NERDTree O recursively open the selected directory x close the current nodes parent X recursively close all children of the current node e edit the current dif ENTER open files, directories and bookmarks double-click open files, directories and bookmarks D delete the current bookmark P jump to the root node p jump to current nodes parent K jump up inside directories at the current tree depth J jump down inside directories at the current tree depth CTRL-J jump down to the next sibling of the current directory CTRL-K jump up to the previous sibling of the current directory C change the tree root to the selected dir u move the tree root up one directory U same as 'u' except the old root node is left open r recursively refresh the current directory R recursively refresh the current root m display the NERD tree menu cd change the CWD to the dir of the selected node I toggle whether hidden files displayed f toggle whether the file filters are used F toggle whether files are displayed B toggle whether the bookmark table is displayed q close the NERDTree window A zoom (maximize/minimize) the NERDTree window ? toggle the display of the quick help # Cursor movement h move left j move down k move up l move right *Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.* CTRL-b page up CTRL-f page down % jump to matching brace w jump by start of words (punctuation considered words) W jump by words (spaces separate words) e jump to end of words (punctuation considered words) E jump to end of words (no punctuation) b jump backward by words (punctuation considered words) B jump backward by words (no punctuation) ge jump backward to end of words (punctuation considered words) gE jump backward to end of words (no punctuation) 0 (zero) start of line ^ first non-blank character of line $ end of line gg go to first line G go to last line of the file [N]G go To line N, (example 20G - go to Line 20) # Inserting/Appending text i start insert mode at cursor I insert at the beginning of the line a append after the cursor A append at the end of the line o open (append) blank line below current line (no need to press return) O open blank line above current line ea append at end of word ESC exit insert mode, to normal mode # Editing r replace a single character (does not use insert mode) J join line below to the current one cc change (replace) an entire line cw change (replace) to the end of word c$ change (replace) to the end of line s delete character at cursor and subsitute text S delete line at cursor and substitute text (same as cc) xp transpose two letters (delete and paste, technically) u undo CTRL-r redo . repeat last command ~ switch case g~iw switch case of current word gUiw make current word uppercase guiw make current word lowercase >> indent line one column to right << indent line one column to left == auto-indent current line ddp swap current line with next ddkP swap current line with previous # Cut and Paste dd delete (cut) a line dw delete the current word x delete current character X delete previous character D delete from cursor to end of line yy yank (copy) a line 2yy yank 2 lines yw yank word y$ yank to end of line p put (paste) the clipboard after cursor/current line P put (paste) before cursor/current line :set paste avoid unexpected effects in pasting # Visual Mode - Marking, Highligting text v start visual mode, mark lines, then do command (such as y-yank) V start Linewise visual mode (i.e selecting the whole line at the cursor location ) o move to other end of marked area U upper case of marked area CTRL-v start visual block mode (that is, selecting text in rectangular area marker) O move to Other corner of block aw mark a word ab a () block (with braces) aB a {} block (with brackets) ib inner () block iB inner {} block ESC exit visual mode, go to normal mode gv re-select the last selected visual area *Note: right after entering visual mode, you can use the same cursor movement commands in the normal/default mode to highlight your selection* # Visual Mode ## Commands > shift right the marked text < shift left the marked text c change (replace) marked text y yank (copy) marked text d delete (cut) marked text ~ switch case of the marked text ## Cut and paste 1. Place the cursor at the start of your text. 2. ma (marks the location as point 'a') 3. Place the cursor at the end of your text. 4. d'a (cuts back to location 'a') ## Exiting :w - write (save) the file, but don't exit :wq - write (save) and quit :x - same as :wq :q - quit (fails if anything has changed) :q! - quit and throw away changes # Search/Replace /pattern search for pattern ?pattern search backward for pattern n repeat search in same direction N repeat search in opposite direction :%s/old/new/g replace all old with new throughout file :%s/old/new/gc replace all old with new throughout file with confirmations % go to the corresponding (, {, [. * go to next occurrence of the word under the cursor # go to previous occurrence of the word under the cursor # Working with multiple files :e filename edit a file in a new buffer :n **/*.pl open all perl files under the current directory, recursively :tabe filename edit a file in a new tab (Vim7, gVim) :bnext (or :bn go to next buffer :bprev (or :bp go to previous buffer :bd delete a buffer (close a file) :sp filename open a file in a new buffer and split window CTRL-w s split windows CTRL-w w switch between windows CTRL-w q quit a window CTRL-w v split windows vertically CTRL-w x swap the current window with the next one :e# Open previous file CTRL-^ Edit the alternate file. Mostly the alternate file is the previously edited file. This is a quick way to toggle between two files. It is equivalent to ":e#", except that it also works when there is no file name. ### Moving the cursor in between windows CTRL-w h Move cursor left to the current window CTRL-w k Move cursor top to the current window CTRL-w j Move cursor bottom to the current window CTRL-w l Move cursor right to the current window ## Moving windows with respect to the current focused window CTRL-w H Move current window to the far left CTRL-w K Move current window to the top CTRL-w J Move current window to the bottom CTRL-w L Move current window to the right ## Window movements CTRL-w | Maximize the window horizontally CTRL-w _ Maximize the window vertically CTRL-w = Resize all window to equal sizes # Changeset, last edit jumping back and forth `. jump to the last edit g; go to previous edit g, go to next edit CTRL-o go to previous edit CTRL-i go to next edit '' jump to last location (no-edits) `` jump to last location (no-edits) # Fuzzy searching files with CTRL.P plugin CTRL-p Open up search user interface you can then start typing what you are looking for CTRL-f By default your search text is match against files, pressing CTRL-f will switch to match for content of the files, buffers, directories, etc. CTRL-b same as CTRL-f but cycle on opposite direction CTRL-j Move down 1 line of the search results CTRL-k Move up 1 line on the search results Open the file of the focused line in the search result CTRL-t Open the focused file of the line in the search result in a new tab CTRL-v Open the focused file of the line in the search result in a vertical split CTRL-x Open the focused file of the line in the search result in a horizontal split CTRL-z Mark select the focused file of the line in the search result CTRL-o Opens all the mark selected files in the search results