Ubuntu系统中如何在bashrc文件添加新行?及vim编辑异常解决
Hey there! Let's break down your two bashrc-related questions for Ubuntu clearly:
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 theechocommand with the>>operator (which appends instead of overwriting the file):echo "Your new line content here" >> ~/.bashrcFor example, if you're adding an environment variable like a custom PATH entry:
echo 'export PATH="$PATH:/path/to/your/directory"' >> ~/.bashrcAfter adding, run
source ~/.bashrcto 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 likenano(no fancy shortcuts needed):nano ~/.bashrcJust scroll to the end, hit Enter to create a new line, type your content, then press
Ctrl+Oto save andCtrl+Xto exit. For vim, we'll cover the correct workflow in the next section.
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:
- Open the file:
vim ~/.bashrc - Jump straight to the last line: Press
G(uppercase, in Vim's "normal" mode—this is the default mode when you open Vim). - Insert a new line below the final
fiand 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
filine, press$to jump to the end of the line, then pressato enter insert mode after the final character. Hit Enter to create a new line, then type your content.
- The easiest way: Press
- Save and exit: Press
Escto go back to normal mode, then type:wqand 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-tinydoesn'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

