How to set environment variable through vi in Ubuntu 16.04

6,167

Vim steps would be:

  1. From your terminal open document for editing with commands:

    • sudo vi /etc/environment
    • Note that the sudo is needed since only users with elevated priveleges can edit files in the /etc folder
  2. To start editing the file from your keyboard use the following key combinations:

    1. Start to edit: i
    2. To move around, use keys h, j, k, and l
  3. After edit to get out of edit mode [ which was entered with i ], use esc

  4. After exiting to save: Shift + ;, then [wq | x], now press Enter

These are just basic steps, others exist so search and read up.

Quick tips:

  1. Always use the esc to get out of vim modes.

  2. I wrote something but don't want to keep it (provided you didn't do esc, Shift+;, w, Enter), use esc, then Shift+;, then q followed by !, then finally Enter.

  3. Finished want to save and quit at the same time, use esc, then Shift+;, then x, now finish with Enter

Share:
6,167
user716691
Author by

user716691

Updated on September 18, 2022

Comments

  • user716691
    user716691 over 1 year

    I'm trying to set my java environment variable but I am new to Ubuntu so don't know how to update it through Vi. I want to do the following things:

    Edit the environment file

    # vi /etc/environment
    

    Update the existing PATH variable by adding the below bin folders, separated with a colon:

    /usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin
    

    HOME directory paths can be different based on version and update,here the version is 1.8 and the update is 151. Add the below variables at the end of environment file, making changes for your specific version and update.

    J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151"
    J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre"
    JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151"
    DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"
    

    The environment file should now be similar to this text:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin"
    J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151"
    J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre*
    JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151"
    DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"
    

    My problem is I don't know much about editing and saving in Vi; I tried to do it through some resources but then it start giving warning and I don't want to corrupt things so I thought I should ask for help from some experienced users, so kindly help me through guiding step by step on how to do above things in Vi through terminal. I am using Ubuntu 16.04 LTS. Thank you!

    • Zanna
      Zanna about 6 years
      you do not have to use Vi. You can use any text editor, for example run sudo nano if you know nano. To edit and save a file using Vi(m), see How do I save files edited with vim?
    • user716691
      user716691 about 6 years
      @Zanna Thank you so much for telling me ,through sudo nano it was quiet easy :)
  • user716691
    user716691 about 6 years
    Thank you so much for your detailed response , I have a further query after updating I was using update-alternatives to inform ubuntu about installed java paths and it gave me following response : update-alternatives: using /usr/lib/jvm/jdk1.8.0_162/bin/java to provide /usr/bin/java (java) in auto mode So is it all good ,I am actually doing it first time so asking thank you !
  • user716691
    user716691 about 6 years
    and my initial command for update-alternatives was following: sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_162/bin/javac" 0
  • George Udosen
    George Udosen about 6 years
    Use this to see what you have: update-alternatives --list java
  • user716691
    user716691 about 6 years
    thanks ,i did that already and its showing /usr/lib/jvm/jdk1.8.0_162/bin/java Actually was confused when it said "in auto mode" so suspected as if I'm wrong somewhere because don't know what auto mode really is ,that's why asked .
  • George Udosen
    George Udosen about 6 years
    Please ask a fresh question!
  • user716691
    user716691 about 6 years
    Ok I was actually asking that after setting my variable I have given this command : sudo update-alternatives --install "/usr/bin/java" "javac" "/usr/lib/jvm/jdk1.8.0_162/bin/java" 0 and it responded with this update-alternatives: using /usr/lib/jvm/jdk1.8.0_162/bin/java to provide /usr/bin/java (java) in auto mode I want to know what 0 stands for in my first given command and what it really meant in its response when it said in auto mode Hope I am clear now ,thank you!