zoukankan      html  css  js  c++  java
  • Basic Vim Configuration

    原文: https://computers.tutsplus.com/tutorials/basic-vim-configuration--cms-21498

    原来,vim的配置文件,.vimrc也是一种类似于shell ,javascript 的脚本语言。叫做vimScript

    Functions are very useful in the .vimrc file. Since the Vim’s configuration files use a full programing language, I will from here out refer to it’s true name: VimScript.

    -------------------------------------------------------

    After studying Vim for Beginners, you’ll have seen that the relative line numbering for your files do not survive a reload. Though this behaviour can be defined in the Vim configuration file.

    All of Vim configuration information is in the .vimrc file found in the home directory. You can open it with the edit file command from inside Vim:

    This file is empty because it has not been used. This tutorial will show you how to use it.

    Before you insert any code, you should document what is done. You might know what you are doing right now, but a month from now is a different matter. Always comment the code so that when you come back to it you’ll be reminded of why you coded something.

    For the .vimrc file, anything after a double quote to the end of a line is not used. Therefore, you can put anything there to help you remember what you did.

    Now you will always know that the set number command turns on line numbering. 

    To turn on line numbers and relative numbering, you need to put this in to the .vimrc file:

    Save that and you no longer have to think about it. Every time Vim opens a file, it will now show relative line numbers. The set command sets any option that is in Vim. An option is turned off by setting the opposite option. Opposite options are the same as the option with no in front.

    Use the following to turn off line numbering and relative numbering:

    Don’t put both sets in to the .vimrc file. That will turn it on and back off. In a little bit, I will show you how to do that more programmatically.

    If you already know the state of an option, toggling the option is another way to set the option. Options are toggled by putting a ! after the option name. This toggles on/off the options:

    If the current state of an option is needed, use a ? after the option name. In Vim, type:

    It will return number. This enables you know the state of the option.

    If you want to change the number of columns that the line number area shows, you can set the numberwidth to a value. The following will set the column width to 4:

    Try it out and see how many spaces you like and set that in to the .vimrc file.

    If you need a variable, variables are created using the let statement. For example:

    When you save that in the .vimrc file (but use your own name), it can displayed in command mode with :echo name.

    Seeing the Variable name

    Options are treated as a variable by prefixing the option with an &. Options can be printed by:

    The echo command is only for variables, but the prefixing an option with an & tells Vim to treat the option as a variable. This enables you to use the option’s value in math. Therefore, to increase the line number area width by one is done by using

    An echo &numberwidth should now show 5.

    All variables have their scope as well. Scope is defined by a letter, :, and the variable name. Undefined scope is treated as global scope. The different scopes are:

    b: Buffer scope—Only usable from within the current buffer
    w: Window scope—Only available from the current window
    t: Tab page scope—Only available in the tab page
    g: Global scope—available everywhere
    l: Local scope—available locally to that function defined
    s: Source scope—available only within the sourced file
    a: Argument scope—only available within the function
    v: Global scope—used to refer to a variable defined and used by Vim

    The proper definition for the name variable using global scope is:

    Now that you know how to set options and variables, you might wonder how do you find out the different options. Searching the web is one option, but the information is in Vim. Type:

    And Vim will show every option in the program. Typing the :let and an enter will show all of the variables.

    The :help command is used to lookup the extensive documentation built into Vim. To get out of :help, use :q. In help mode, this does not exit the program, but puts the editor in to normal mode.

    Functions are very useful in the .vimrc file. Since the Vim’s configuration files use a full programing language, I will from here out refer to it’s true name: VimScript.

    In VimScript, define a function using:

    I love the line numbering and relative numbering, but sometimes you do not want relative numbering. Typing the full string :set norelativenumber or even :set relativenumber! is a lot of typing. A function to turn it on and off would be good.

    A function is run with the :call command.

    The function command does not allow you to overwrite an already defined function. If that function was already defined somewhere else or you tried to reload the file again, Vim will error and stop processing the file at that point. If you use the function! command, it will overwrite the function without an error. It is good programming practice in VimScript to mostly use function!.

    To turn off all numbering, a function can be created to do that and it’s opposite. Add this to the .vimrc:

    To pass parameters to a function, you place a variable name inside the parenthesis. For example:

    As you see, you have to scope variables that are arguments of a function. When you call the function now with a name, you will get

    One of the most important uses for the configuration file is to setup key mappings. For example, you toggle relative numbers on and off a whole lot. Instead of typing :call TRelative all the time, a hotkey is much faster. To do that, the map command will help.

    With this line in the .vimrc file, you can now type Control-T and the relative numbering will toggle on and off. Try it out by making a mapping for toggling all line numbering.

    Key maps are not just for normal mode, but for every mode. If you prefix the command with the first letter of a mode, it will map the key for that mode. nmap also maps for normal modevmap maps for visual modeimap maps for insert mode.

    Now that you know these command, forget them. These commands can be dangerous. If you remap a key again, it will use the new mapping and can create an infinite loop in the mappings.

    To prevent that happening, you need to add nore after the mode and before the map. Therefore, nmap is nnoremapvmap is vnoremap, and imap is inoremap. These use the default mapping for any key command you reference in the macro. These are much safer to use in the configuration file.

    In this tutorial I have shown you the basics of creating a Vim configuration file. I have shown you how to: comment the configuration file, set/unset options and variables, create basic functions, and mapping keyboard shortcuts. The best way to remember is by practicing.

    ------------------------------------------------------------------------------------------------------------

    Install and setup vim on Ubuntu 14.04

    <note> I have upgraded to Ubuntu 16.04 and wrote another post about setting up vim, this time using vundle instead of pathogen. Checkout the new post– vim and vundle on Ubuntu 16.04 – if that sounds interesting to you. </note>

    Vim is a powerful editor that can be used on your laptop/desktop and is also typically found of any Linux server you might encounter. As a result, I started using vim as my main editor. The Ubuntu install is simple:

    $ sudo apt-get install vim
    
     

    That’s it, you’re ready to edit. A good place to start if you need to learn more about vim is the main website: vim . Also be sure to check out the vim tutorials.

    Pathogen

    My next step is to add a variety of vim plugins that make (my) life easier. To do this I use pathogen a wonderful vim package manager. Installation is accomplished with the following lines:

    $ mkdir -p ~/.vim/autoload ~/.vim/bundle;
    $ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
    

    If you don’t have curl installed, use:

    $ sudo apt-get install curl
    

    and try the second line again. Finally, if you don’t have a ~/.vimrc file create the following minimal example:

    " contents of minimal .vimrc
    execute pathogen#infect()
    syntax on
    filetype plugin indent on
    

    NERDTree

    NERDTree is a plugin the provides a nice file browser with bookmarks. With pathogen available, getting NERDTree installed is as simple as:

    $ cd ~/.vim/bundle
    $ git clone https://github.com/scrooloose/nerdtree.git
    

    After installing, start vim and type:

    :NERDTreeToggle
    

    to toggle the file browser open or closed. Use up and down arrows to select files and press enter to open file in current tab. To open in a new tab, select the file and press t.

    To bookmark, select a directory and type:

    :Bookmark bookmarkname
    

    to assign bookmarkname to the desired directory. To toggle the bookmarks open or closed (while in the NERDTree window) press shift-b. Finally, to open a bookmark, select the bookmark using up/down arrows and press enter when the desired bookmark is highlighted.

    Tagbar

    Tagbar does a nice job of showing code outlines– class, methods, etc and allows for jumping to different parts of the code using the outline. I’ve mainly used this with Python, where the results are very nice.

    First, we have to install exuberant ctags , which Tagbar uses to do its parsing. Luckily, there is version in the Ubuntu repository:

    $ sudo apt-get install exuberant-ctags
    

    Next, install the vim plugin using pathogen, as before:

    $ cd ~/.vim/bundle
    $ git clone git://github.com/majutsushi/tagbar
    

    Thanks to pathogen, we can now start vim and type:

    :TagbarToggle
    

    to toggle the code outline open or closed. To get to the code outline window press Cntrl-w and then l – this is a general vim command to move to the right window. Use up and down arrows to move through the code outline. When the desired class or function is highlighted press enter and vim will jump to the desired code. This is very nice for larger files!

    jedi-vim

    Next we install the jedi-vim plugin which allows for auto-complete and documentation search for Python projects. First, we install the Python package jedi

    $ pip install --user jedi
    

    I use this command assuming that you are installing all python packages as a user. Otherwise you will have install with sudo pip install jedi (global install), or activate the desired virtual environment and usepip install jedi.

    Finally, use pathogen to install jedi-vim in the usual way:

    $ cd ~/.vim/bundle/
    $ git clone https://github.com/davidhalter/jedi-vim.git
    

    The two command I use most with jedi are:

    • cntrl-space : auto-complete
    • shift-k : get documentation (must be in command-mode and put cursor on function of class of interest)

    vim-template

    vim-template is a plugin that provides nice file templates for new files. Using pathogen the installation is simple:

    $ cd ~/.vim/bundle
    $ git clone git://github.com/aperezdc/vim-template.git
    

    Now, try:

    $ vim test.py
    

    or,

    $ vim test.sh
    

    to see the standard templates for Python files and bash scripts, respectively.

    There are a variety of customizations that can be made (see the link above), but I like to add the following defaults to my .~/vimrc file:

    " Customize the settings for vim-template plugin
    let g:email = "desiredemail@gmail.com"
    let g:user = "Desired Name"
    let g:license = "Desired License"
    

    That’s it for my basic vim and vim-plugins setup. Questions and comments are always welcome.

  • 相关阅读:
    javascript的this
    javascript里的prototype
    【每天进步一点点 Python 】Python 字典(Dictionary) items()方法 用法
    【每天进步一点点--Python】字典dic类型 转换成 str 类型 json.dumps()
    【每天进步一点点
    【每天get 一点新知识】Python print 打印结果(字符串类型)前面添加 说明
    【每天get 到一点小知识】python 取response data 里面的数据
    【python web 开发】第三方登录开发模式及Oauth2.0 简介
    每天一个小程序:读取txt文件存储到excel 表中(2)
    每天一个小程序:读取txt文件存储到excel 表中
  • 原文地址:https://www.cnblogs.com/oxspirt/p/8874076.html
Copyright © 2011-2022 走看看