You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu系统中如何在bashrc文件添加新行?及vim编辑异常解决

Hey there! Let's break down your two bashrc-related questions for Ubuntu clearly:

1. How to Add New Lines to Your .bashrc File

You've got two straightforward options here, depending on whether you want to use the command line directly or a text editor:

  • Append lines via command line (no editor required)
    This is super quick if you know exactly what line you want to add. Use the echo command with the >> operator (which appends instead of overwriting the file):

    echo "Your new line content here" >> ~/.bashrc
    

    For example, if you're adding an environment variable like a custom PATH entry:

    echo 'export PATH="$PATH:/path/to/your/directory"' >> ~/.bashrc
    

    After adding, run source ~/.bashrc to apply the changes immediately without restarting your terminal.

  • Edit via a text editor
    If you prefer to see the file content while editing, use a user-friendly editor like nano (no fancy shortcuts needed):

    nano ~/.bashrc
    

    Just scroll to the end, hit Enter to create a new line, type your content, then press Ctrl+O to save and Ctrl+X to exit. For vim, we'll cover the correct workflow in the next section.

2. Fixing Vim Issues When Adding Lines at the End of .bashrc

The weird behavior you're seeing—like fi moving or random characters (A/C/B) appearing instead of a new line—usually boils down to one of two things: using the stripped-down vim-tiny instead of full Vim, or not using the correct Vim modes. Here's how to fix it:

First, install full Vim (if you're using vim-tiny)

Ubuntu often ships with vim-tiny by default, which has limited functionality and wonky key behavior. Install the full version:

sudo apt update && sudo apt install vim

Correct Vim workflow to add lines at the end of .bashrc

Once you have full Vim, follow these steps to avoid issues:

  1. Open the file:
    vim ~/.bashrc
    
  2. Jump straight to the last line: Press G (uppercase, in Vim's "normal" mode—this is the default mode when you open Vim).
  3. Insert a new line below the final fi and start typing:
    • The easiest way: Press o (lowercase, still in normal mode). This automatically creates a blank new line below the current one and drops you into "insert" mode—no need to hit Enter first. Just type your environment variable here.
    • Alternate method: If you're on the fi line, press $ to jump to the end of the line, then press a to enter insert mode after the final character. Hit Enter to create a new line, then type your content.
  4. Save and exit: Press Esc to go back to normal mode, then type :wq and hit Enter (this means "write and quit").

Why you were seeing weird behavior

  • If you were in normal mode (not insert mode) and hit Enter or direction keys, Vim interprets those as commands—hence the random characters or line shifts. Always make sure you're in insert mode (look for -- INSERT -- at the bottom of the Vim window) before typing or hitting Enter.
  • vim-tiny doesn't support standard key mappings, so direction keys and Enter won't behave as expected. Installing full Vim fixes this entirely.

Don't forget to run source ~/.bashrc after editing to apply your new environment variables right away!

内容的提问来源于stack exchange,提问作者Squeez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:07:12